SQL:Datepart不返回7个工作日中的1个的结果

时间:2017-11-09 18:30:49

标签: sql postgresql

我在7个工作日的第7天(星期六)让我的日期部分工作时遇到了麻烦。它将在周一至周五返回正确的结果。有什么建议?

SELECT 
        u.username AS poweruploader,
        u.profile_uploads_count AS uploads,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 1 THEN 1 ELSE NULL END) AS sunday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 2 THEN 1 ELSE NULL END) AS monday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 3 THEN 1 ELSE NULL END) AS tuesday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 4 THEN 1 ELSE NULL END) AS wednesday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 5 THEN 1 ELSE NULL END) AS thursday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 6 THEN 1 ELSE NULL END) AS friday,
        COUNT (CASE WHEN datepart(weekday,i.submission::date) = 7 THEN 1 ELSE NULL END) AS saturday
    FROM application.users AS u
    JOIN application.image AS i ON u.username = i.username
    WHERE profile_uploads_count >= '5'
    GROUP BY 1,2

1 个答案:

答案 0 :(得分:3)

如果您使用的是Postgres,则函数为date_part()。它返回0到6范围内的值,其中0表示星期日,6表示星期六。