简体   繁体   中英

How can I get the sum of distinct items in a subquery?

This is my query so far.

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

After this I get the error "ORA-00913: too many values" Since the subquery in the select statement is pulling two columns. Is there a fix for this?

Basically I need to pull the inventory of two different sets of locations and compare them side by side.

在此处输入图片说明

I need another column with the inventory from the "BK5" location. The "QTY" column currently contains the sum from the BK1-4 locations.

You can either move the subquery into CTE expression With temp_table_name as (subquery..) or use a window function sum(column1)over( partition by column2 order by xxxx)

I would imagine the first approach could go some what like the following, (I don't have access to SQL editor at the moment, so you might want to watch out for syntax or trivial errors)

    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

the With statement declares some form of temporary table (Common Table Expression) above let's named it qty_src and then left join it in the main query to get the summation value.

I removed the distinct statement from the sub query as the aggregation functions sum()....group by do filter fields distinctly.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM