如何在子查询中获得不同项的总和?

时间:2019-12-11 22:03:24

标签: sql oracle

这是我到目前为止的查询。

SELECT DISTINCT(ITEM_NAME),
  DESCRIPTION,
  SUM(wm_inventory.ON_HAND_QTY) "INV",
  (SELECT DISTINCT(ITEM_NAME),
    SUM(wm_inventory.ON_HAND_QTY)
    FROM LOCN_HDR lh
    INNER JOIN wm_inventory
    ON lh.LOCN_ID = wm_inventory.LOCATION_ID
    INNER JOIN item_cbo
    ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
    where zone = 'BK5') as "QTY"
FROM LOCN_HDR lh
INNER JOIN wm_inventory
ON lh.LOCN_ID = wm_inventory.LOCATION_ID
INNER JOIN item_cbo
ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
WHERE ZONE IN ('BK1','BK2','BK3','BK4')
and ITEM_NAME in (SELECT DISTINCT(item_cbo.ITEM_NAME)
    FROM LOCN_HDR lh
    INNER JOIN wm_inventory
    ON lh.LOCN_ID = wm_inventory.LOCATION_ID
    INNER JOIN item_cbo
    ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
    where zone = 'BK5')
GROUP BY ITEM_NAME,
DESCRIPTION
ORDER BY ITEM_NAME

在此之后,我收到错误消息“ ORA-00913:值太多”,因为select语句中的子查询提取了两列。有解决办法吗?

基本上,我需要提取两组不同位置的清单,并将它们并排比较。

enter image description here

我需要从“ BK5”位置开始的另一列带有库存的清单。 “ QTY”列当前包含来自BK1-4位置的总和。

2 个答案:

答案 0 :(得分:0)

您可以将子查询移动到CTE表达式With temp_table_name as (subquery..)中,也可以使用窗口函数sum(column1)over( partition by column2 order by xxxx)

我想第一种方法可以像下面这样(我目前无法访问SQL编辑器,因此您可能要提防语法或琐碎的错误)

    With qty_src as (
    SELECT ITEM_NAME ,
    SUM(wm_inventory.ON_HAND_QTY)  as qty
    FROM LOCN_HDR lh
    INNER JOIN wm_inventory
    ON lh.LOCN_ID = wm_inventory.LOCATION_ID
    INNER JOIN item_cbo
    ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
    where zone = 'BK5'
)
SELECT DISTINCT(ITEM_NAME),
  DESCRIPTION,
  SUM(wm_inventory.ON_HAND_QTY) "INV",
  qty_src.qty as "QTY"
FROM LOCN_HDR lh
INNER JOIN wm_inventory
ON lh.LOCN_ID = wm_inventory.LOCATION_ID
INNER JOIN item_cbo
ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
LEFT JOIN qty_src
    on qty_src.ITEM_NAME = lh.ITEM_NAME
WHERE ZONE IN ('BK1','BK2','BK3','BK4')
and ITEM_NAME in (SELECT DISTINCT(item_cbo.ITEM_NAME)
    FROM LOCN_HDR lh
    INNER JOIN wm_inventory
    ON lh.LOCN_ID = wm_inventory.LOCATION_ID
    INNER JOIN item_cbo
    ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
    where zone = 'BK5')
GROUP BY ITEM_NAME,
DESCRIPTION
ORDER BY ITEM_NAME

With语句在上面声明了某种形式的临时表(Common Table Expression),让我们将其命名为qty_src,然后将其保留在主查询中以获得求和值。

我从子查询中删除了distinct语句,因为聚合函数sum()....group by确实对字段进行了过滤。

答案 1 :(得分:0)

    SELECT ITEM_NAME,
      DESCRIPTION,
      SUM(wm_inventory.ON_HAND_QTY) "INV",
      (SELECT 
        SUM(wm_inventory.ON_HAND_QTY)
        FROM LOCN_HDR lh
        INNER JOIN wm_inventory
        ON lh.LOCN_ID = wm_inventory.LOCATION_ID
        INNER JOIN item_cbo_sub_query
        ON wm_inventory.ITEM_ID = item_cbo_sub_query.ITEM_ID
        where zone = 'BK5'
        AND item_cbo_sub_query.ITEM_ID=item_cbo.ITEM_ID
) as "QTY BK5"
    FROM LOCN_HDR lh
    INNER JOIN wm_inventory
    ON lh.LOCN_ID = wm_inventory.LOCATION_ID
    INNER JOIN item_cbo
    ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
    WHERE ZONE IN ('BK1','BK2','BK3','BK4')
    and ITEM_NAME in (SELECT item_cbo.ITEM_NAME
        FROM LOCN_HDR lh
        INNER JOIN wm_inventory
        ON lh.LOCN_ID = wm_inventory.LOCATION_ID
        INNER JOIN item_cbo
        ON wm_inventory.ITEM_ID = item_cbo.ITEM_ID
        where zone = 'BK5')
    GROUP BY ITEM_NAME,
    DESCRIPTION
    ORDER BY ITEM_NAME