我有一个名为searchdetails的表
id sourcecity destcity viacity distance viewcount
1 Bangalore Chennai Hosur 348 215
2 Delhi Patna Moradabad 1087 13
3 Delhi Manali Chandigarh 553 81
4 Mumbai Kolhapur 377 151
5 Delhi Patna 1082 4
6 Chennai Munnar Dindigul 586 29
7 Mumbai Jaipur 1207 6
8 Chennai Salem 348 144
9 Chennai Salem Vellore 369 7
10 Hyderabad Goa Raichur 655 69
11 Delhi Manali 538 200
12 Chennai Trichy 334 204
13 Delhi Mehandipur Mathura 302 7
14 Jaipur Delhi 273 135
15 Chennai Trichy Viluppuram 334 11
16 Bengaluru Hyderabad 569 49
17 Chenani Trichy Tindivanam 3137 1
18 Bhadrachalam Rajahmundry 178 3
19 Hyderabad Khammam 193 10
20 Bhadrachalam Morbi 1608 1
21 Goa Morbi 1302 1
22 Meerut Pathankot Jalandhar 478 8
23 Bangalore Hyderabad 569 160
24 Hyderabad Morbi 1401 1
现在我想要基于最高viewcount的前5个唯一的destcity名称,其中sourcecity =“Chennai”。
答案应该是:
Trichy 204
Salem 144
Munnar 29
什么是SQL查询?
我试过了:
SELECT DISTINCT destcity, viewcount
FROM searchdetails
WHERE sourcecity = 'Chennai'
ORDER BY viewcount DESC
LIMIT 0, 5
但它不起作用。
答案 0 :(得分:0)
这适用于SQL-Server
SELECT TOP 5 *
FROM (
SELECT destcity, MAX(viewcount ) viewcount
FROM searchdetails
WHERE sourcecity ='Chennai'
GROUP BY destcity
) X
ORDER BY viewcount DESC
thi是for mysql
SELECT *
FROM (
SELECT destcity, MAX(viewcount ) viewcount
FROM searchdetails
WHERE sourcecity ='Chennai'
GROUP BY destcity
) X
ORDER BY viewcount DESC
LIMIT 5
答案 1 :(得分:0)
试试这个(如果是对于mssql):
declare @table table (id int,sourcecity varchar(100),destcity varchar(100),viacity varchar(100),distance int,viewcount int)
insert into @table values (1,'Bangalore','Chennai','Hosur',348,215);
insert into @table values (2,'Delhi','Patna','Moradabad',1087,13);
insert into @table values (3,'Delhi','Manali','Chandigarh',553,81);
insert into @table values (4,'Mumbai','Kolhapur','',377,151);
insert into @table values (5,'Delhi','Patna','',1082,4);
insert into @table values (6,'Chennai','Munnar','Dindigul',586,29);
insert into @table values (7,'Mumbai','Jaipur','',1207,6);
insert into @table values (8,'Chennai','Salem','',348,144);
insert into @table values (9,'Chennai','Salem','Vellore',369,7);
insert into @table values (10,'Hyderabad','Goa','Raichur',655,69);
insert into @table values (11,'Delhi','Manali','',538,200);
insert into @table values (12,'Chennai','Trichy','',334,204);
insert into @table values (13,'Delhi','Mehandipur','Mathura',302,7);
insert into @table values (14,'Jaipur','Delhi','',273,135);
insert into @table values (15,'Chennai','Trichy','Viluppuram',334,11);
insert into @table values (16,'Bengaluru','Hyderabad','',569,49);
insert into @table values (17,'Chenani','Trichy','Tindivanam',3137,1);
insert into @table values (18,'Bhadrachalam','Rajahmundry','',178,3);
insert into @table values (19,'Hyderabad','Khammam','',193,10);
insert into @table values (20,'Bhadrachalam','Morbi','',1608,1);
insert into @table values (21,'Goa','Morbi','',1302,1);
insert into @table values (22,'Meerut','Pathankot','Jalandhar',478,8);
insert into @table values (23,'Bangalore','Hyderabad','',569,160);
insert into @table values (24,'Hyderabad','Morbi','',1401,1);
select top 5 destcity from @table where sourcecity='Chennai' order by viewcount desc