sql仅选择缺少的月份

2skhul33  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(331)

请注意,输出中缺少2017-04-01、2018-02-01、2018-07-01和2019-01-01个月。我只想展示那些失踪的月份。有人知道怎么做吗?
查询:

SELECT TO_DATE("Month", 'mon''yy') as dates FROM sample_sheet
group by dates
order by dates asc;

输出:

2017-01-01
2017-02-01
2017-03-01
2017-05-01
2017-06-01
2017-07-01
2017-08-01
2017-09-01
2017-10-01
2017-11-01
2017-12-01
2018-01-01
2018-03-01
2018-04-01
2018-05-01
2018-06-01
2018-08-01
2018-09-01
2018-10-01
2018-11-01
2018-12-01
2019-02-01
2019-03-01
2019-04-01
zpgglvta

zpgglvta1#

我不知道vertica,所以我在microsoftsqlserver中编写了一个有效的概念证明,并试图根据在线文档将其转换为vertica语法。
应该是这样的:

with 
months as (
   select 2017 as date_year, 1 as date_month, to_date('2017-01-01', 'YYYY-MM-DD') as first_date, to_date('2017-01-31', 'yyyy-mm-dd') as last_date
   union all
   select
      year(add_months(first_date, 1)) as date_year,
      month(add_months(first_date, 1)) as date_month, 
      add_months(first_date, 1) as first_date, 
      last_day(add_months(first_date, 1)) as last_date 
   from months
   where first_date < current_date
),
sample_dates (a_date) as (
   select to_date('2017-01-15', 'YYYY-MM-DD') union all
   select to_date('2017-01-22', 'YYYY-MM-DD') union all
   select to_date('2017-02-01', 'YYYY-MM-DD') union all
   select to_date('2017-04-15', 'YYYY-MM-DD') union all
   select to_date('2017-06-15', 'YYYY-MM-DD') 
)
select * 
from sample_dates right join months on sample_dates.a_date between first_date and last_date
where sample_dates.a_date is null

months是一个递归动态表,它包含自2017-01年以来的所有月份,以及月份的第一天和最后一天。示例日期只是测试逻辑的日期列表-您应该用自己的表替换它。
一旦你建立了月历表,你所需要做的就是用一个外部查询来对照它检查你的日期,看看哪些日期不在第一个日期和最后一个日期列之间。

vql8enpb

vql8enpb2#

您可以构建一个时间序列,包含第一个输入日期和最后一个输入日期之间的所有日期(时间序列的最高粒度是天),并且只过滤掉其中几个月的第一天;然后将创建月份第一个序列的join与您的输入一起左移,以找出join失败的位置,并从join的输入分支检查空值:

WITH
-- your input
input(mth1st) AS (
          SELECT DATE '2017-01-01'
UNION ALL SELECT DATE '2017-02-01'
UNION ALL SELECT DATE '2017-03-01'
UNION ALL SELECT DATE '2017-05-01'
UNION ALL SELECT DATE '2017-06-01'
UNION ALL SELECT DATE '2017-07-01'
UNION ALL SELECT DATE '2017-08-01'
UNION ALL SELECT DATE '2017-09-01'
UNION ALL SELECT DATE '2017-10-01'
UNION ALL SELECT DATE '2017-11-01'
UNION ALL SELECT DATE '2017-12-01'
UNION ALL SELECT DATE '2018-01-01'
UNION ALL SELECT DATE '2018-03-01'
UNION ALL SELECT DATE '2018-04-01'
UNION ALL SELECT DATE '2018-05-01'
UNION ALL SELECT DATE '2018-06-01'
UNION ALL SELECT DATE '2018-08-01'
UNION ALL SELECT DATE '2018-09-01'
UNION ALL SELECT DATE '2018-10-01'
UNION ALL SELECT DATE '2018-11-01'
UNION ALL SELECT DATE '2018-12-01'
UNION ALL SELECT DATE '2019-02-01'
UNION ALL SELECT DATE '2019-03-01'
UNION ALL SELECT DATE '2019-04-01'
)
,
-- need a series of month's firsts
-- TIMESERIES works for INTERVAL DAY TO SECOND
-- so build that timeseries, and filter out
-- the month's firsts
limits(mth1st) AS (
          SELECT MIN(mth1st) FROM input
UNION ALL SELECT MAX(mth1st) FROM input
)
,
alldates AS (
  SELECT dt::DATE FROM limits
  TIMESERIES dt AS '1 day' OVER(ORDER BY mth1st::TIMESTAMP)
)
,
allfirsts(mth1st) AS (
  SELECT dt FROM alldates WHERE DAY(dt)=1
)
SELECT
  allfirsts.mth1st
FROM allfirsts
LEFT JOIN input USING(mth1st)
WHERE input.mth1st IS NULL;
-- out    mth1st   
-- out ------------
-- out  2017-04-01
-- out  2018-02-01
-- out  2018-07-01
-- out  2019-01-01

相关问题