[英]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.在此之后,我收到错误“ORA-00913:值太多”,因为 select 语句中的子查询正在拉两列。 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.我需要另一列包含来自“BK5”位置的库存。 The "QTY" column currently contains the sum from the BK1-4 locations.
“QTY”列当前包含来自 BK1-4 位置的总和。
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)
您可以将子查询移动到 CTE 表达式
With temp_table_name as (subquery..)
或使用窗口函数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)我想第一种方法可能类似于以下内容(我目前无法访问 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
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. With 语句声明了上面的某种形式的临时表(公用表表达式),让我们将其命名为
qty_src
,然后将其加入主查询以获取求和值。
I removed the distinct statement from the sub query as the aggregation functions sum()....group by
do filter fields distinctly.我从子查询中删除了不同的语句作为聚合函数
sum()....group by
do filter fields 明显。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.