日期范围季度的总和金额

时间:2016-09-16 13:38:14

标签: sql tsql sum date-range

我正在尝试制作一份报告,显示我们每季度的案件数量以及货币价值。 问题是我可以计算出每季度的案件数量,但贷款金额是一个很大的问题。

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 

1 个答案:

答案 0 :(得分:1)

只需使用CASE COUNT中的SUM作为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],

$http.get("/displayPdf", {responseType: 'arraybuffer', params: {id: 1}}).success(function(data) {
    var blob = new Blob([data], {type 'application/pdf'});
    var anchor = document.createElement("a");
    if(navigator.userAgent.indexOf("Chrome") != -1||navigator.userAgent.indexOf("Opera") != -1){
                    $window.open(URL.createObjectURL(file,{oneTimeOnly:true}));
    }else if(navigator.userAgent.indexOf("iPad") != -1){
                    var fileURL = URL.createObjectURL(file);
                    //var anchor = document.createElement("a");
                    anchor.download="myPDF.pdf";
                    anchor.href = fileURL;
                    anchor.click();
    }else if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1){
                        var url = window.URL.createObjectURL(file);
                        anchor.href = url;
                        anchor.download = "myPDF.pdf";
                        document.body.appendChild(anchor);
                        anchor.click();
                        setTimeout(function(){
                            document.body.removeChild(anchor);
                            window.URL.revokeObjectURL(url);  
                        }, 1000);
      }
   });