简单的SQL问题,我搜索但似乎找不到已经问过的答案。
我想查询名称,然后查询每个名称的唯一网址数。
输入:
NAME URL
John google.com
John google.com
John youtube.com
Bob youtube.com
Bob twitter.com
期望的输出:
NAME URL_count
John 2
Bob 2
我试过了:
SELECT DISTINCT(name), COUNT(URL) as URL_count
FROM database_table
WHERE date='2013-06-12'
似乎没有产生我需要的结果!帮助
答案 0 :(得分:4)
由于您希望为每个用户计算不同的urls
,因此正确的语法将使用count(distinct url)
并按name
对数据进行分组:
select name, count(distinct url) as URL_count
from yourtable
where date='2013-06-12'
group by name;
答案 1 :(得分:1)
结合使用该组:
SELECT name, COUNT(DISTINCT URL) as URL_count
FROM database_table
WHERE date='2013-06-12'
GROUP BY NAME
答案 2 :(得分:1)
你可以尝试:
SELECT name, COUNT(DISTINCT URL) as URL_count
FROM database_table
WHERE date='2013-06-12'
GROUP BY name