判断是工作日还是周末的SQL语句
答案:2 悬赏:50 手机版
解决时间 2021-03-13 18:24
- 提问者网友:聂風
- 2021-03-13 14:47
判断是工作日还是周末的SQL语句
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-03-13 16:12
方法一:
SELECt d.theDate,
DATEDIFF(DAY, '17530101', d.theDate) % 7 / 5 AS IsWeekend,
1 - DATEDIFF(DAY, '17530101', d.theDate) % 7 / 5 AS IsWeekday
FROM (
SELECt CAST('20081124' AS SMALLDATETIME) AS theDate UNIOn ALL
SELECT '20081125' UNIOn ALL
SELECT '20081126' UNIOn ALL
SELECT '20081127' UNIOn ALL
SELECT '20081128' UNIOn ALL
SELECT '20081129' UNIOn ALL
SELECT '20081130'
) AS d
方法二:
SELECT d.theDate,
CASE WHEN DATEPART(weekday,thedate+@@DATEFIRST -1)<6 THEN 1 ELSE 0 END,
CASE WHEN DATEPART(weekday,thedate+@@DATEFIRST -1)>=6 THEN 1 ELSE 0 END
FROM (
SELECT CAST('20081124' AS SMALLDATETIME) AS theDate UNIOn ALL
SELECT '20081125' UNIOn ALL
SELECT '20081126' UNIOn ALL
SELECT '20081127' UNIOn ALL
SELECT '20081128' UNIOn ALL
SELECT '20081129' UNIOn ALL
SELECT '20081130'
) AS d
SELECt d.theDate,
DATEDIFF(DAY, '17530101', d.theDate) % 7 / 5 AS IsWeekend,
1 - DATEDIFF(DAY, '17530101', d.theDate) % 7 / 5 AS IsWeekday
FROM (
SELECt CAST('20081124' AS SMALLDATETIME) AS theDate UNIOn ALL
SELECT '20081125' UNIOn ALL
SELECT '20081126' UNIOn ALL
SELECT '20081127' UNIOn ALL
SELECT '20081128' UNIOn ALL
SELECT '20081129' UNIOn ALL
SELECT '20081130'
) AS d
方法二:
SELECT d.theDate,
CASE WHEN DATEPART(weekday,thedate+@@DATEFIRST -1)<6 THEN 1 ELSE 0 END,
CASE WHEN DATEPART(weekday,thedate+@@DATEFIRST -1)>=6 THEN 1 ELSE 0 END
FROM (
SELECT CAST('20081124' AS SMALLDATETIME) AS theDate UNIOn ALL
SELECT '20081125' UNIOn ALL
SELECT '20081126' UNIOn ALL
SELECT '20081127' UNIOn ALL
SELECT '20081128' UNIOn ALL
SELECT '20081129' UNIOn ALL
SELECT '20081130'
) AS d
全部回答
- 1楼网友:愁杀梦里人
- 2021-03-13 17:31
select count(id),sum(case when day='星期一' or day='星期二' or day='星期三' or day='星期四' or day='星期五' then 1 else 0 end) 工作日,sum(case when day='星期六' or day='星期日' then 1 else 0 end) 周末 from table
另外:我发现你的表中有重复值啊,这个怎么算,不管么?还是要去重?这张表(我说的是例子)似乎有点问题。
还应该由其他写法,暂时就想起这一种来。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯