将多个子查询转换为连接(将表连接到自身)

时间:2017-01-13 16:33:53

标签: sql sql-server sql-server-2008 join self-join

Table structures 

Table Products :
=============================================
ID | Store | Type | Cost | NetCost | 
=============================================
1  |102    | 0    |14.95 | 29.65
2  |103    | 1    |13.95 | 32.65
3  |104    | 2    |12.36 | 42.12
4  |105    | 3    |10.24 | 23.11
5  |106    | 4    |19.57 | 27.16
6  |102    | 0    |57.26 | 26.98
7  |103    | 3    |43.26 | 26.98
8  |104    | 4    |10.26 | 26.98
9  |105    | 1    |9.26  | 26.98
10 |105    | 5    |11.26 | 46.98


Same table separated into  two tables by Type
Premium products  : Type = ( 0, 1, 2 )
Basic products    : Type = ( 3, 4, 5 )

Table Products : Products with Type (0,1,2)
=============================================
ID | Store | Type | Cost | NetCost | 
=============================================
1  |102    | 0    |14.95 | 29.65
2  |103    | 1    |13.95 | 32.65
3  |104    | 2    |12.36 | 42.12
6  |102    | 0    |57.26 | 26.98
9  |105    | 1    |9.26  | 26.98

Table Products : Products with Type (3,4,5)
=============================================
ID | Store | Type | Cost | NetCost | 
=============================================
4  |105    | 3    |10.24 | 23.11
5  |106    | 4    |19.57 | 27.16
7  |103    | 3    |43.26 | 26.98
8  |104    | 4    |10.26 | 26.98
10 |105    | 5    |11.26 | 46.98

我想根据按商店分组的类型对所有产品的Count,Cost和NetCost求和。 结果应如下所示:

Table of totals: (B = Basic, P= Premium)
=================================================================
Store | B Cnt |B Cost | B NetCost | P Cnt | P Cost | P NetCost | 
=================================================================
102
103
104
105
106

我可以通过使用下面显示的子查询的主查询来实现上述目的,还有更多 高效的连接(将表连接到自身等)我可以做到这一点将实现上面的表(总计)?

我将放置在主查询中的子查询:

SELECT  
    count(*)
FROM 
    Products prod_prem
WHERE   
    (prod_prem.Type =  0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)

SELECT  
    SUM(Cost),
FROM 
    Products prod_prem
WHERE   
    (prod_prem.Type =  0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)

SELECT  
    SUM(NetCost)
FROM 
    Products prod_prem
WHERE   
    (prod_prem.Type =  0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)


SELECT  
    count(*)
FROM 
    Products prod_basic
WHERE   
    (prod_prem.Type =  3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)


SELECT  
    SUM(Cost),
FROM 
    Products prod_basic
WHERE   
    (prod_prem.Type =  3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)


SELECT  
    SUM(NetCost)
FROM 
    Products prod_basic
WHERE   
    (prod_prem.Type =  3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)

2 个答案:

答案 0 :(得分:2)

您可以使用Conditional Aggregation

在单个查询中执行此操作
SELECT Store,
       [B Cnt] = Count(CASE WHEN Type IN ( 3, 4, 5 ) THEN 1 END),
       [B Cost] = Sum(CASE WHEN Type IN ( 3, 4, 5 ) THEN Cost ELSE 0 END),
       [B NetCost] = Sum(CASE WHEN Type IN ( 3, 4, 5 ) THEN NetCost ELSE 0 END),
       [P Cnt] = Count(CASE WHEN Type IN ( 0, 1, 2 ) THEN 1 END),
       [P Cost] = Sum(CASE WHEN Type IN ( 0, 1, 2 ) THEN Cost ELSE 0 END),
       [P NetCost] = Sum(CASE WHEN Type IN ( 0, 1, 2 ) THEN NetCost ELSE 0 END)
FROM   Products
GROUP  BY Store 

答案 1 :(得分:0)

您应该有一个带有IsPremium位列的ProductType表。这样,当类型6,7,8出现时,您不必对代码进行可怕的错误维护 - 只需在类型表中插入新行。