如何在子查询中按逻辑修复组?
How to fix group by logic in subquery?
我有以下 2 个示例查询及其结果 tables(虚拟数据)如下:
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
GROUP BY subs.Region, subs.Product
Region
Product
TotalPriceA
USA
cameras
200
USA
phones
300
Canada
cameras
300
Canada
phones
500
SELECT
r.Region
,r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY r.Region, rp.Product
Region
Product
TotalPriceB
USA
cameras
201
USA
phones
301
Canada
cameras
301
Canada
phones
501
我想加入他们,这样结果 table 类似于:
Region
Product
TotalPriceA
TotalPriceB
USA
cameras
200
201
USA
phones
300
301
Canada
cameras
300
301
Canada
phones
500
501
但是当我使用这个查询时,我得到的结果 table 类似于:
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
,rptotal.TotalPriceB
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
JOIN
(
SELECT
r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY rp.Product
) rptotal on subs.Product = rptotal.Product
GROUP BY subs.Region, subs.Product, rptotal.TotalPriceB
Region
Product
TotalPriceA
TotalPriceB
USA
cameras
200
502
USA
phones
300
802
Canada
cameras
300
502
Canada
phones
500
802
当我也按区域对子查询进行分组时,我得到的结果更差...
你可以尝试在join
之前使用两个子查询
SELECT t1.Region,
t1.Product,
t2.TotalPriceA,
t1.TotalPriceB
FROM (
SELECT
r.Region
,r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY r.Region, rp.Product
) t1 INNER JOIN (
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
GROUP BY subs.Region, subs.Product
) t2 ON t1.Region = t2.Region AND t1.Product = t2.Product
也许分组依据不是这里所需要的,至少不是最终结果。您是否考虑过使用 pivot
子句?正如 DRapp 所述,您可能需要一个联合来组合这两个查询。您的 group by 只需要预先汇总总值,但 pivot 应该负责。
在此示例中,我使用 table 变量来合并所有信息,然后合并数据透视表。仔细看看,您会发现其中一列对于每个查询始终具有一个常量。此外,根据经验,我知道 table 变量与空列一起工作得更好,而不管实际数据源如何。
Declare @myData Table (
region varchar(max) null,
product varchar(max) null,
type varchar(max) null,
totalPriceA money null
)
--The type is the constant to know whether it's A or B
Insert Into @myData(region, product, type, totalPrice)
Select Region, Product, 'TotalPriceA', Sum(Price)
From <your tables here>
Group By region, product
--Repeat for total B.
Insert Into @myData(region, product, type, totalPrice)
Select Region, Product, 'TotalPriceB', Sum(Price)
From <your tables here>
Group By region, product
--Now myData table has all the information.
--You only need the output format
Select region, product, TotalPriceA, TotalPriceB
From @myData
Pivot (
Sum(totalPrice)
For type In ('TotalPriceA', 'TotalPriceB')
) As Result
希望这对您有所帮助。如您所见,第 type
列中的常量值成为最终结果中的列标题。如果最终 table 中的一个“单元格”没有与该 row/column 匹配项对应的值,您将获得空值。
我有以下 2 个示例查询及其结果 tables(虚拟数据)如下:
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
GROUP BY subs.Region, subs.Product
Region | Product | TotalPriceA |
---|---|---|
USA | cameras | 200 |
USA | phones | 300 |
Canada | cameras | 300 |
Canada | phones | 500 |
SELECT
r.Region
,r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY r.Region, rp.Product
Region | Product | TotalPriceB |
---|---|---|
USA | cameras | 201 |
USA | phones | 301 |
Canada | cameras | 301 |
Canada | phones | 501 |
我想加入他们,这样结果 table 类似于:
Region | Product | TotalPriceA | TotalPriceB |
---|---|---|---|
USA | cameras | 200 | 201 |
USA | phones | 300 | 301 |
Canada | cameras | 300 | 301 |
Canada | phones | 500 | 501 |
但是当我使用这个查询时,我得到的结果 table 类似于:
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
,rptotal.TotalPriceB
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
JOIN
(
SELECT
r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY rp.Product
) rptotal on subs.Product = rptotal.Product
GROUP BY subs.Region, subs.Product, rptotal.TotalPriceB
Region | Product | TotalPriceA | TotalPriceB |
---|---|---|---|
USA | cameras | 200 | 502 |
USA | phones | 300 | 802 |
Canada | cameras | 300 | 502 |
Canada | phones | 500 | 802 |
当我也按区域对子查询进行分组时,我得到的结果更差...
你可以尝试在join
SELECT t1.Region,
t1.Product,
t2.TotalPriceA,
t1.TotalPriceB
FROM (
SELECT
r.Region
,r.Product
,SUM(rp.Price) TotalPriceB
FROM dbo.report_dtl r
JOIN dbo.report_price rp ON r.SubmissionNumber = rp.SubmissionNumber
GROUP BY r.Region, rp.Product
) t1 INNER JOIN (
SELECT
subs.Region
,subs.Product
,SUM(p.Price) TotalPriceA
FROM dbo.submission_dtl subs
JOIN dbo.price_dtl p ON subs.SubmissionNumber = p.SubmissionNumber
GROUP BY subs.Region, subs.Product
) t2 ON t1.Region = t2.Region AND t1.Product = t2.Product
也许分组依据不是这里所需要的,至少不是最终结果。您是否考虑过使用 pivot
子句?正如 DRapp 所述,您可能需要一个联合来组合这两个查询。您的 group by 只需要预先汇总总值,但 pivot 应该负责。
在此示例中,我使用 table 变量来合并所有信息,然后合并数据透视表。仔细看看,您会发现其中一列对于每个查询始终具有一个常量。此外,根据经验,我知道 table 变量与空列一起工作得更好,而不管实际数据源如何。
Declare @myData Table (
region varchar(max) null,
product varchar(max) null,
type varchar(max) null,
totalPriceA money null
)
--The type is the constant to know whether it's A or B
Insert Into @myData(region, product, type, totalPrice)
Select Region, Product, 'TotalPriceA', Sum(Price)
From <your tables here>
Group By region, product
--Repeat for total B.
Insert Into @myData(region, product, type, totalPrice)
Select Region, Product, 'TotalPriceB', Sum(Price)
From <your tables here>
Group By region, product
--Now myData table has all the information.
--You only need the output format
Select region, product, TotalPriceA, TotalPriceB
From @myData
Pivot (
Sum(totalPrice)
For type In ('TotalPriceA', 'TotalPriceB')
) As Result
希望这对您有所帮助。如您所见,第 type
列中的常量值成为最终结果中的列标题。如果最终 table 中的一个“单元格”没有与该 row/column 匹配项对应的值,您将获得空值。