generate days from date range

后端 未结 29 2405
渐次进展
渐次进展 2020-11-21 05:19

I would like to run a query like

select ... as days where `date` is between \'2010-01-20\' and \'2010-01-24\'

And return data like:

相关标签:
29条回答
  • 2020-11-21 05:53

    try this.

    SELECT TO_DATE('20160210','yyyymmdd') - 1 + LEVEL AS start_day 
    from DUAL
    connect by level <= (TO_DATE('20160228','yyyymmdd') + 1) - TO_DATE('20160210','yyyymmdd') ;
    
    0 讨论(0)
  • 2020-11-21 05:54

    if you will ever need more then a couple days, you need a table.

    Create a date range in mysql

    then,

    select from days.day, count(mytable.field) as fields from days left join mytable on day=date where date between x and y;
    
    0 讨论(0)
  • 2020-11-21 05:55

    MSSQL Query

    select datetable.Date 
    from (
        select DATEADD(day,-(a.a + (10 * b.a) + (100 * c.a)),getdate()) AS Date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    ) datetable
    where datetable.Date between '2014-01-20' and '2014-01-24' 
    order by datetable.Date DESC
    

    Output

    Date
    -----
    2014-01-23 12:35:25.250
    2014-01-22 12:35:25.250
    2014-01-21 12:35:25.250
    2014-01-20 12:35:25.250
    
    0 讨论(0)
  • 2020-11-21 05:57

    For Access 2010 - multiple steps required; I followed the same pattern as posted above, but thought I could help someone in Access. Worked great for me, I didn't have to keep a seeded table of dates.

    Create a table called DUAL (similar to how the Oracle DUAL table works)

    • ID (AutoNumber)
    • DummyColumn (Text)
    • Add one row values (1,"DummyRow")

    Create a query named "ZeroThru9Q"; manually enter the following syntax:

    SELECT 0 AS a
    FROM dual
    UNION ALL
    SELECT 1
    FROM dual
    UNION ALL
    SELECT 2
    FROM dual
    UNION ALL
    SELECT 3
    FROM dual
    UNION ALL
    SELECT 4
    FROM dual
    UNION ALL
    SELECT 5
    FROM dual
    UNION ALL
    SELECT 6
    FROM dual
    UNION ALL
    SELECT 7
    FROM dual
    UNION ALL
    SELECT 8
    FROM dual
    UNION ALL
    SELECT 9
    FROM dual;
    

    Create a query named "TodayMinus1KQ" (for dates before today); manually enter the following syntax:

    SELECT date() - (a.a + (10 * b.a) + (100 * c.a)) AS MyDate
    FROM
      (SELECT *
       FROM ZeroThru9Q) AS a,
    
      (SELECT *
       FROM ZeroThru9Q) AS b,
    
      (SELECT *
       FROM ZeroThru9Q) AS c
    

    Create a query named "TodayPlus1KQ" (for dates after today); manually enter the following syntax:

    SELECT date() + (a.a + (10 * b.a) + (100 * c.a)) AS MyDate
    FROM
      (SELECT *
       FROM ZeroThru9Q) AS a,
    
      (SELECT *
       FROM ZeroThru9Q) AS b,
    
      (SELECT *
       FROM ZeroThru9Q) AS c;
    

    Create a union query named "TodayPlusMinus1KQ" (for dates +/- 1000 days):

    SELECT MyDate
    FROM TodayMinus1KQ
    UNION
    SELECT MyDate
    FROM TodayPlus1KQ;
    

    Now you can use the query:

    SELECT MyDate
    FROM TodayPlusMinus1KQ
    WHERE MyDate BETWEEN #05/01/2014# and #05/30/2014#
    
    0 讨论(0)
  • 2020-11-21 05:57

    For Oracle, my solution is:

    select trunc(sysdate-dayincrement, 'DD') 
      from dual, (select level as dayincrement 
                    from dual connect by level <= 30)
    

    Sysdate can be changed to specific date and level number can be changed to give more dates.

    0 讨论(0)
  • 2020-11-21 05:59

    Using a recursive Common Table Expression (CTE), you can generate a list of dates, then select from it. Obviously you normally wouldn't want to create three million dates, so this just illustrates the possibilities. You could simply limit the date range inside the CTE and omit the where clause from the select statement using the CTE.

    with [dates] as (
        select convert(datetime, '1753-01-01') as [date] --start
        union all
        select dateadd(day, 1, [date])
        from [dates]
        where [date] < '9999-12-31' --end
    )
    select [date]
    from [dates]
    where [date] between '2013-01-01' and '2013-12-31'
    option (maxrecursion 0)
    

    On Microsoft SQL Server 2005, generating the CTE list of all possible dates took 1:08. Generating one hundred years took less than a second.

    0 讨论(0)
提交回复
热议问题