簡體   English   中英

如何在 SQL 服務器中根據日期計算行數?

[英]How to count rows based on date in SQL Server?

我有日期表:

在此處輸入圖像描述

我想根據表格計算 2019-06-30 到 2020-03-30 之間的行數來顯示多少行? output 應該是 10 行。 我試過了

SELECT ln.datestart,
       ln.loanid,
       col.IDNo,
       ls.LoanID,
       max(CAST(col.DateOR AS date)) AS lastpayment,
       COUNT(ls.loanid) AS rowcount
FROM Collections AS col
INNER JOIN LoanSchedules AS ls ON ls.LoanID = col.IDNo
INNER JOIN Loans AS ln ON ln.LoanID = ls.LoanID
WHERE ls.DatePayment BETWEEN ln.DateStart AND max(col.DateOR)
GROUP BY ln.loanid,
         col.IDNo,
         ls.LoanID,
         ln.datestart

但它返回一個錯誤說:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

ps: ln.datestart來自另一個表,記錄了每個客戶的第一個付款時間表。 我使用另一個表中的max(dateor)來了解付款的最后日期。

在使用聚合列進行過濾時,您必須使用having子句,

select ls.DatePayment, ln.datestart,ln.loanid,col.IDNo, ls.LoanID, max(CAST(col.DateOR as date)) as 
lastpayment,
COUNT(ls.loanid) as rowcount

from Collections as col
inner join LoanSchedules as ls 
on ls.LoanID = col.IDNo
inner join Loans as ln
on ln.LoanID = ls.LoanID
group by ls.DatePayment, ln.loanid,col.IDNo,ls.LoanID,ln.datestart
having ls.DatePayment between ln.DateStart and max(col.DateOR)

我不清楚你想要達到的結果是什么,但我想你需要使用 apply 運算符。 我試圖重現你的情況,在這里你得到了結果:

create table #LOANS(
             LoanId           char(11) not null
            ,DatePayment      date not null
            ,PrincipalPayment numeric(11,2) not null
            ,InterestPayment  numeric(11,2) not null
            ,TotalPayment     numeric(11,2) not null
)

insert into #LOANS (LoanId, DatePayment, PrincipalPayment, InterestPayment, TotalPayment)
            values ('B 1905.0005', '2019-06-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-07-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-08-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-09-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-10-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-11-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2019-12-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2020-01-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2020-02-29', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2020-03-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2020-04-30', '5833.33', '2106.67', '7490.00')
                  ,('B 1905.0005', '2020-05-30', '5833.33', '2106.67', '7490.00')

select *
from   #LOANS l
       outer apply(select   COUNT(*) as rowNr
                   from     #LOANS ll
                   where    ll.LoanId = l.LoanId
                        and ll.DatePayment between '2019-06-30' and '2020-03-30'
                   group by LoanId) c

https://i.stack.imgur.com/9hxpc.png

你的邏輯很難理解。 您顯示一張數據表,但您的查詢引用了三個表。 不過,我很確定,您可以使用 window function 來做您想做的事。 目前尚不清楚您想要哪個window function。 我認為:

SELECT datestart, loanid, cIDNo, LoanID,
       COUNT(*) AS rowcount
FROM (SELECT ln.datestart, ln.loanid, col.IDNo, ls.LoanID, ls.DatePayment,
             MAX(CAST(col.DateOR AS date)) OVER (PARTITION BY ln.datestart, ln.loanid, col.IDNo, ls.LoanID) as max_DateOR
      FROM Collections col JOIN
           LoanSchedules ls
           ON ls.LoanID = col.IDNo JOIN
           Loans ln
           ON ln.LoanID = ls.LoanID
     ) x
WHERE DatePayment BETWEEN ln.DateStart AND max_DateOR
GROUP BY loanid, IDNo, LoanID, datestart;

這假設您正在尋找基於四個group by鍵的最大值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM