如何从两个月前的第一天到昨天的日期表? [英] How can I get a table of dates from the first day of the month, two months ago, to yesterday?

查看:111
本文介绍了如何从两个月前的第一天到昨天的日期表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,通常可以通过创建一个馈线表(例如,在五年前和将来的一百年之间的每个日期)进行查询,但不幸的是,这个特定的工作不允许创建这样一个表。



所以我打开这个SO社区。今天是2010年1月29日。我可以运行什么查询,可以给出一个单个日期列的表,值从2009年11月1日到2010年1月28日包含在内? 2月1日,我应该从2009年12月1日到2010年1月31日的每个日期。



我正在使用DB2,但我很高兴看到任何其他我们知道我可以从 CURRENT DATE 中选择



c $ c> sysibm.sysdummy1 (或$ code> dual for Oracle bods),但我不知道如何立即选择没有物理的日期范围支持表。

解决方案

这只是在两个日期之间做连续的日子,但是我已经发布显示可以消除递归错误通过提供限制。

  with temp(level,seqdate)as 
(select 1,date 01-01')
从sysibm.sysdummy1
联合全部
选择级别,seqdate +级别天
从临时
其中级别< 1000
和seqdate + 1天<日期('2008-02-01')

从temp
中选择seqdate作为CalendarDay
order by seqdate






从pax更新:



这个答案实际上把我放在正确的轨道上。您可以通过引入受常数限制的变量来摆脱警告。上面的查询没有它相当正确(和日期错误,我会原谅),但是,由于它指出了问题的解决方案,它赢得了奖品。



以下代码是最终的工作版本(无警告):

  WITH DATERANGE (LEVEL,DT)AS(
SELECT 1,CURRENT DATE +(1 - DAY(CURRENT DATE))DAYS - 2 MONTHS
FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT LEVEL + 1,DT + 1 DAY
FROM DATERANGE
WHERE LEVEL< 1000 AND DT< CURRENT DATE - 1天$ ​​b $ b)从DATERANGE中选择DT;

当在2月2日的 nd 上运行时, p>

  ---------- 
DT
--------- -
2009-12-01
2009-12-02
2009-12-03
:::
2010-01-30
2010 -01-31
2010-02-01

DSNE610I显示的行数为63
DSNE616I声明执行成功。


I have a situation that I would normally solve by creating a feeder table (for example, every date between five years ago and a hundred years into the future) for querying but, unfortunately, this particular job disallows creation of such a table.

So I'm opening this up to the SO community. Today is Jan 29, 2010. What query could I run that would give a table with a single date column with values ranging from Nov 1, 2009 through Jan 28, 2010 inclusive? On Feb 1, it should give me every date from Dec 1, 2009 through Jan 31, 2010.

I'm using DB2 but I'm happy to see any other solutions on the off-chance they may provide a clue.

I know I can select CURRENT DATE from sysibm.sysdummy1 (or dual for Oracle bods) but I'm not sure how to immediately select a date range without a physical backing table.

解决方案

This just does sequential days between two dates, but I've posted to show you can eliminate the recursive error by supplying a limit.

with temp (level, seqdate) as 
(select 1, date('2008-01-01')
from sysibm.sysdummy1
union all
select level, seqdate + level days
from temp
where level < 1000 
and seqdate + 1 days < Date('2008-02-01')
)
select seqdate as CalendarDay
from temp
order by seqdate


Update from pax:

This answer actually put me on the right track. You can get rid of the warning by introducing a variable that's limited by a constant. The query above didn't have it quite right (and got the dates wrong, which I'll forgive) but, since it pointed me to the problem solution, it wins the prize.

The code below was the final working version (sans warning):

WITH DATERANGE(LEVEL,DT) AS (
  SELECT 1, CURRENT DATE + (1 - DAY(CURRENT DATE)) DAYS - 2 MONTHS
    FROM SYSIBM.SYSDUMMY1
  UNION ALL SELECT LEVEL + 1, DT + 1 DAY
    FROM DATERANGE
    WHERE LEVEL < 1000 AND DT < CURRENT DATE - 1 DAY
) SELECT DT FROM DATERANGE;

which outputs, when run on the 2nd of February:

----------
    DT
----------
2009-12-01
2009-12-02
2009-12-03
:  :  :  :
2010-01-30
2010-01-31
2010-02-01

DSNE610I NUMBER OF ROWS DISPLAYED IS 63
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL.

这篇关于如何从两个月前的第一天到昨天的日期表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆