日期:2014-05-18  浏览次数:20589 次

统计排行榜的问题


giftname为礼物名,giftcount为数量
要求:查出同一礼物名下,giftcount数量最多的userid及礼物名、数量,如数量相同则取最早的时间(addtime)

结果:
giftname userid giftcount
香水 3 2
金条 2 1
.。。。。
。。。。
。。。


------解决方案--------------------
SQL code

select giftname,userid,sum(giftcount) as giftcount
from table1 
group by giftname,userid
order by sum(giftcount) desc,min(addtime)

------解决方案--------------------
SQL code

--try
select giftname,userid,giftcount from tb a where not exists(
select 1 from tb where giftname=a.giftname and giftcount>a.giftcount or (giftcount=a.giftcount and addtime<a.addtime)
)

------解决方案--------------------
你确定吗?确定吗?确定吗?确定吗?确定吗?确定吗?确定吗?
确定吗?确定吗?确定吗?确...定...吗...?
探讨
引用:

SQL code

--try
select giftname,userid,giftcount from tb a where not exists(
select 1 from tb where giftname=a.giftname and giftcount>a.giftcount or (giftcount=a.giftcount……

------解决方案--------------------
SQL code
select
 giftname,userid,giftcount 
from
 tb t 
where
 not exists(select 1 from tb where giftname=t.giftname and giftcount>t.giftcount or(giftcount=t.giftcount and addtime<t.addtime))

------解决方案--------------------
没道理啊!你放10行或者20行数据上来,复制上来,别上图
探讨
引用:

你确定吗?确定吗?确定吗?确定吗?确定吗?确定吗?确定吗?
确定吗?确定吗?确定吗?确...定...吗...?引用:
引用:

SQL code

--try
select giftname,userid,giftcount from tb a where not e……

------解决方案--------------------
探讨

引用:

SQL code

select giftname,userid,sum(giftcount) as giftcount
from table1
group by giftname,userid
order by sum(giftcount) desc,min(addtime)


统计出来的礼物有重复

------解决方案--------------------
這樣?

SQL code
use Tempdb
go
--> --> 
 
if not object_id(N'Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[giftname] nvarchar(3),[userid] int,[giftcount] int,[addtime] Datetime,[sendid] nvarchar(5),[price] int,[price2] nvarchar(8),[aaa] nvarchar(9))
Insert #T
select 68,N'香水',3,1,'2011-10-19 16:25:05.483',N'1 100',100,null,null union all
select 69,N'香水',3,1,'2011-10-19 16:25:42.547',N'1 100',100,null,null union all
select 70,N'金条',2,1,'2011-10-21 10:01:29.733',N'1 10',10,N'car',null union all
select 71,N'雪茄',2,1,'2011-10-21 10:01:48.390',N'1 100',100,null,null union all
select 72,N'飞机',1,1,'2011-10-21 10:10:08.110',N'2',100,N'100',null union all
select 73,N'猪头',1,1,'2011-10-21 10:11:11.093',N'2',100,N'100',N'washboard' union all
select 74,N'巧克力',1,1,'2011-10-21 10:11:53.437',N'1 10',10,null,null union all
select 75,N'口红',2,1,'2011-10-21 10:14:39.813',N'1 100',100,N'yacht',null union all
select 76,N'棒棒粮',2,1,'2011-10-21 10:15:22.627',N'1 10',10,N'rose',null union all
select 77,N'奥迪',4,1,'2011-10-21 10:16:38.797',N'1 100',100,null,null union all
select 78,N'航母',4,1,'2011-10-21 10:16:51.030',N'1 100',100,null,null union all
select 79,N'爱你',4,1,'2011-10-21 10:17:17.767',N'1 10',10,N'lollipop',null union all
select 80,N'XO',5,1,'2011-10-21 10:18:06.360',N'1 100',100,N'perfume',null union all
select 81,N'谢谢',5,1,'2011-10-21 10:19:44.407',N'2',100,N'100',N'chocolate' union all
select 82,N'飞机',2,1,'2011-10-21 10:20:18.187',N'2',100,N'100',null union all
select 83,N'包',2,1,'2011-10-21 10:21:07.110',N'3',100,N'100',N'xo' union all
select 84,N'砖',5,1,'2011-10-21 10:21:27.187',N'3',100,N'100',N'yiqunrenC' union all
select 85,N'鼓掌',5,1,'2011-10-21 10:21:45.313',N'3',10,N'10',N'cake' union all
select 86,N'爱你',2,1,'2011-10-21 10