SQL限制数据集

时间:2019-03-27 14:23:43

标签: sql oracle

我有一个查询每天更新的数据集。该查询首先根据月末时间范围(每个月末不是指定的时间范围)和规定的时间范围(当前月末,上个月末,当前季度末,上一季度)从数据库中提取11个不同的日期结束等)。

我遇到的问题是在一月/四月/七月/十月和二月/五月/八月/十一月-我们看到重复的日期流入查询。即,在一月中旬,当前月末将是12月,当前季度末将是12月。或在2月中旬,当前的“上个月末”和“当前的季度末”都将是12月。

这导致Tableau下游出现聚合问题。

创建的日期表如下:

Period    Period_Selection
12/31/18  Current Quarter End
12/31/18  Prior Month End
1/31/19   Current Month End
9/30/18   Prior Quarter End

我想做的是编写一个case语句或where子句,将数据表限制为每个日期的单个实例。这些情况将是特定的,因此将影响以下对:“当前月末”和“当前季度末”或“先前月末”和“当前季度末”。在上面的示例中,该子句将看到有两个示例“ 12/31/18”,然后删除/忽略其中一个实例,例如,删除“上个月末”并保留“当前季度末”。 / p>

SQL示例:

SELECT PERIOD_DATE, PERIOD_SELECTION
FROM db.PERIOD_PROMPT
WHERE db_ORDER_NO IN ( '60000000', '50000000', '40000000')-- 50.. = Current Quarter end, 60.. = current month end, 40.. = prior month end

UNION ALL

SELECT ADD_MONTHS(PERIOD_DT, -3) AS PERIOD_DATE
, 'Prior Quarter End' AS PERIOD_SELECTION
FROM db.PERIOD_PROMPT 
WHERE db_ORDER_NO IN (50000000) --CURRENT QUARTER END

1 个答案:

答案 0 :(得分:0)

如果您希望每个PERIOD_DATE有一行,那么您可能想要GROUP BY PERIOD_DATE。如果您不在乎选择哪个PERIOD_SELECTION,则只需选择一个汇总函数即可为您选择。在这里,我使用MAX选择按字母顺序排在最后的PERIOD_SELECTION(在这种情况下为“ Prior Month End”)。

-- sample data
with period_prompt as (select to_date('12/31/18', 'mm/dd/yy') as period_dt, 'Current Quarter End' as period_selection, 50000000 as db_order_no from dual
                       union all 
                       select to_date('12/31/18', 'mm/dd/yy') as period_dt, 'Prior Month End' as period_selection, 40000000 as db_order_no from dual)
select period_date, 
    max(period_selection) as period_selection
from ( -- your query
    SELECT PERIOD_DT as PERIOD_DATE, PERIOD_SELECTION
    FROM PERIOD_PROMPT
    WHERE db_ORDER_NO IN ( '60000000', '50000000', '40000000')-- 50.. = Current Quarter end, 60.. = current month end, 40.. = prior month end
    UNION ALL
    SELECT ADD_MONTHS(PERIOD_DT, -3) AS PERIOD_DATE
    , 'Prior Quarter End' AS PERIOD_SELECTION
    FROM PERIOD_PROMPT 
    WHERE db_ORDER_NO IN (50000000) --CURRENT QUARTER END
    )
group by period_date;

但是,如果您对应显示哪个特定PERIOD_SELECTION 有意见,则需要对它们进行排序-提出一种排序算法。有时会有一个自然的选择-例如,您可能想要PERIOD_SELECTION最高的DB_ORDER_NO。但是在这种情况下,我不确定您是否有一个很好的排序依据列,因此我们将创建一个列。

在此示例中,我添加了一个PRIORITY列,并将其设置为db_order_no为50000000(当前季度末)的优先级为2,其他所有事物的优先级为1。然后我们调用MAX(period_selection),但指定我们要使用priority最高的period_selection条目。

with period_prompt as (select to_date('12/31/18', 'mm/dd/yy') as period_dt, 'Current Quarter End' as period_selection, 50000000 as db_order_no from dual
                       union all 
                       select to_date('12/31/18', 'mm/dd/yy') as period_dt, 'Prior Month End' as period_selection, 40000000 as db_order_no from dual)
select period_date, 
    max(period_selection) keep (dense_rank first order by priority desc) as period_selection
from (
    SELECT PERIOD_DT PERIOD_DATE, PERIOD_SELECTION, 
        case db_order_no
            when 50000000 then 2
            else 1
        end as priority
    FROM PERIOD_PROMPT
    WHERE db_ORDER_NO IN ( '60000000', '50000000', '40000000')-- 50.. = Current Quarter end, 60.. = current month end, 40.. = prior month end
    UNION ALL
    SELECT ADD_MONTHS(PERIOD_DT, -3) AS PERIOD_DATE
    , 'Prior Quarter End' AS PERIOD_SELECTION
    , 1 as priority
    FROM PERIOD_PROMPT 
    WHERE db_ORDER_NO IN (50000000) --CURRENT QUARTER END
    )
group by period_date;

输出:

PERIOD_DATE PERIOD_SELECTION   
----------- -------------------
30-SEP-18   Prior Quarter End  
31-DEC-18   Current Quarter End