给定日期计算同一周的工作日日期

时间:2015-12-03 22:38:48

标签: sql sqlite sql-date-functions

我有以下sql代码我已经一起散列,以便从我的时间表数据中获取我想要的输出。不确定它是以这种方式检索数据的最有效方法,但似乎有效。我发现的唯一问题是某些记录上显示的日期是一周。因此,如果时间表日期是2015-12-02,当我计算那周的星期一的日期时,我需要减去'-7天'作为其提出下一个星期一的日期。我想用变量做这个,但读取sqlite不支持变量。任何人都建议我采取最佳方法。

我附上我在下面的代码

SELECT 'Mon' , DATE(t.date, 'weekday 1') AS myDate , tl.projectID , t.staffID , tl.mon AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.mon NOT Null) AND (tl.mon <> '')

UNION ALL

SELECT 'Tues' , DATE(t.date, 'weekday 2') AS myDate , tl.projectID , t.staffID , tl.tues AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.tues NOT Null) AND (tl.tues <> '')

UNION ALL

SELECT 'Wed' , DATE(t.date, 'weekday 3') AS myDate , tl.projectID , t.staffID , tl.wed AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.wed NOT Null) AND (tl.wed <> '')

UNION ALL

SELECT 'Thurs' , DATE(t.date, 'weekday 4') AS myDate , tl.projectID , t.staffID , tl.thurs AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.thurs NOT Null) AND (tl.thurs <> '')

UNION ALL

SELECT 'Fri' , DATE(t.date, 'weekday 5') AS myDate , tl.projectID , t.staffID , tl.fri AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.fri NOT Null) AND (tl.fri <> '')

UNION ALL

SELECT 'Sat' , DATE(t.date, 'weekday 6') AS myDate , tl.projectID , t.staffID , tl.sat AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.sat NOT Null) AND (tl.sat <> '')

UNION ALL

SELECT 'Sun' , DATE(t.date, 'weekday 0') AS myDate , tl.projectID , t.staffID , tl.sun AS hours, t.date
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01') AND (tl.sun NOT Null) AND (tl.sun <> '')

ORDER BY myDate ASC

1 个答案:

答案 0 :(得分:0)

一点点背景。问题是'weekday N'修饰符会为您提供下一个日期,即星期几== N。因此,如果您提供Date函数的日期是星期一并且您要求'weekday 0'(星期日),它将为您提供即将到来的星期日,但如果您问'weekday 2'(星期二),那么因为那个日期还没有过去,所以本周的星期二会到来。

所以,如果你知道timeheet.date上一周的日期总是星期一或其他什么,你可以硬编码调整以回到一周的开头:
Date(t.date, '-1 day', 'weekday 1')

但如果您不想做出这样的假设,可以使用strftime计算每条记录的调整结果:
Date(t.date, strftime('-%w days',t.date), 'weekday 1')