SQL查询返回空值存在

时间:2019-06-27 13:12:52

标签: sql sql-server oracle

我有一个SQL表,其中包含ITEM_ID,FROM_QUANTITY,TO_QUANTITY,LIST_PRICE等列。现在,每个项目都应具有从1到12的FROM_QUANTITY,并且FROM_QUANTITY为空。

现在,为了检查表中ITEM缺少哪个QUANTITY,我编写了一个SQL查询,如果FROM_QUANTITY不存在,它将打印None。

查询按预期工作。只是每个商品的数量也为空值,并具有与之相关的价格。但是我的查询显示空为标价。

这是我的查询

with pl as (
      select DISTINCT ITEM_ID, FROM_QUANTITY,TO_QUANTITY, coalesce(to_char(list_price), 'NONE') as list_price
      from PRICELIST_LINE
      where item_id IN ('XYZ') and
            PRICELIST_HDR_KEY in (select Pricelist_Hdr_Key
                              from PRICELIST_HDR
                              where PRICELIST_NAME IN ('ABC') and
                                     SELLER_ORGANIZATION_CODE IN ('100')
                             ) and SYSDATE < END_DATE_ACTIVE
      )
select i.item_id, u.from_quantity, pl.to_quantity,pl.list_price
from (select distinct item_id from pl) i cross join
     (select '1' as from_quantity from dual union all
      select '2' as from_quantity from dual union all
      select '3' as from_quantity from dual union all
      select '4' as from_quantity from dual union all
      select '5' as from_quantity from dual union all
      select '6' as from_quantity from dual union all
      select '7' as from_quantity from dual union all
      select '8' as from_quantity from dual union all
      select '9' as from_quantity from dual union all
      select '10' as from_quantity from dual union all
      select '11' as from_quantity from dual union all
      select '12' as from_quantity from dual union all
      select '' as from_quantity from dual
     ) u left join
     pl
     on pl.item_id = i.item_id and pl.from_quantity = u.from_quantity;

现在的输出如下:

ITEM_ID FROM_QUANTITY TO_QUANTITY LIST_PRICE
------- ------------- ----------- ----------
ABC                 1           2        100
ABC

预期:

ITEM_ID FROM_QUANTITY TO_QUANTITY LIST_PRICE
------- ------------- ----------- ----------
ABC                 1           2        100
ABC                                      200

1 个答案:

答案 0 :(得分:1)

在Oracle中使用partition outer join应该会有所帮助,例如:

 * { box-sizing: border-box; }

html, body{
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}

body{
    display: flex;
    flex-direction: column;
}

section{
    width: 100%;
    height: 100%;
}

nav {
    position: fixed;
    display: flex;
    justify-content: flex-end;
    padding-right: 15px;
    width: 100%;
    top: 0;
    background-color: pink;
    font-family: 'Roboto Mono', monospace;
  }

nav>div {
    margin: 5px;
    padding: 5px;
}
nav div:hover {
    cursor: pointer;
    text-decoration: underline;
}

.projects-section{
    display: flex;
    flex-direction: column;
    position: relative;
    height: 100vh;
    width: 100%;
    font-family: 'Roboto Mono', monospace;
}

.project-main{
    text-align: right;
    margin-top: 20px;
    margin-left: 35%;
    font-size: 20px;
    width: fit-content;
    font-family: 'Roboto Mono', monospace;
}

.project-picture{
    top: 60px;
    height: 200px;
    width: 100%;
    margin-top: 5%;
    background-image: url('./Images/NavbarHeader.png');
    background-size: cover;
    background-position: center;
}

.project-title{
    margin-top: 3%;
    margin-left: auto;
    margin-right: auto;
    width: fit-content;
    font-size: 20px;
    /* font-family: 'Chivo', sans-serif; */
}

.project-description{
    margin-top: 3%;
    margin-left: auto;
    margin-right: auto;
    width: 300px;
    line-height: 20px;
    font-size: 13px;
}
.tech-stack{
    font-size: 13px;
    color: grey;
}
.project-description a {
    font-size: 13px;
    text-decoration: none;
    color: grey;
}
.project-description a:hover {
    cursor: pointer;
    text-decoration: underline;
}
.project-link a:hover{
    cursor: pointer;
}

未经测试。