输入
+-------+---------+----------+
| Name | Country | State |
+-------+---------+----------+
| jon | US | Chicago |
+-------+---------+----------+
| kathy | US | CA |
+-------+---------+----------+
| linda | US | LA |
+-------+---------+----------+
| harry | UK | wales |
+-------+---------+----------+
| steve | UK | scotland |
+-------+---------+----------+
| ram | India | ap |
+-------+---------+----------+
| jai | India | ka |
+-------+---------+----------+
| raju | India | ap |
+-------+---------+----------+
| ravi | India | mp |
+-------+---------+----------+
因为它返回2(country& count)值,因此无法使用以下查询通过具有最高计数(印度)的国家/地区。请纠正我。
select name from table a
where a.country in
(SELECT country, COUNT(*) as c FROM table b
GROUP BY country
ORDER BY c DESC LIMIT 1);
输出:
应显示其数量在同一张表中最高的州(在最高统计的国家 - 印度之下)的详细信息。
+---------+-------+----+
| ram | india | ap |
+---------+-------+----+
| raju | india | ap |
+---------+-------+----+
答案 0 :(得分:1)
试试这个
select * from table e
where e.state in
(select d.state from table a
where a.country in
(SELECT country FROM table b
GROUP BY country
ORDER BY count(*) DESC LIMIT 1);
GROUP BY state
ORDER BY count(*) DESC LIMIT 1);
答案 1 :(得分:0)
SELECT country,COUNT(*)as c FROM table b GROUP BY国家 ORDER BY c DESC LIMIT 1;
更新:这应该有效
select state,count(*) c2 from a t1
INNER JOIN (
SELECT country, COUNT(*) as c FROM a t1 GROUP BY country ORDER BY c DESC LIMIT 1) t2 on t1.country = t2.country
GROUP BY state ORDER BY c2 DESC LIMIT 1
答案 2 :(得分:0)
SELECT c.name,c.country,c.state,
FROM table c
INNER JOIN (SELECT a.country,a.state,count(*) statecount
FROM table a
INNER JOIN (SELECT b.country, COUNT(*) as countrycount
FROM table b
GROUP BY b.country
ORDER BY countrycount DESC LIMIT 1) b ON a.country = b.country
GROUP BY a.country,a.state
ORDER BY a.statecount DESC LIMIT 1) d ON c.country = d.country
AND c.state = d.state
答案 3 :(得分:0)
我在sql server中尝试过,该语句对我有用:
select * from [table] a
where a.country in
(SELECT top 1 country FROM [table] b
GROUP BY country
ORDER BY COUNT(*) DESC)
我猜你使用Postgre或somthing similer,所以试试这个:
select * from [table] a
where a.country in
(SELECT country FROM [table] b
GROUP BY country
ORDER BY COUNT(*) DESC LIMIT 1)
如果您告诉我SQL语言类型,我将能够为您提供确切的查询
答案 4 :(得分:-1)
这是mssql Query你可以根据你的转换。
select top 1 unitid as n, count (*) as co from npcstatus where unitid is not null Group by unitid having count (*)>1 order by co desc
此查询将为您提供最多出现的unitid。
您可以随意修改。