2016-10-26 75 views
0

我寫了這個查詢,結果沒有被分組。它們被顯示爲這樣:查詢中的組數據

Facility | Posting Month | Account Type | Amount 
------------------------------------------------- 
Name  | July   | Debit Adj. | 100 
Name  | July   | Credit Adj. | -200 
Name  | July   | Debit Adj. | 150 
Name  | July   | Credit Adj. | -150 
------------------------------------------------- 

我試圖得到的結果是每月發佈爲借記調出現一次。和信貸調整。類型和相應的總金額。借方總計250美元,貸方350美元。謝謝您的幫助!

SELECT pw.Facility, DATENAME(mm, pw.[Posting Date]) AS [Posting Month], 
    lc.[Gl Account Type], sum(pw.[Tx Amt]) AS Amount 
FROM DBO.PaymentWOLedger AS pw 
    JOIN DBO.LedgerCodeTable AS lc 
     ON lc.[Description] = pw.[Tx Desc] 
WHERE lc.[Gl Account Type] = 'Credit Adjustment (Write Off)' OR 
     lc.[GlAccount Type] = 'Debit Adjustment (CWO)' 
GROUP BY pw.[Posting Date], pw.Facility, lc.[Gl Account Type] 
ORDER BY pw.Facility, pw.[Posting Date] 

回答

2

使用Month而不是DateGroup by & Order by

GROUP BY DATENAME(mm, pw.[Posting Date]), pw.Facility, lc.[Gl Account Type] 
ORDER BY pw.Facility, DATENAME(mm, pw.[Posting Date]) 

目前,因爲你已經在Group by這有它date/month/year部分使用pw.[Posting Date]數據在每個date分組。

+0

太棒了!現在有道理。感謝您的幫助! – Tosh