2016-09-16 135 views
0

我正在嘗試生成一份報告,顯示我們每個季度的案件數量和存在的貨幣價值。 問題是我可以計算每個季度的案件數量,但貸款金額是一個掙扎。這裏總額日期範圍每季度

SELECT 
COUNT(case when CONVERT(DATE,p.ProjectEndEstimate,103) BETWEEN GETDATE() AND DATEADD(Day , +91,GETDATE()) then 1 end) AS [1st Quater], 
SUM(c.LoanAmount) AS [1st Quater LoanAmount] , 
COUNT(case when CONVERT(DATE,p.ProjectEndEstimate,103) BETWEEN DATEADD(Day , +92,GETDATE()) AND DATEADD(Day , +183,GETDATE()) then 1 end) AS [2nd Quater], 
SUM(c.LoanAmount) AS [2nd Quater LoanAmount] , 
COUNT(case when CONVERT(DATE,p.ProjectEndEstimate,103) BETWEEN DATEADD(Day , +184,GETDATE()) AND DATEADD(Day , +275,GETDATE()) then 1 end) AS [3rd Quater], 
SUM(c.LoanAmount) AS [3rd Quater LoanAmount] , 
COUNT(case when CONVERT(DATE,p.ProjectEndEstimate,103) BETWEEN DATEADD(Day , +276,GETDATE()) AND DATEADD(Day , +366,GETDATE()) then 1 end) AS [4th Quater], 
SUM(c.LoanAmount) AS [4th Quater LoanAmount] 
FROM [dbo].[tbl_Projects] AS p 
INNER JOIN tbl_CaseDetails AS c 
ON p.PK_ProjectID = c.FK_ProjectID 
INNER JOIN [dbo].[tbl_Lenders] AS l 
ON l.PK_CompanyID = c.LenderID 
WHERE l.PK_CompanyID = @Lender 

感謝

更新是輸出應該是具有正確正確的貸款額度是什麼。

1st Quater 1st Quater LoanAmount 2nd Quater 2nd Quater LoanAmount 3rd Quater 3rd Quater LoanAmount 4th Quater 4th Quater LoanAmount 
309   2068822879.50   223   2068822879.50   64   2068822879.50   21   2068822879.50 
+0

我們展示了一些示例數據和預期的結果。請閱讀[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/),瞭解如何提高您的問題質量並獲得更好的答案。 –

回答

1

只要使用相同CASECOUNTSUM

COUNT(case WHEN CONVERT(DATE,p.ProjectEndEstimate,103) 
       BETWEEN GETDATE() 
       AND DATEADD(Day , +91,GETDATE()) 
      THEN 1 
     end) AS [1st Quater], 
SUM(case WHEN CONVERT(DATE,p.ProjectEndEstimate,103) 
       BETWEEN GETDATE() 
       AND DATEADD(Day , +91,GETDATE()) 
     THEN c.LoanAmount 
     ELSE 0 
     end) AS [1st Quater], 
+0

作品感謝分配 – RustyHamster