简体   繁体   English

根据另一列的值将一列汇总为两个不同的列

[英]Sum a column into two different columns based on another column's value

What I need is only a list of the items in "Storage", but the resulting set should include the sum of that item's quantity in both the storage and active locations.我需要的只是“存储”中的项目列表,但结果集应包括存储和活动位置中该项目数量的总和。

Here's a dataset example:这是一个数据集示例:

ID ID Item物品 Location地点 Qty数量
1 1个 ItemA项目A Storage贮存 4 4个
2 2个 ItemA项目A Active积极的 9 9
3 3个 ItemB项目B Storage贮存 3 3个
4 4个 ItemB项目B Storage贮存 2 2个
5 5个 ItemA项目A Active积极的 1 1个
6 6个 ItemC C项 Boxed盒装 3 3个
7 7 ItemD项目D Active积极的 1 1个
8 8个 ItemD项目D Storage贮存 1 1个

The result would look like this:结果看起来像这样:

Item物品 Storage贮存 Active积极的
ItemA项目A 4 4个 10 10
ItemB项目B 5 5个 0 0
ItemD项目D 1 1个 1 1个

Note that ItemC should not be included because it is not in a valid location.请注意,不应包含 ItemC,因为它不在有效位置。

What I have tried so far is:到目前为止我尝试过的是:

SELECT 
    ITEMDESC.A, 
    SUM(CASE WHEN LOCATION.A='Storage' THEN QTY.A ELSE 0 END), 
    SUM(CASE WHEN LOCATION.B='Active'  THEN QTY.B ELSE 0 END)
 FROM       
    ITEMS A, ITEMS B
 INNER JOIN     
    ITEMDESC.A = ITEMDESC.B
 WHERE      
 GROUP BY   
    ITEMDESC.A

but this returns ALL items listed.但这会返回列出的所有项目。 When I add something like "WHERE Location.B = 'Storage'" then it only sums the items in the storage and all the active location items are 0.当我添加类似“WHERE Location.B = 'Storage'”的内容时,它只会对存储中的项目求和,所有活动位置项目均为 0。

Use a WHERE clause to only look at the locations in question:使用WHERE子句只查看有问题的位置:

select
  item,
  sum(case when location = 'Storage' then qty else 0 end) as storage,
  sum(case when location = 'Active' then qty else 0 end) as active
from items
where location in ('Storage', 'Active')
group by item
order by item;

Update更新

You have changed the desired output in your request and only want items that are in 'Storage'.您已在请求中更改所需的 output,并且只需要“存储”中的项目。 For this, just add a HAVING clause, eg:为此,只需添加一个HAVING子句,例如:

select
  item,
  sum(case when location = 'Storage' then qty else 0 end) as storage,
  sum(case when location = 'Active' then qty else 0 end) as active
from items
where location in ('Storage', 'Active')
group by item
having sum(case when location = 'Storage' then qty else 0 end) > 0
order by item;

This should give you the desired results.这应该会给你想要的结果。 The other answers are including values not in 'storage'其他答案包括不在“存储”中的值

select 
  item,
  sum(case when location = 'Active' then qty else 0 end) as active_qty,
  sum(case when location = 'Storage' then qty else 0 end) as storage_qty
from *table*
where item in (select item from *table* where location = 'Storage')
group by item
order by item;
Select  item,
        SUM(Case When Location = 'Storage' THEN Qty else 0 END) AS Storage,
        SUM(Case When Location = 'Active' THEN Qty else 0 END) AS Active 
from table1 
where location in ('Storage','Active')
GROUP BY Item

http://sqlfiddle.com/#!9/7339b9a/8 http://sqlfiddle.com/#!9/7339b9a/8

You could use the WHERE clause in a subquery to identify items of interest and then JOIN to filter the rows prior to aggregation您可以在子查询中使用 WHERE 子句来识别感兴趣的项目,然后在聚合之前使用 JOIN 来过滤行

SELECT 
    A.ITEMDESC, 
    SUM(CASE WHEN A.LOCATION='Storage' THEN A.QTY ELSE 0 END), 
    SUM(CASE WHEN A.LOCATION='Active'  THEN A.QTY ELSE 0 END)
 FROM       
    ITEMS A
 INNER JOIN
    (SELECT DISTINCT ITEMDESC FROM ITEMS WHERE LOCATION='Storage') B
 ON      
    A. ITEMDESC = B.ITEMDESC
 GROUP BY   
    A.ITEMDESC

Or you could filter the rows after aggregation with a HAVING clause或者您可以在聚合后使用 HAVING 子句过滤行

SELECT 
    ITEMDESC, 
    SUM(CASE WHEN LOCATION='Storage' THEN QTY ELSE 0 END), 
    SUM(CASE WHEN LOCATION='Active'  THEN QTY ELSE 0 END)
 FROM       
    ITEMS
 GROUP BY   
    ITEMDESC
 HAVING
    MAX(CASE WHEN LOCATION='Storage' THEN 1 ELSE 0 END) > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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