简体   繁体   English

如何在 case 语句中进行子查询?

[英]How do I subquery within a case statement?

So here's my coding所以这是我的编码

select item_number[Item Number],    
            SUM(CASE WHEN [type] = 0 and location_id not in (select location_id from t_location with(nolock) where location_id like 'GW%' or location_id like 'SW%')    THEN actual_qty ELSE 0 END) AS [DC C1]  
            ,SUM(CASE WHEN [type] = -6 and location_id not in (select location_id from t_location with(nolock) where location_id like 'GW%' or location_id like 'SW%')  THEN actual_qty ELSE 0 END) AS [DC C7]  
            ,SUM(CASE WHEN [type] = -9 and location_id not in (select location_id from t_location with(nolock) where location_id like 'GW%' or location_id like 'SW%')  THEN actual_qty ELSE 0 END) AS [DC C1lock]          
            ,SUM(CASE WHEN [type] = -15 and location_id not in (select location_id from t_location with(nolock) where location_id like 'GW%' or location_id like 'SW%') THEN actual_qty ELSE 0 END) AS [DC C7lock]
            ,SUM(CASE WHEN [type] = 0 and location_id like 'GW%'    THEN actual_qty ELSE 0 END) AS [GW C1]          
            ,SUM(CASE WHEN [type] = -6 and location_id like 'GW%'   THEN actual_qty ELSE 0 END) AS [GW C1]      
            ,SUM(CASE WHEN [type] = -9 and location_id like 'GW%'   THEN actual_qty ELSE 0 END) AS [GW C1lock]      
            ,SUM(CASE WHEN [type] = -15 and location_id like 'GW%'  THEN actual_qty ELSE 0 END) AS [GW C7lock]
            ,SUM(CASE WHEN [type] = 0 and location_id like 'SW%'    THEN actual_qty ELSE 0 END) AS [SW C1]          
            ,SUM(CASE WHEN [type] = -6 and location_id like 'SW%'   THEN actual_qty ELSE 0 END) AS [SW C1]      
            ,SUM(CASE WHEN [type] = -9 and location_id like 'SW%'   THEN actual_qty ELSE 0 END) AS [SW C1lock]      
            ,SUM(CASE WHEN [type] = -15 and location_id like 'SW%'  THEN actual_qty ELSE 0 END) AS [SW C7lock]  
From t_stored_item with(nolock)         
group by item_number

The error code I get is我得到的错误代码是

Msg 130, Level 15, State 1, Line 3 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.消息 130,级别 15,状态 1,第 3 行无法对包含聚合或子查询的表达式执行聚合函数。

Msg 130, Level 15, State 1, Line 5 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.消息 130,级别 15,状态 1,第 5 行无法对包含聚合或子查询的表达式执行聚合函数。

Msg 130, Level 15, State 1, Line 7 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.消息 130,级别 15,状态 1,第 7 行无法对包含聚合或子查询的表达式执行聚合函数。

Msg 130, Level 15, State 1, Line 9 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.消息 130,级别 15,状态 1,第 9 行无法对包含聚合或子查询的表达式执行聚合函数。

Now I know the rest of the code is good but the only issue that I'm having right now is because of the subquery when wrote like this.现在我知道代码的其余部分很好,但我现在遇到的唯一问题是因为这样编写时的子查询。 I eventually want all of the inventory within the warehouses we have but I want them to be split up by different warehouses.我最终想要我们拥有的仓库中的所有库存,但我希望它们被不同的仓库分开。 We gave the precursor warehouse names as GW and SW the rest is just out there as a lot of different aisles within the main warehouse but there's no real constant there that I can ping off of.我们给前身仓库命名为 GW 和 SW,其余的就在那里,因为主仓库中有很多不同的过道,但那里没有真正的常数可以确定。 The query wrote on it's own works just fine自己写的查询工作正常

select location_id 
from t_location with(nolock) 
where location_id like 'GW%' or location_id like 'SW%'

But when nested into the case statement and summing it altogether then it doesn't want to pull back any results... The format at the end gives one line per each item number but with different columns for the different warehouses and those are how I want to view the information because it's much easier to read than to just jump around different lines.但是,当嵌套到 case 语句中并将其总和时,它不想撤回任何结果......最后的格式为每个项目编号提供一行,但对于不同的仓库有不同的列,这就是我的方式想要查看信息,因为它比只是在不同的行之间跳转更容易阅读。

Is there anyway to make this possible?有没有办法让这成为可能? The grouping needs to stay so that everything pulls back but I'm not figuring out an easy way to get this to pull the information.分组需要保留,以便一切都拉回来,但我没有想出一种简单的方法来获取信息。

Use a join with conditional aggregation:使用带有条件聚合的连接:

SELECT
    si.item_number,
    SUM(CASE WHEN si.[type] = 0 AND l.location_id NOT LIKE '[GS]W%'
            THEN l.actual_qty ELSE 0 END) AS [DC C1],
    SUM(CASE WHEN si.[type] = -6 AND l.location_id NOT LIKE '[GS]W%'
            THEN l.actual_qty ELSE 0 END) AS [DC C7],
    SUM(CASE WHEN si.[type] = -9 AND l.location_id NOT LIKE '[GS]W%'
            THEN l.actual_qty ELSE 0 END) AS [DC C1lock],
    SUM(CASE WHEN si.[type] = -15 AND l.location_id NOT LIKE '[GS]W%'
            THEN l.actual_qty ELSE 0 END) AS [DC C7lock],
    SUM(CASE WHEN si.[type] = 0 AND l.location_id LIKE 'GW%'
            THEN l.actual_qty ELSE 0 END) AS [GW C1],
    ...
FROM t_stored_item si
LEFT JOIN t_location l
    ON si.location_id = l.location_id
GROUP BY
    si.item_number;

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

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