我有一个查询'page_views'表的查询。到目前为止,我有能力获得白天的总观看次数。但是,我在白天获得独特观点时遇到了问题。
我正在测试的列是user_id ...这是我对总视图的查询。
SELECT
site_id,
CONCAT( month(created_at)
, '-'
, day(created_at)
, '-'
, year(created_at)
) as created_at_date,
COUNT(*) as total_results
FROM
page_views
GROUP BY day(created_at)
我无法弄清楚根据user_id列获取基于日独特的总体唯一视图的逻辑。
Schema:
id:int
user_id:int
site_id:int
request_uri:varchar
referer:varchar
user_agent:varchar
created_at:datetime
答案 0 :(得分:2)
首先,您的原始查询不正确。 (它将在不同月份的同一天或不同的站点返回无效结果)应如下所示
SELECT
site_id,
CONCAT(month(created_at) , '-', day(created_at), '-' , year(created_at))
as created_at_date,
COUNT(*) as total_results
FROM
page_views
GROUP BY
site_id,
CONCAT(month(created_at) , '-', day(created_at), '-' , year(created_at))
对于原始问题,下面的查询应该有效:
SELECT
site_id,
CONCAT(month(created_at) , '-', day(created_at), '-' , year(created_at))
as created_at_date,
COUNT(distinct user_id) as unique_users
FROM
page_views
GROUP BY
site_id,
CONCAT(month(created_at) , '-', day(created_at), '-' , year(created_at))
答案 1 :(得分:0)
建议您使用DATE_FORMAT函数而不是CONCAT:
SELECT
site_id,
DATE_FORMAT( created_at, '%m-%d-%Y' ) AS created_at_date,
COUNT( DISTINCT user_id ) AS unique_users
FROM
page_views
GROUP BY
site_id,
DATE_FORMAT( created_at, '%m-%d-%Y' )
请注意(如xycf7所述)按季度发布的查询组,最多可显示31个组 - 这可能不是您的意图。