求一个Sql语句,有点烦

xiao_fen_tuan 2008-04-03 11:45:34
表结果为:
id smallclassid smallclassname thumbgallery isindex hit
1 343 测试 xxx 1 100
2 343 测试 xxx 0 200
3 200 test xxx 1 300
4 200 test xxx 0 300

要求计算出hit总和排在前6位的分类值(也就是必须按照smallclassid分),并按照hit总和的降序排列且isidnex的值为1,页面中需要用到smallclasid,smallclassname,isindex,thumbgallery这四个字段
...全文
116 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 happyflystone 的回复:]
恩,手误了一下..谢谢了..
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
谢谢各位的帮助,结贴了...
-狙击手- 2008-04-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xiao_fen_tuan 的回复:]
我可能每表达清楚..我希望得出的结果如果按happyflystone的方法应该是,就是每一类的Hit的总和统计出来

smallclassid smallclassname isindex thumbgallery hits
------------ -------------- ----------- -------------------- -----------
200 test 1 xxx 300
343 测试 1 xxx 600
[/Quote]

最后的hits 反了吧
kaikai_kk 2008-04-04
  • 打赏
  • 举报
回复
还是不明白LZ这个600从那里来?
-狙击手- 2008-04-04
  • 打赏
  • 举报
回复
create table ta(id int, smallclassid int, smallclassname varchar(10), thumbgallery varchar(20),
isindex int, hit int)
insert ta select
1 , 343 , '测试' , 'xxx', 1, 100 union select
2, 343 , '测试' , 'xxx' , 0 , 200 union select
3 , 200 , 'test' , 'xxx' , 1 , 300 union select
4 , 200 , 'test', 'xxx' , 0 , 300


go

select a.smallclassid,smallclassname,isindex,thumbgallery,hits
from ta a,(
select smallclassid ,sum(hit) as hits
from ta
group by smallclassid ) b
where a.smallclassid = b.smallclassid
and isindex = 1

order by hits desc
drop table ta

/*

(所影响的行数为 4 行)

smallclassid smallclassname isindex thumbgallery hits
------------ -------------- ----------- -------------------- -----------
200 test 1 xxx 600
343 测试 1 xxx 300

(所影响的行数为 2 行)
*/
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
我可能每表达清楚..我希望得出的结果如果按happyflystone的方法应该是,就是每一类的Hit的总和统计出来

smallclassid smallclassname isindex thumbgallery hits
------------ -------------- ----------- -------------------- -----------
200 test 1 xxx 300
343 测试 1 xxx 600
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 pt1314917 的回复:]
引用 8 楼 xiao_fen_tuan 的回复:
谢谢大家...忘记说了一点..IsIndex每一类只能有一个值为1....所以这样的话结果出来就不一样了...


是每一类中有多个1的,只取一个1吗?
[/Quote]

就是说每一个类里只有一行的IsIndex的值为1,其他均为0
-狙击手- 2008-04-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiao_fen_tuan 的回复:]
谢谢大家...忘记说了一点..IsIndex每一类只能有一个值为1....所以这样的话结果出来就不一样了...
[/Quote]

怎么每一次不一样呀


应该一样
pt1314917 2008-04-04
  • 打赏
  • 举报
回复
按照楼主的意思结果应该是怎样的呢/
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
happyflystone,你那样统计出来的hits不是一类的总和啊...
pt1314917 2008-04-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiao_fen_tuan 的回复:]
谢谢大家...忘记说了一点..IsIndex每一类只能有一个值为1....所以这样的话结果出来就不一样了...
[/Quote]

是每一类中有多个1的,只取一个1吗?
xiao_fen_tuan 2008-04-04
  • 打赏
  • 举报
回复
谢谢大家...忘记说了一点..IsIndex每一类只能有一个值为1....所以这样的话结果出来就不一样了...
-狙击手- 2008-04-04
  • 打赏
  • 举报
回复
晕 ,KK 已经回复
kaikai_kk 2008-04-04
  • 打赏
  • 举报
回复
可以把别名A去掉

PS:数据和LZ的不同
-狙击手- 2008-04-04
  • 打赏
  • 举报
回复
create table ta(id int, smallclassid int, smallclassname varchar(10), thumbgallery varchar(20),
isindex int, hit int)
insert ta select
1 , 343 , '测试' , 'xxx', 1, 100 union select
2, 343 , '测试' , 'xxx' , 0 , 200 union select
3 , 200 , 'test' , 'xxx' , 1 , 300 union select
4 , 200 , 'test', 'xxx' , 0 , 300


go
select top 6 smallclassid,smallclassname,isindex,thumbgallery,sum(hit) as hits
from ta
where isindex = 1
group by smallclassid,smallclassname,isindex,thumbgallery
order by sum(hit) desc,isindex

drop table ta

/*
smallclassid smallclassname isindex thumbgallery hits
------------ -------------- ----------- -------------------- -----------
200 test 1 xxx 300
343 测试 1 xxx 100

(所影响的行数为 2 行)
*/
kaikai_kk 2008-04-04
  • 打赏
  • 举报
回复


declare @tb table(id int, smallclassid int, smallclassname varchar(10),thumbgallery VARCHAR(10), isindex INT, hit INT)
INSERT @TB
SELECT 1 , 343 , '测试', 'xxx', 1 , 100 UNION ALL
SELECT 2 , 343 , '测试', 'xxx', 1 , 700 UNION ALL
SELECT 3 , 343 , '测试', 'xxx', 1 , 100 UNION ALL
SELECT 4 , 343 , '测试', 'xxx', 0 , 200 UNION ALL
SELECT 5 , 200 , 'test', 'xxx', 1 , 300 UNION ALL
SELECT 6 , 200 , 'test', 'xxx', 0 , 300 UNION ALL
SELECT 7 , 200 , 'test', 'xxx', 1 , 500 UNION ALL
SELECT 8 , 200 , 'test', 'xxx', 1 , 300


--SELECT * FROM @TB

SELECT TOP 6 A.smallclassid,A.smallclassname,A.thumbgallery,A.isindex,SUM(HIT) HIT
FROM @TB A WHERE isindex=1
GROUP BY A.smallclassid,A.smallclassname,A.thumbgallery,A.isindex
ORDER BY SUM(HIT) DESC

--结果

/*
smallclassid smallclassname thumbgallery isindex hit
------------------------------------------------------------------
200 test xxx 1 1100
343 测试 xxx 1 900

*/
xiao_fen_tuan 2008-04-03
  • 打赏
  • 举报
回复
谢谢,但是这个能排序出来,但无法统计显示出hit的总和来..
-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
create table ta(id int, smallclassid int, smallclassname varchar(10), thumbgallery varchar(20),
isindex int, hit int)
insert ta select
1 , 343 , '测试' , 'xxx', 1, 100 union select
2, 343 , '测试' , 'xxx' , 0 , 200 union select
3 , 200 , 'test' , 'xxx' , 1 , 300 union select
4 , 200 , 'test', 'xxx' , 0 , 300


go
select top 6 smallclassid,smallclassname,isindex,thumbgallery
from ta
where isindex = 1
group by smallclassid,smallclassname,isindex,thumbgallery
order by sum(hit) desc,isindex

drop table ta

/*
smallclassid smallclassname isindex thumbgallery
------------ -------------- ----------- --------------------
200 test 1 xxx
343 测试 1 xxx

(所影响的行数为 2 行)
*/
-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
select top 6 smallclasid,smallclassname,isindex,thumbgallery
from ta
where isindex = 1
group by smallclassid ,smallclassname,isindex,thumbgallery
order by sum(hit) desc,isindex

34,838

社区成员

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

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