generate days from date range

后端 未结 29 2527
渐次进展
渐次进展 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: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#
    

提交回复
热议问题