PHP/SQL -如何计算每个月的星期六?



我有两个MySQL表,名为" accounts"one_answers"events" .

<账户/strong>

<表类="年代桌子">ID名称tbody><1皮特2Josh3哈利

基本上可以使用DAYOFWEEK函数和GROUP BY MONTH

SELECT 
account_id,
MONTH(date), 
COUNT(*)
FROM Events 
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date);

SQL Fiddle here

以及何时可以在接收到的表上使用PIVOT:

WITH res AS (
SELECT 
account_id,
MONTH(date) mnth, 
COUNT(*) cnt
FROM Events 
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date)
) SELECT 
account_id,
name,
SUM(mnth=1) Januar,
-- 
SUM(mnth=9) September,
SUM(mnth=10) October,
SUM(mnth=11) November,
SUM(mnth=12) December
FROM res
JOIN Accounts ON Accounts.id = account_id
GROUP BY account_id, name;

SQL Pivot Fiddle

相关内容

  • 没有找到相关文章

最新更新