1

我有两个表,#Test并且#Control,它们的列彼此相同,看起来如下所示:

#Test:                        #Control:
Name  Component  Price        Name  Component  Price
A     a          1.00         A     a          7.00
A     b          2.00         A     a          8.00
A     a          3.00         B     a          9.00
B     a          4.00         B     d          10.00
B     a          5.00         B     d          11.00
B     c          6.00     

但是有更多的列和 和 的Name组合Component

我想将它们每个聚合以获得PriceperName和的总和Component,但是我想要出现在任一列表中的所有组合的值。使用上面的示例表所需的输出如下所示:

#TestAgg:                        #ControlAgg:
Name  Component  SumPrice        Name  Component  SumPrice
A     a          4.00            A     a          15.00
A     b          2.00            A     b          0.00
B     a          9.00            B     a          9.00
B     c          6.00            B     c          0.00
B     d          0.00            B     d          21.00

我怎样才能做到这一点?

对于单个表,以下工作:

SELECT Name
       ,Component
       ,sum(Price) as SumPrice                  
INTO #TestAgg
FROM #Test
GROUP BY rollup(Name,Component)
order by 1, SumPrice desc 

但是,我无法弄清楚如何为仅存在于另一个表中的名称组件组合返回零。

4

1 回答 1

1

你可以试试这个:

CREATE TABlE #Test(Name VARCHAR(1), Component VARCHAR(1),  Price DECIMAL(14,4));
INSERT INTO #Test VALUES
 ('A','a',1.00)    
,('A','b',2.00)    
,('A','a',3.00)   
,('B','a',4.00)    
,('B','a',5.00)    
,('B','c',6.00);     

CREATE TABlE #Control(Name VARCHAR(1), Component VARCHAR(1),  Price DECIMAL(14,4));
INSERT INTO #Control VALUES
 ('A','a',7.00)
,('A','a',8.00)
,('B','a',9.00)
,('B','d',10.00)
,('B','d',11.00);

--首先我使用 CTE 来获得两个表的所有组合的不同列表

WITH AllCombos AS
(
    SELECT DISTINCT Name,Component
    FROM #Test
    UNION --without "ALL" it will be distinct over the tables
    SELECT DISTINCT Name,Component
    FROM #Control
)

--现在我使用LEFT JOINs 来获取两个结果集 --并使用 finalGROUP BY

SELECT Source,Name,Component,ISNULL(SUM(Price),0) AS Price
FROM
(
    SELECT 'Test' AS Source, AC.Name,AC.Component,T.Price
    FROM AllCombos AS AC
    LEFT JOIN #Test AS T ON AC.Component=T.Component AND AC.Name=T.Name
    UNION ALL
    SELECT 'Control',AC.Name,AC.Component,C.Price
    FROM AllCombos AS AC
    LEFT JOIN #Control AS C ON AC.Component=C.Component AND AC.Name=C.Name
) AS tbl
GROUP BY Source,Name,Component

--Clean-up
GO
DROP TABLE #Test;
DROP TABLE #Control;

结果

Control A   a   15.0000
Control A   b   0.0000
Control B   a   9.0000
Control B   c   0.0000
Control B   d   21.0000
Test    A   a   4.0000
Test    A   b   2.0000
Test    B   a   9.0000
Test    B   c   6.0000
Test    B   d   0.0000

更新

如果你真的需要两张表,你可以写

SELECT ... INTO #ControlAgg 
FROM (...) AS tbl 
WHERE Source='Control' 
GROUP BY ...

(与测试相同)

...并调用它两次...或者-在我看来更好-您将其写在一个 commong 表中并Source在查询中使用将它们分开...

于 2016-08-02T00:04:06.560 回答