我的数据库中有以下表格
----------------------
id | keyword | location
----------------------
1 | Test-New | 1
2 | Test | 1
3 | Test | 1
4 | Test | 1
----------------------
我需要根据位置获得结果,所以我使用分组
select keyword, location ,count(*) as count from table_name group by location
现在我得到以下结果
----------------------------
keyword | Location | count
---------------------------
Test-New | 1 | 4
---------------------------
但我希望每个位置的最热门关键字和每个位置的数量。在这个例子中,我需要测试而不是测试新用于位置 1 (因为在实际的表中,测试出现了3次并且Test-New出现了1次)。
所以我需要这样的结果
--------------------------
keyword | Location | count
--------------------------
Test | 1 | 4
--------------------------
答案 0 :(得分:0)
您可以尝试此查询:
select keyword ,count(keyword) as TotalKeyword from table_name group by location
答案 1 :(得分:0)
SELECT a.*
FROM
( SELECT keyword
, location
, COUNT(*) cnt
FROM my_table
GROUP
BY keyword
, location
) a
JOIN
( SELECT keyword
, MAX(cnt) max_cnt
FROM
( SELECT keyword
, location
, COUNT(*) cnt
FROM my_table
GROUP
BY keyword
, location
) x
GROUP
BY keyword
) b
ON b.keyword = a.keyword
AND b.max_cnt = a.cnt;
答案 2 :(得分:0)
这是正常工作......享受RaGu bhai
SELECT
temp.keyword,temp.location,SUM(temp.TotalLocation) AS COUNT
FROM (SELECT
keyword,location,
COUNT(location) AS TotalLocation
FROM
`test`
GROUP BY location,keyword ORDER BY TotalLocation DESC)
temp GROUP BY location;
答案 3 :(得分:0)
你的问题有点混乱,你要问这样的事吗?
select *,
count(location) as location_count
from test1 group by location, keyword