【问题标题】:SQL Select only missing monthsSQL 仅选择缺少的月份
【发布时间】:2020-06-10 03:50:36
【问题描述】:

请注意,输出中缺少 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

【问题讨论】:

  • 如果您的表格不包含日期,您将如何显示?
  • 我想也许可以与一个临时日历表进行比较,其中包含上述表中第一个和最后一个日期之间的日期。

标签: sql vertica


【解决方案1】:

我不了解 Vertica,因此我在 Microsoft SQL Server 中编写了一个工作概念证明,并尝试根据在线文档将其转换为 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 以来的所有月份,包括该月的第一天和最后一天。 sample_dates 只是用于测试逻辑的日期列表 - 您应该将其替换为您自己的表格。

一旦你建立了月历表,你需要做的就是使用外部查询检查你的日期,看看哪些日期不在 first_datelast_date 之间的任何时期之间 列。

【讨论】:

  • 如果您在使用 Vertica 时发现任何语法错误,请告诉我。
【解决方案2】:

您可以构建第一个输入日期和最后一个输入日期之间的所有日期的 TIMESERIES(TIMESERIES 的最高粒度是天。),然后只过滤掉月份的第一天;然后使用您的输入离开创建月份第一序列的连接,以找出连接失败的位置,从连接的输入分支检查 NULLS:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2012-02-24
    • 2021-05-22
    相关资源
    最近更新 更多