1

私は SQL を書くのが得意ではありません。これを Access で書き、SQL ビューに移動しました。両方のクエリを取得し、それらの間でユニオンを実行しました。各クエリから合計を取得し続けます。各製品ラインの合計を 1 つだけ取得するにはどうすればよいですか?

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 に答える