1

我有 2 个查询要合并为一个。基本上我希望 2 个查询在一个输出表中......输出看起来像这样......</p>

 
商店 GC 已售汽车总数

1 22 75

2 24 88

3 15 89

问题是我不知道如何将它放在同一张桌子上。

这是我的疑问……</p>

SELECT        Store_Number, COUNT_BIG(Quantity_Sold) AS GC Sold
FROM            Invoice_Detail_Tb
WHERE        (Invoice_Date BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND (JLI_Category_Code = 'gc') 
                         AND (Invoice_Detail_Code LIKE 'jlgc%') AND (Invoice_Detail_Type = 'Item')
GROUP BY Store_Number

这个输出是:

店铺编号 GC 已售

1 12

2 13

3 14
SELECT   Store_Number,
         SUM(Vehicle_Count) AS [Total_Cars]
FROM     Daily_Sales_Tb
WHERE    (Operations_Day BETWEEN CONVERT (DATETIME, @startdate, 102) AND CONVERT (DATETIME, @enddate, 102))
GROUP BY Store_Number;

输出:

店铺数量 总车次

1 7

2 8

3 9

另外,查询按设计工作。但是,如果我尝试将它们组合起来,它们是 2 个不同的聚合函数,所以它不起作用。

4

1 回答 1

6
SELECT A.Store_Number,A.GC_Sold,B.Total_Cars
FROM
(
SELECT        Store_Number, COUNT_BIG(Quantity_Sold) AS GC_Sold
FROM            Invoice_Detail_Tb
WHERE        (Invoice_Date BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND (JLI_Category_Code = 'gc') 
                         AND (Invoice_Detail_Code LIKE 'jlgc%') AND (Invoice_Detail_Type = 'Item')
GROUP BY Store_Number
) A,
(
SELECT   Store_Number,
         SUM(Vehicle_Count) AS [Total_Cars]
FROM     Daily_Sales_Tb
WHERE    (Operations_Day BETWEEN CONVERT (DATETIME, @startdate, 102) AND CONVERT (DATETIME, @enddate, 102))
GROUP BY Store_Number
) B
WHERE A.Store_Number=B.Store_Number;
于 2012-04-23T02:20:10.067 回答