如何在知道当天的一个月内获得前一个星期。
例如,如果我有这样的表:
emp_num trans_date
22 1-10-2015
22 5-10-2015
22 7-10-2015
22 11-10-2015
22 14-10-2015
22 19-10-2015
22 27-10-2015
现在每个月都有4个星期。
所以我的约会时间为11-10-2015
(第二周)
我希望在同一个月内获得前几周的结果。
所以在这个例子中我想要10月份第一周的结果如下:
emp_num trans_date
22 1-10-2015
22 5-10-2015
22 7-10-2015
注意:
The start day of the week is: sat The end day of the week is:Fri
答案 0 :(得分:1)
如果我理解您的问题,2015年10月,您会预料到以下结果:
RunDate Weekday Result
01-10-2015 Thurs [nothing]
02-10-2015 Fri [nothing]
03-10-2015 Sat 01-10-2015, 02-10-2015
04-10-2015 Sun 01-10-2015, 02-10-2015 (same as above)
...
09-10-2015 Fri 01-10-2015, 02-10-2015 (same as above)
10-10-2015 Sat 01-10-2015, ... , 09-10-2015
...
16-10-2015 Fri 01-10-2015, ... , 09-10-2015 (same as above)
17-10-2015 Sat 01-10-2015, ... , 16-10-2015
20-10-2015 Tue 01-10-2015, ... , 16-10-2015 (same as above)
30-10-2015 Fri 01-10-2015, ... , 23-10-2015
31-10-2015 Sat 01-10-2015, ... , 30-10-2015
如果是这种情况,则以下查询将执行您所需的操作:
SELECT trans_date, emp_num, COUNT(1)
FROM transaction_table
WHERE MONTH(trans_date) = MONTH(TODAY)
AND YEAR(trans_date) = YEAR(TODAY)
AND trans_date <= DECODE(WEEKDAY(TODAY),
6, TODAY -1,
TODAY - WEEKDAY(TODAY) -2)
GROUP BY 1, 2
ORDER BY 1, 2
DECODE()
中的逻辑是管理WEEKDAY()
默认行为的偏移量,其中Sun =&gt; 0和Sat =&gt; 6.上述查询中的关键字TODAY
可以替换为变量或任意单个日期。
答案 1 :(得分:0)
我还没有在informix中工作,但这是SQL中的样子:
DECLARE @SomeDate DATE
SET @SomeDate = '2015-10-11'
DECLARE @DayOfWeek INT
SELECT @DayOfWeek = ((DATEPART(dw, @SomeDate) ) % 7)
DECLARE @WeekStart DATE
SELECT @WeekStart = DATEADD(d, -@DayOfWeek, @SomeDate)
SELECT * FROM SomeTable
WHERE MONTH(trans_date) = MONTH(@SomeDate) AND trans_date < @WeekStart