【问题标题】:How to combine data from different tables using a query如何使用查询组合来自不同表的数据
【发布时间】:2013-12-17 16:31:48
【问题描述】:

我有一组如下所示的表格。

您能告诉我如何获得所需的输出吗?

CREATE TABLE #ABC([Year] INT, [Month] INT,Customer Varchar(10), SalesofProductA INT);
CREATE TABLE #DEF([Year] INT, [Month] INT,Customer Varchar(10), SalesofProductB INT);
CREATE TABLE #GHI([Year] INT, [Month] INT,Customer Varchar(10), SalesofProductC INT);

INSERT #ABC VALUES (2013,1,'PPP',1);
INSERT #ABC VALUES (2013,1,'QQQ',2);
INSERT #ABC VALUES (2013,2,'PPP',3);

INSERT #DEF VALUES (2013,1,'QQQ',4);
INSERT #DEF VALUES (2013,1,'RRR',5);
INSERT #DEF VALUES (2013,2,'PPP',6);

INSERT #GHI VALUES (2013,1,'QQQ',7);
INSERT #GHI VALUES (2013,2,'RRR',8);
INSERT #GHI VALUES (2013,3,'PPP',9);
   INSERT #GHI VALUES (2013,3,'QQQ',10);

我目前有一个看起来像这样的查询。 @Month 和 @Year 作为参数提供

SELECT
    -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run)
    (SELECT SUM(SalesofProductA) FROM #ABC WHERE [Year]=T.[Year] AND [Month]=T.[Month]) AS [Sum_SalesofProductA]
    ,(SELECT SUM(SalesofProductB) FROM #DEF WHERE [Year]=T.[Year] AND [Month]=T.[Month]) AS [Sum_SalesofProductB]
    ,(SELECT SUM(SalesofProductC) FROM #GHI WHERE [Year]=T.[Year] AND [Month]=T.[Month]) AS [Sum_SalesofProductC]
FROM (
    -- this selects a list of all possible dates.
    SELECT [Year],[Month] FROM #ABC
    where Year = @Year and Month = @Month
    UNION
    SELECT [Year],[Month] FROM #DEF
    where Year = @Year and Month = @Month
     UNION
    SELECT [Year],[Month] FROM #GHI
    where Year = @Year and Month = @Month
) AS T;

现在我看到这样的输出:@Month 和 @Year 的特定值

 SalesofProductA, SalesofProductB, SalesofProductC 

我想看到的是:

[Customer],SalesofProductA, SalesofProductB, SalesofProductC 

有人知道怎么做吗?

【问题讨论】:

  • 为什么用三个版本的 SQL Server 标记这个问题?您是否正在为这三者中的每一个寻找解决方案?

标签: sql-server sql-server-2008 tsql sql-server-2008-r2 sql-server-2012


【解决方案1】:
SELECT Customer 
          , SUM(COALESCE(P.[ProductA], 0)) AS [ProductA] 
          , SUM(COALESCE(P.[ProductB], 0)) AS [ProductB] 
          , SUM(COALESCE(P.[ProductC], 0)) AS [ProductC] 
FROM
(
SELECT Customer , SUM(SalesofProductA) AS  Sales, 'ProductA' AS ProductName 
FROM #ABC 
GROUP BY Customer
UNION ALL 
SELECT Customer , SUM(SalesofProductB) AS  Sales, 'ProductB' AS ProductName 
FROM #DEF 
GROUP BY Customer
UNION ALL 
SELECT Customer , SUM(SalesofProductC) AS  Sales, 'ProductC' AS ProductName 
FROM #GHI 
GROUP BY Customer
) Q
   PIVOT ( SUM(Sales) 
           FOR ProductName
           IN ([ProductA], [ProductB], [ProductC])
           )P
GROUP BY  Customer

结果

Customer    ProductA    ProductB    ProductC
PPP             4          6           9
QQQ             2          4           17
RRR             0          5           8

【讨论】:

  • 我很抱歉。我刚刚再次编辑了我的问题。我第一次做错了。我正在寻找特定月份和年份的 Customer 、 ProductA、ProductB 和 ProductC ..
  • 我相信您需要在 @Year@Month 某处添加过滤器。此外,由于 PIVOT 已经完成了分组,因此无需在外部查询中显式分组 Customer
猜你喜欢
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多