我花了相当多的时间试图了解如何做到这一点,我不能。我为自己做了很多复杂的事情,我理解代码,而不是它是如何一起流动的。
如果我的表"Customers"
包含"customer_id"
,"store_id"
,"visited"
和"date"
的列,我想识别访问过的客户({{ 1}})设定日期visited = yes
上的特定商店(store_id="NEA"
) - 然后从那时起返回到同一商店,并计算已退回的客户数量 - 任何人都可以帮助我?
我知道我需要为"2015-05-14"
customer_id
,日期为“2015-05-14”且访问次数为“是”的人选择store_id
,但是,我如何识别那些返回的人,并计算他们 - 那么当天访问了多少客户然后返回?
例如:
"NEA"
答案 0 :(得分:0)
SELECT DISTINCT customer_id, date
FROM Customers
WHERE visited = 'yes'
GROUP BY customer_id, store_id, date
HAVING COUNT(*) >= 2
请按照以下链接查看正在运行的演示:
上述查询会生成重复客户列表以及他们访问同一商店两次或更多次的日期。如果您希望按日期计算重复客户数,可以将其包装和子查询:
SELECT t.date, COUNT(*) AS duplicateCount
FROM
(
SELECT DISTINCT customer_id, date
FROM Customers
WHERE visited = 'yes'
GROUP BY customer_id, store_id, date
HAVING COUNT(*) >= 2
) t
GROUP BY t.date
<强>更新强>
根据您的反馈,您可能会想到以下查询:
SELECT DISTINCT customer_id
FROM Customers
WHERE visited = 'yes'
GROUP BY customer_id, store_id
HAVING SUM(CASE WHEN date = '2015-05-14' THEN 1 ELSE 0 END) >= 1 AND
SUM(CASE WHEN date > '2015-05-14' THEN 1 ELSE 0 END) >= 1
答案 1 :(得分:0)
或类似的东西:
SELECT count(*) AS cnt,t.*
FROM yourTable AS t
WHERE
`date` = '2015-05-14'
AND
store_id = 'NEA'
AND
visited = 'YES'
GROUP BY customer_id
HAVING cnt >1;