如何从案例语句Oracle SQL的结果集中排除null值行

发布于 2025-01-24 06:37:51 字数 1434 浏览 0 评论 0原文

我试图从单位_SHORTED列(来自案例子句)中排除具有null值的行,但找不到方法。

SELECT DISTINCT td.task_id,td.cntr_nbr,lh.dsp_locn AS pull_locn,td.orig_reqmt,td.qty_pulld,
(
CASE 
WHEN
((SUM(td.qty_pulld) over (partition by td.pull_locn_id)) < td.orig_reqmt)  and ((SUM(td.qty_pulld) over (partition by td.pull_locn_id))-td.orig_reqmt <> 0) 
THEN (td.orig_reqmt- td.qty_pulld)
END) AS units_shorted
FROM wm14.task_dtl td 
INNER JOIN wm14.locn_hdr lh ON lh.locn_id = td.pull_locn_id
INNER JOIN wm14.order_line_item oli ON oli.item_id = td.item_id
WHERE  EXISTS (SELECT 1 FROM wm14.msg_log ml WHERE ml.user_id = td.user_id AND ml.msg_id IN ('1060','1034') AND module = 'CTRLKEY' 
AND TRUNC(td.mod_date_time) = TRUNC(ml.create_date_time)) AND td.invn_need_type IN ('53','54')
AND td.stat_code >= '90' 
and td.task_genrtn_ref_nbr NOT IN (SELECT  ml.ref_value_1 FROM wm14.msg_log ml WHERE  ml.msg ='Undo Wave completed')
group by td.task_id,td.cntr_nbr,lh.dsp_locn,td.task_genrtn_ref_nbr,td.pull_locn_id,td.item_id,td.qty_pulld,td.orig_reqmt
ORDER BY td.task_id,lh.dsp_locn DESC;

结果集:

TASK_ID, CNTR_NBR,   PULL_LOCN,     WAVE_NBR,   ORIG_REQMT, QTY_PULLD, UNITS_SHORTED
5420174  GPK0324828 1GF-27-02144    202204200024    24       23           1
5420174  GPK0324822 1GF-23-03110    202204200024    5        3            2
5420174  GPK0324823 1GF-26-04102    202204200024    7        7           null

I am trying to exclude the rows with null values from Units_shorted column (from the CASE clause) but not finding a way.

SELECT DISTINCT td.task_id,td.cntr_nbr,lh.dsp_locn AS pull_locn,td.orig_reqmt,td.qty_pulld,
(
CASE 
WHEN
((SUM(td.qty_pulld) over (partition by td.pull_locn_id)) < td.orig_reqmt)  and ((SUM(td.qty_pulld) over (partition by td.pull_locn_id))-td.orig_reqmt <> 0) 
THEN (td.orig_reqmt- td.qty_pulld)
END) AS units_shorted
FROM wm14.task_dtl td 
INNER JOIN wm14.locn_hdr lh ON lh.locn_id = td.pull_locn_id
INNER JOIN wm14.order_line_item oli ON oli.item_id = td.item_id
WHERE  EXISTS (SELECT 1 FROM wm14.msg_log ml WHERE ml.user_id = td.user_id AND ml.msg_id IN ('1060','1034') AND module = 'CTRLKEY' 
AND TRUNC(td.mod_date_time) = TRUNC(ml.create_date_time)) AND td.invn_need_type IN ('53','54')
AND td.stat_code >= '90' 
and td.task_genrtn_ref_nbr NOT IN (SELECT  ml.ref_value_1 FROM wm14.msg_log ml WHERE  ml.msg ='Undo Wave completed')
group by td.task_id,td.cntr_nbr,lh.dsp_locn,td.task_genrtn_ref_nbr,td.pull_locn_id,td.item_id,td.qty_pulld,td.orig_reqmt
ORDER BY td.task_id,lh.dsp_locn DESC;

Result set:

TASK_ID, CNTR_NBR,   PULL_LOCN,     WAVE_NBR,   ORIG_REQMT, QTY_PULLD, UNITS_SHORTED
5420174  GPK0324828 1GF-27-02144    202204200024    24       23           1
5420174  GPK0324822 1GF-23-03110    202204200024    5        3            2
5420174  GPK0324823 1GF-26-04102    202204200024    7        7           null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2025-01-31 06:37:51

一个简单的选项 - 如果查询您有返回所需的结果集 - 是将其用作CTE(或子查询),然后滤除行:

    with temp as
    -- your current query begins here
    (SELECT DISTINCT td.task_id,td.cntr_nbr,lh.dsp_locn AS pull_locn,td.orig_reqmt,td.qty_pulld,
        (
        CASE 
        WHEN
        ((SUM(td.qty_pulld) over (partition by td.pull_locn_id)) < td.orig_reqmt)  and ((SUM(td.qty_pulld) over (partition by td.pull_locn_id))-td.orig_reqmt <> 0) 
        THEN (td.orig_reqmt- td.qty_pulld)
        END) AS units_shorted
        FROM wm14.task_dtl td 
        INNER JOIN wm14.locn_hdr lh ON lh.locn_id = td.pull_locn_id
        INNER JOIN wm14.order_line_item oli ON oli.item_id = td.item_id
        WHERE  EXISTS (SELECT 1 FROM wm14.msg_log ml WHERE ml.user_id = td.user_id AND ml.msg_id IN ('1060','1034') AND module = 'CTRLKEY' 
        AND TRUNC(td.mod_date_time) = TRUNC(ml.create_date_time)) AND td.invn_need_type IN ('53','54')
        AND td.stat_code >= '90' 
        and td.task_genrtn_ref_nbr NOT IN (SELECT  ml.ref_value_1 FROM wm14.msg_log ml WHERE  ml.msg ='Undo Wave completed')
        group by td.task_id,td.cntr_nbr,lh.dsp_locn,td.task_genrtn_ref_nbr,td.pull_locn_id,td.item_id,td.qty_pulld,td.orig_reqmt
    -- your current query ends here; only the ORDER BY clause is moved out
    )
    select *
    from temp
    where units_shorted is not null      --> filter
    ORDER BY task_id, dsp_locn DESC;

A simple option - if query you have returns desired result set - is to use it as a CTE (or a subquery) and filter rows out:

    with temp as
    -- your current query begins here
    (SELECT DISTINCT td.task_id,td.cntr_nbr,lh.dsp_locn AS pull_locn,td.orig_reqmt,td.qty_pulld,
        (
        CASE 
        WHEN
        ((SUM(td.qty_pulld) over (partition by td.pull_locn_id)) < td.orig_reqmt)  and ((SUM(td.qty_pulld) over (partition by td.pull_locn_id))-td.orig_reqmt <> 0) 
        THEN (td.orig_reqmt- td.qty_pulld)
        END) AS units_shorted
        FROM wm14.task_dtl td 
        INNER JOIN wm14.locn_hdr lh ON lh.locn_id = td.pull_locn_id
        INNER JOIN wm14.order_line_item oli ON oli.item_id = td.item_id
        WHERE  EXISTS (SELECT 1 FROM wm14.msg_log ml WHERE ml.user_id = td.user_id AND ml.msg_id IN ('1060','1034') AND module = 'CTRLKEY' 
        AND TRUNC(td.mod_date_time) = TRUNC(ml.create_date_time)) AND td.invn_need_type IN ('53','54')
        AND td.stat_code >= '90' 
        and td.task_genrtn_ref_nbr NOT IN (SELECT  ml.ref_value_1 FROM wm14.msg_log ml WHERE  ml.msg ='Undo Wave completed')
        group by td.task_id,td.cntr_nbr,lh.dsp_locn,td.task_genrtn_ref_nbr,td.pull_locn_id,td.item_id,td.qty_pulld,td.orig_reqmt
    -- your current query ends here; only the ORDER BY clause is moved out
    )
    select *
    from temp
    where units_shorted is not null      --> filter
    ORDER BY task_id, dsp_locn DESC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文