1

我不是最擅长编写 SQL 的。我在 Access 中写了这个,然后转到 SQL 视图。接受了两个查询并在它们之间进行了联合。我不断从每个查询中得到一个总数。我如何只为每个产品线获得一个总数。

Results:
Prod Line           Period             Amount
Cash Discounts  12            -1404010.46
CASH DISCOUNTS  12                1541.19
Freight           12               4050823.43
Freight           12                  6817.27
INK           12                -24467.76
INK           12                 44414.29

Want        
Cash discounts                          -1402469.27
Freight                                   405764.70
INK                                        24467.76

SQL

SELECT [JE Details].[Prod Line], [JE Details].Period, Sum([JE Details].Amount) AS Amount
FROM [JE Details]
GROUP BY [JE Details].[Prod Line], [JE Details].Period
HAVING ((([JE Details].Period)=12))
UNION ALL 
SELECT [AP Details].[Product Line], [AP Details].[Fiscal Period], Sum([AP Details].
[Invoice Amt]) AS Amount
FROM [AP Details]
GROUP BY [AP Details].[Product Line], [AP Details].[Fiscal Period]
HAVING ((([AP Details].[Fiscal Period])=12));    
4

1 回答 1

2

怎么样:

SELECT [Prod Line], Sum(Amount) 
FROM 
   (SELECT [Prod Line], Amount
    FROM [JE Details] 
    WHERE Period=12
    UNION ALL 
    SELECT [Product Line] ,[Invoice Amt]
    FROM [AP Details] 
    WHERE [Fiscal Period]=12) q
GROUP BY [Product Line]
于 2013-02-22T22:28:07.737 回答