求一条SQL语句的写法

Ivan光辉岁月 2011-11-17 05:00:41
各位大侠,如何用一个SQL语句写出如下的题目。在线等哦。

写出当月商品销售金额占总金额80%的商品有哪些?
不要用游标,不要用存储过程、不要用临时表倒来倒去。是否有办法,请各位高手拆招了。

谢谢!

据了解可以用Top Percent处理,但是不知道怎么处理?
...全文
166 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ivan光辉岁月 2011-11-21
  • 打赏
  • 举报
回复
没有得到正确的解答,结贴了。
fanmei123 2011-11-19
  • 打赏
  • 举报
回复
declare @samount decimal(18,2)
select @samount = sum(amount) from tb

select convert(varchar(7),date,120) as date,productid
from tb
group by convert(varchar(7),date,120),productid
having sum(amount) = @samount*0.4

Ivan光辉岁月 2011-11-18
  • 打赏
  • 举报
回复

Create Table Sales(
GdCode Varchar(20),
Amount Money )

insert into Sales
select'A',20 Union ALL
select'B',18 Union ALL
select'C',15 Union ALL
select'D',14 Union ALL
select'E',12 Union ALL
select'F',10 Union ALL
select'G',6 Union ALL
select'H',5
Ivan光辉岁月 2011-11-18
  • 打赏
  • 举报
回复

表结构:
Create Table Sales(
GdCode Varchar(20),
Amount Money )

insert into Sales
select'A',20 Union ALL
select'B',18 Union ALL
select'C',15 Union ALL
select'D',14 Union ALL
select'E',12 Union ALL
select'F',10 Union ALL
select'G',6 Union ALL
select'H',5
Ivan光辉岁月 2011-11-18
  • 打赏
  • 举报
回复
可以使用基本的SQL语句,但是禁止使用游标实现。
--小F-- 2011-11-18
  • 打赏
  • 举报
回复
select top 80 percent GdCode,sum(Amount) as Amount from sales group by GdCode order by 2 desc
--小F-- 2011-11-18
  • 打赏
  • 举报
回复
晕了 根据你的语句没有一个满足要求


时间字段也没有。
pengxuan 2011-11-18
  • 打赏
  • 举报
回复
A+B+C+D+E+F 占当月销售的前80%
A+B+C+D+E+G 占当月销售的前80%
A+B+C+D+E+H 占当月销售的前80%

好多组合哪,怎么取
昵称被占用了 2011-11-18
  • 打赏
  • 举报
回复
SELECT TOP (80) PERCENT
GdCode
,SUM(Amount) AS Amount
FROM Sales
GROUP BY GdCode
ORDER BU SUM(Amount) DESC
Ivan光辉岁月 2011-11-18
  • 打赏
  • 举报
回复
有一点没有说清楚,应该是占当月销售前80%的商品有哪些?

表结构来了:
Create Table Sales(
GdCode Varchar(20),
Amount Money )

insert into Sales
select'A',20 Union ALL
select'B',18 Union ALL
select'C',15 Union ALL
select'D',14 Union ALL
select'E',12 Union ALL
select'F',10 Union ALL
select'G',6 Union ALL
select'H',5
通过语句能够查出 A、B、C、D、E、F商品(他们的当月销售占当月总销售的前80%)。
Felixzhaowenzhong 2011-11-18
  • 打赏
  • 举报
回复
感觉回到了高中,老师没有出题目或留作业,就把这些做完了。强人都这样。
流年筱澈 2011-11-17
  • 打赏
  • 举报
回复
表的结构很烂诶
dawugui 2011-11-17
  • 打赏
  • 举报
回复
--这是按照你的需求,在sql 2005的示例库中做的.
select * from
(
select m.* , bl =
(select sum(customerid) from
(
select salespersonid , sum(customerid) customerid from sales.store group by salespersonid
) n where n.customerid >= m.customerid) * 100.0/ (select sum(customerid) from sales.store)
from
(
select salespersonid , sum(customerid) customerid from sales.store group by salespersonid
) m
) k where bl <= 80


--salespersonid-->你的商品ID
--customerid -->你的销售
--另外你需要加上月份的限制.
--小F-- 2011-11-17
  • 打赏
  • 举报
回复
没有表结构 乱猜么?
pengxuan 2011-11-17
  • 打赏
  • 举报
回复
楼主还是把表结构,数据和想要的结果集贴出来吧
叶子 2011-11-17
  • 打赏
  • 举报
回复

DECLARE @t TABLE(
编号 INT IDENTITY,产品 VARCHAR(10),金额 DECIMAL(18,4))

INSERT INTO @t
SELECT '产品1',21.00 UNION ALL
SELECT '产品2',210.00 UNION ALL
SELECT '产品3',12.00 UNION ALL
SELECT '产品4',10.00

SELECT 产品 FROM @t WHERE 金额/(SELECT SUM(金额) FROM @t)>0.8
/*
产品
----------
产品2
*/
dawugui 2011-11-17
  • 打赏
  • 举报
回复
写出当月商品销售金额占总金额80%的商品有哪些?

我怎么觉得只可能有一种商品,而不是那些商品呢?
AcHerat 元老 2011-11-17
  • 打赏
  • 举报
回复

declare @samount decimal(18,2)
select @samount = sum(amount) from tb

select convert(varchar(7),date,120) as date,productid
from tb
group by convert(varchar(7),date,120),productid
having sum(amount) = @samount*0.4
dawugui 2011-11-17
  • 打赏
  • 举报
回复

建议你提供详细的资料:
例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。
这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧