giftname为礼物名,giftcount为数量
要求:查出同一礼物名下,giftcount数量最多的userid及礼物名、数量,如数量相同则取最早的时间(addtime)结果:
giftname userid giftcount
香水 3 2
金条 2 1
.。
。
要求:查出同一礼物名下,giftcount数量最多的userid及礼物名、数量,如数量相同则取最早的时间(addtime)结果:
giftname userid giftcount
香水 3 2
金条 2 1
.。
。
select giftname,userid,sum(giftcount) as giftcount
from table1
group by giftname,userid
order by sum(giftcount) desc,min(addtime)
--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)
)
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))
SELECT
giftname,userid,giftcount
FROM
(select
giftname,
userid,
sum(giftcount) as giftcount,
min(addtime) AS addtime
row=ROW_NUMBER()OVER(partition BY giftname ORDER BY giftcount DESC,addtime)
from table1
group by giftname,userid
)T
WHERE row=1
68 香水 3 1 2011-10-19 16:25:05.483 1 100 100
69 香水 3 1 2011-10-19 16:25:42.547 1 100 100
70 金条 2 1 2011-10-21 10:01:29.733 1 10 10 car
71 雪茄 2 1 2011-10-21 10:01:48.390 1 100 100
72 飞机 1 1 2011-10-21 10:10:08.110 2 100 100
73 猪头 1 1 2011-10-21 10:11:11.093 2 100 100 washboard
74 巧克力 1 1 2011-10-21 10:11:53.437 1 10 10
75 口红 2 1 2011-10-21 10:14:39.813 1 100 100 yacht
76 棒棒粮 2 1 2011-10-21 10:15:22.627 1 10 10 rose
77 奥迪 4 1 2011-10-21 10:16:38.797 1 100 100
78 航母 4 1 2011-10-21 10:16:51.030 1 100 100
79 爱你 4 1 2011-10-21 10:17:17.767 1 10 10 lollipop
80 XO 5 1 2011-10-21 10:18:06.360 1 100 100 perfume
81 谢谢 5 1 2011-10-21 10:19:44.407 2 100 100 chocolate
82 飞机 2 1 2011-10-21 10:20:18.187 2 100 100
83 包 2 1 2011-10-21 10:21:07.110 3 100 100 xo
84 砖 5 1 2011-10-21 10:21:27.187 3 100 100 yiqunrenC
85 鼓掌 5 1 2011-10-21 10:21:45.313 3 10 10 cake
86 爱你 2 1 2011-10-21 10:22:12.327 3 10 10 lollipop
87 飞吻 5 1 2011-10-21 10:22:14.360 3 10 10 zhuantou
88 爱你 5 1 2011-10-21 10:22:37.047 3 10 10 lollipop
89 飞吻 2 1 2011-10-21 10:22:50.673 3 10 10 zhuantou
90 航母 9 1 2011-10-21 10:28:25.593 3 100 100
91 奥迪 9 1 2011-10-21 10:28:35.280 3 100 100
92 猪头 9 1 2011-10-21 10:28:46.687 3 100 100 washboard
93 包 9 1 2011-10-21 10:28:53.203 3 100 100 xo
94 巧克力 9 1 2011-10-21 10:29:00.657 3 10 10
95 雪茄 9 1 2011-10-21 10:29:11.327 3 100 100
96 航母 6 1 2011-10-21 10:30:01.530 3 100 100
97 棒棒粮 6 1 2011-10-21 10:30:16.593 3 10 10 rose
98 香水 9 1 2011-10-21 10:30:42.280 3 100 100
99 猪头 9 1 2011-10-21 10:31:59.673 3 100 100 washboard===================
可能是我没表达清楚,
我要查出同一个礼物(礼物不能重复也不能少,上面数据中的礼物名都要在结果中出现)中userid名下giftcount (sum(giftcount)) 最多的记录比如 香水这个,有这记录的userid有 3和9,而3的giftcount为2,9的giftcount为1,所以就得出
giftname userid giftcount
香水 3 2
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:22:12.327',N'3',10,N'10',N'lollipop' union all
select 87,N'飞吻',5,1,'2011-10-21 10:22:14.360',N'3',10,N'10',N'zhuantou' union all
select 88,N'爱你',5,1,'2011-10-21 10:22:37.047',N'3',10,N'10',N'lollipop' union all
select 89,N'飞吻',2,1,'2011-10-21 10:22:50.673',N'3',10,N'10',N'zhuantou' union all
select 90,N'航母',9,1,'2011-10-21 10:28:25.593',N'3',100,N'100',null union all
select 91,N'奥迪',9,1,'2011-10-21 10:28:35.280',N'3',100,N'100',null union all
select 92,N'猪头',9,1,'2011-10-21 10:28:46.687',N'3',100,N'100',N'washboard' union all
select 93,N'包',9,1,'2011-10-21 10:28:53.203',N'3',100,N'100',N'xo' union all
select 94,N'巧克力',9,1,'2011-10-21 10:29:00.657',N'3',10,N'10',null union all
select 95,N'雪茄',9,1,'2011-10-21 10:29:11.327',N'3',100,N'100',null union all
select 96,N'航母',6,1,'2011-10-21 10:30:01.530',N'3',100,N'100',null union all
select 97,N'棒棒粮',6,1,'2011-10-21 10:30:16.593',N'3',10,N'10',N'rose' union all
select 98,N'香水',9,1,'2011-10-21 10:30:42.280',N'3',100,N'100',null union all
select 99,N'猪头',9,1,'2011-10-21 10:31:59.673',N'3',100,N'100',N'washboard'
Go
SELECT giftname,userid,giftcount
FROM
(select giftname, userid, sum(giftcount) as giftcount, min(addtime) AS addtime,
row=ROW_NUMBER()OVER(partition BY giftname ORDER BY sum(giftcount) DESC, min(addtime))
from #T group by giftname,userid )T
WHERE row=1
ORDER BY giftcount DESC/*
giftname userid giftcount
香水 3 2
猪头 9 2
砖 5 1
谢谢 5 1
飞机 1 1
飞吻 5 1
航母 4 1
雪茄 2 1
棒棒粮 2 1
鼓掌 5 1
奥迪 4 1
爱你 4 1
XO 5 1
口红 2 1
包 2 1
巧克力 1 1
金条 2 1
*/
厉害,貌似是这样的结果
--我咋觉得你的需求是这个
select giftname,userid,giftcount from (
select rid=ROW_NUMBER() OVER (partition BY giftname ORDER BY giftcount desc,addtime),*
from tb ) a
where rid=1