1

我在数据库中有 2 个表,基本上是当前销售额 (dbo_SalesItem) 和历史销售额 (dbo_SalesItemHistory)。我有一个查询来从每个表中获取唯一供应商名称的计数,但是如何将它们连接在一起以从两个查询中获得一个总数?

表中的字段是相同的。

SELECT dbo_SalesItem.Vendor AS [Vendor ID], dbo_Vendor.Vendor_Name AS [Vendor Name], Count(*) AS [Count]
FROM (dbo_SalesDoc INNER JOIN dbo_SalesItem ON dbo_SalesDoc.Sales_Doc_Num = dbo_SalesItem.Sales_Doc_Num) INNER JOIN dbo_Vendor ON dbo_SalesItem.Vendor = dbo_Vendor.Vendor_ID
WHERE (((dbo_SalesDoc.Hold_Code)="ITEMLINK" Or (dbo_SalesDoc.Hold_Code)="TKTLINK") AND ((dbo_SalesDoc.Sales_Doc_Num) Like "ORD*"))
GROUP BY dbo_SalesItem.Vendor, dbo_Vendor.Vendor_Name, dbo_SalesDoc.Last_Update_On
HAVING (((dbo_SalesDoc.Last_Update_On)>=DateAdd("d",-90,Date())))
ORDER BY Count(*) DESC;

SELECT dbo_SalesItemHistory.Vendor AS [Vendor ID], dbo_Vendor.Vendor_Name AS [Vendor Name], Count(*) AS [Count]
FROM (dbo_SalesDoc INNER JOIN dbo_SalesItemHistory ON dbo_SalesDoc.Sales_Doc_Num = dbo_SalesItemHistory.Sales_Doc_Num) INNER JOIN dbo_Vendor ON dbo_SalesItemHistory.Vendor = dbo_Vendor.Vendor_ID
WHERE (((dbo_SalesDoc.Hold_Code)="ITEMLINK" Or (dbo_SalesDoc.Hold_Code)="TKTLINK") AND ((dbo_SalesDoc.Sales_Doc_Num) Like "ORD*"))
GROUP BY dbo_SalesItemHistory.Vendor, dbo_Vendor.Vendor_Name, dbo_SalesDoc.Last_Update_On
HAVING (((dbo_SalesDoc.Last_Update_On)>=DateAdd("d",-90,Date())))
ORDER BY Count(*) DESC;

查询的结果如下所示:

Vendor ID    Vendor Name    Count
12345        Big Vendor     1200
23453        Big Vendor      900
43498        Other Vendor    600
98342        This Vendor     500
08943        Small Vendor    100

谢谢!

4

1 回答 1

2

UNION ALL对2 个现有查询使用子查询。不过,放弃他们的ORDER BY条款。然后父查询可以是GROUP BY总计每个供应商的计数。

SELECT
    sub.[Vendor ID],
    sub.[Vendor Name],
    Sum(sub.Count) As SumOfCount
FROM
    (
        SELECT dbo_SalesItem.Vendor AS [Vendor ID], dbo_Vendor.Vendor_Name AS [Vendor Name], Count(*) AS [Count]
        FROM (dbo_SalesDoc INNER JOIN dbo_SalesItem ON dbo_SalesDoc.Sales_Doc_Num = dbo_SalesItem.Sales_Doc_Num) INNER JOIN dbo_Vendor ON dbo_SalesItem.Vendor = dbo_Vendor.Vendor_ID
        WHERE (((dbo_SalesDoc.Hold_Code)="ITEMLINK" Or (dbo_SalesDoc.Hold_Code)="TKTLINK") AND ((dbo_SalesDoc.Sales_Doc_Num) Like "ORD*"))
        GROUP BY dbo_SalesItem.Vendor, dbo_Vendor.Vendor_Name, dbo_SalesDoc.Last_Update_On
        HAVING (((dbo_SalesDoc.Last_Update_On)>=DateAdd("d",-90,Date())))
        UNION ALL
        SELECT dbo_SalesItemHistory.Vendor AS [Vendor ID], dbo_Vendor.Vendor_Name AS [Vendor Name], Count(*) AS [Count]
        FROM (dbo_SalesDoc INNER JOIN dbo_SalesItemHistory ON dbo_SalesDoc.Sales_Doc_Num = dbo_SalesItemHistory.Sales_Doc_Num) INNER JOIN dbo_Vendor ON dbo_SalesItemHistory.Vendor = dbo_Vendor.Vendor_ID
        WHERE (((dbo_SalesDoc.Hold_Code)="ITEMLINK" Or (dbo_SalesDoc.Hold_Code)="TKTLINK") AND ((dbo_SalesDoc.Sales_Doc_Num) Like "ORD*"))
        GROUP BY dbo_SalesItemHistory.Vendor, dbo_Vendor.Vendor_Name, dbo_SalesDoc.Last_Update_On
        HAVING (((dbo_SalesDoc.Last_Update_On)>=DateAdd("d",-90,Date())))
    ) AS sub
GROUP BY
    sub.[Vendor ID],
    sub.[Vendor Name]
ORDER BY 3 DESC;
于 2013-05-22T15:08:55.123 回答