1
我有2個查詢返回有關貿易債務人的結果。所有貿易債務人每月首次返還金額,30天后所有貿易債務人每月返還二次金額。SQL如何將2個查詢合併爲一個
1查詢
SELECT T2.Name AS Period,
SUM(T1.LineTotal) AS CurrentAmount,
MAX(T0.DocRate) AS ExchangeRate
FROM OINV T0 INNER JOIN
INV1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN
dbo.OFPR AS t2 ON T1.FinncPriod = T2.AbsEntry
WHERE T0.DocStatus = 'O' AND t0.DocDate BETWEEN '2007-01-01' AND '2007-12-01'
GROUP BY T2.Name
第二查詢
SELECT T2.Name AS Period,
SUM(T1.LineTotal) AS NonCurrentAmount,
MAX(T0.DocRate) AS ExchangeRate
FROM OINV T0 INNER JOIN
INV1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN
dbo.OFPR AS t2 ON T1.FinncPriod = T2.AbsEntry
WHERE T0.DocStatus = 'O' AND DATEDIFF(day,t0.DocDate,t0.DocDueDate)>30
AND t0.DocDate BETWEEN '2007-01-01' AND '2007-12-01'
GROUP BY T2.Name
如何合併2級以上的查詢爲一體,使得生成的查詢將返回結果4列; Period, CurrentAmount, NonCurrentAmount and exchange rate.
+1:除此之外,我會離開的't0.DocDate BETWEEN '2007-01-01' AND「2007 -12-01''在WHERE子句中應用於兩個查詢。第一個'SUM()'和'MAX()'不需要'CASE'語句。 – MatBailie 2012-08-13 10:49:30
@Dems:非常感謝,在查詢中更新 – Andomar 2012-08-13 10:49:59
非常感謝Andomar和Dems。 – 2012-08-13 10:52:16