商品ID 商品名称 点击率
9 魔兽世界(国服) 1
10 魔兽世界(国服) 2
11 魔兽世界(国服) 3
12 魔兽世界(国服) 4
13 魔兽世界(国服) 5
14 魔兽世界(国服) 6
15 魔兽世界(国服) 7
16 梦幻西游 8
17 梦幻西游 9
18 梦幻西游 10
19 梦幻西游 11
20 梦幻西游 12
27 跑跑卡丁车 13
28 跑跑卡丁车 14
29 跑跑卡丁车 15
30 跑跑卡丁车 16
31 跑跑卡丁车 17
32 跑跑卡丁车 18
33 跑跑卡丁车 19
34 地下城与勇士 20
35 地下城与勇士 600
36 地下城与勇士 22
38 地下城与勇士 23
39 地下城与勇士 24
40 地下城与勇士 25
41 永恒之塔 600
42 永恒之塔 600
查询后的数据:
商品ID 商品名称 点击率
15 魔兽世界(国服) 7
33 跑跑卡丁车 19
54 征途 39
65 Q币 50
67 梦幻西游 52
69 天龙八部 54
35 地下城与勇士 600
41 永恒之塔 600
怎么去查询。SQL牛X的帮忙一下了。
------解决方案--------------------
- SQL code
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([商品ID] int,[商品名称] varchar(14),[点击率] int)insert [tb]select 9,'魔兽世界(国服)',1 union allselect 10,'魔兽世界(国服)',2 union allselect 11,'魔兽世界(国服)',3 union allselect 12,'魔兽世界(国服)',4 union allselect 13,'魔兽世界(国服)',5 union allselect 14,'魔兽世界(国服)',6 union allselect 15,'魔兽世界(国服)',7 union allselect 16,'梦幻西游',8 union allselect 17,'梦幻西游',9 union allselect 18,'梦幻西游',10 union allselect 19,'梦幻西游',11 union allselect 20,'梦幻西游',12 union allselect 27,'跑跑卡丁车',13 union allselect 28,'跑跑卡丁车',14 union allselect 29,'跑跑卡丁车',15 union allselect 30,'跑跑卡丁车',16 union allselect 31,'跑跑卡丁车',17 union allselect 32,'跑跑卡丁车',18 union allselect 33,'跑跑卡丁车',19 union allselect 34,'地下城与勇士',20 union allselect 35,'地下城与勇士',600 union allselect 36,'地下城与勇士',22 union allselect 38,'地下城与勇士',23 union allselect 39,'地下城与勇士',24 union allselect 40,'地下城与勇士',25 union allselect 41,'永恒之塔',600 union allselect 42,'永恒之塔',600 -->查询select *from tb t where not exists(select 1 from tb where 商品名称=t.商品名称 and 点击率>t.点击率)order by 3/**商品ID 商品名称 点击率----------- -------------- -----------15 魔兽世界(国服) 720 梦幻西游 1233 跑跑卡丁车 1935 地下城与勇士 60041 永恒之塔 60042 永恒之塔 600(6 行受影响)**/
------解决方案--------------------
create table #tb(商品ID int,商品名称 varchar(20),点击率 int)
insert into #tb
select '9','魔兽世界(国服)','1'
union all select '10','魔兽世界(国服)','2'
union all select '11','魔兽世界(国服)','3'
union all select '12','魔兽世界(国服)','4'
union all select '13','魔兽世界(国服)','5'
union all select '14','魔兽世界(国服)','6'
union all select '15','魔兽世界(国服)','7'
union all select '16','梦幻西游','8'
union all select '17','梦幻西游','9'
union all select '18','梦幻西游','10'
union all select '19','梦幻西游','11'
union all select '20','梦幻西游','12'
union all select '27','跑跑卡丁车','13'
union all select '28','跑跑卡丁车','14'
union all select '29','跑跑卡丁车','15'
union all select '30','跑跑卡丁车','16'
union all select '31','跑跑卡丁车','17'
union all select '32','跑跑卡丁车','18'
union all select '33','跑跑卡丁车','19'
union all select '34','地下城与勇士','20'
union all select '35','地下城与勇士','600'
union all select '36','地下城与勇士','22'
union all select '38','地下城与勇士','23'
union all select '39','地下城与勇士','24'
union all select '40','地下城与勇士','25'
union all select '41','永恒之塔','600'
union all select '42','永恒之塔','600'
select a.*
from #tb a
inner join (select 商品名称,max(点击率) as 点击率 from #tb group by 商品名称)b
on a.商品名称=b.商品名称 and a.点击率=b.点击率
order by 1
商品ID 商品名称 点击率
--------------------
15 魔兽世界(国服) 7
20 梦幻西游 12
33 跑跑卡丁车 19
35 地下城与勇士 600
41 永恒之塔 600
42 永恒之塔 600
------解决方案--------------------
- SQL code