将多个子查询转换成一个join(Join table to itself)
Convert multiple subqueries into a join (Join table to itself)
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
我想根据按商店分组的类型对所有产品的计数、成本和净成本求和。
结果应如下所示:
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
我可以通过使用主查询和如下所示的子查询来实现上述目标,是否有更多
高效连接(将 table 连接到自身等)我能做到吗?
我将放置在主查询中的子查询:
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)
您可以使用 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
您应该有一个带有 IsPremium 位列的 ProductType table。这样,当出现类型 6、7、8.. 时,您不必对代码进行可怕的容易出错的维护 - 您只需在类型 table.
中插入新行
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
我想根据按商店分组的类型对所有产品的计数、成本和净成本求和。 结果应如下所示:
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
我可以通过使用主查询和如下所示的子查询来实现上述目标,是否有更多 高效连接(将 table 连接到自身等)我能做到吗?
我将放置在主查询中的子查询:
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)
您可以使用 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
您应该有一个带有 IsPremium 位列的 ProductType table。这样,当出现类型 6、7、8.. 时,您不必对代码进行可怕的容易出错的维护 - 您只需在类型 table.
中插入新行