synkx10
1/17/2020 - 2:02 PM

開始日/終了日から日付表を作成する

WITH SAMPLE AS (
  SELECT 1 AS ID,	DATE('2020-01-01') AS START_DATE, 	DATE('2020-01-15') AS END_DATE,	20 AS COUNT UNION ALL
  SELECT 2, DATE('2020-01-01'),	DATE('2020-01-15'),	2	UNION ALL
  SELECT 3, DATE('2020-01-05'),	DATE('2020-01-06'),	5	UNION ALL
  SELECT 4, DATE('2020-01-03'),	DATE('2020-01-12'),	7	UNION ALL
  SELECT 5, DATE('2020-01-10'),	DATE('2020-01-14'),	1	UNION ALL
  SELECT 6, DATE('2020-01-02'),	DATE('2020-01-13'),	4	UNION ALL
  SELECT 7, DATE('2020-01-12'),	DATE('2020-01-15'),	9	UNION ALL
  SELECT 8, DATE('2020-01-08'),	DATE('2020-01-10'),	2	UNION ALL
  SELECT 9, DATE('2020-01-02'),	DATE('2020-01-08'),	4	UNION ALL
  SELECT 10, DATE('2020-01-12'),	DATE('2020-01-14'),	10
),
CALENDER AS (
  SELECT GENERATE_DATE_ARRAY(
    (SELECT MIN(START_DATE) FROM SAMPLE), 
    (SELECT MAX(END_DATE) FROM SAMPLE)
  ) AS DT
)
SELECT DATE FROM CALENDER, UNNEST(DT) AS DATE