如何在SQLServer中根据日期计算行数?

0kjbasz6  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(357)

我有日期表:

我想通过计算表上2019-06-30到2020-03-30之间的行来显示多少行?输出应该是10行。我试过了

  1. SELECT ln.datestart,
  2. ln.loanid,
  3. col.IDNo,
  4. ls.LoanID,
  5. max(CAST(col.DateOR AS date)) AS lastpayment,
  6. COUNT(ls.loanid) AS rowcount
  7. FROM Collections AS col
  8. INNER JOIN LoanSchedules AS ls ON ls.LoanID = col.IDNo
  9. INNER JOIN Loans AS ln ON ln.LoanID = ls.LoanID
  10. WHERE ls.DatePayment BETWEEN ln.DateStart AND max(col.DateOR)
  11. GROUP BY ln.loanid,
  12. col.IDNo,
  13. ls.LoanID,
  14. ln.datestart

但它会返回一个错误:

  1. 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.

附言: ln.datestart 来自记录每个客户的第一次付款计划的另一个表。我用 max(dateor) 从另一个表中知道最后的付款日期。

8wtpewkr

8wtpewkr1#

你必须使用 having 子句进行筛选时,

  1. select ls.DatePayment, ln.datestart,ln.loanid,col.IDNo, ls.LoanID, max(CAST(col.DateOR as date)) as
  2. lastpayment,
  3. COUNT(ls.loanid) as rowcount
  4. from Collections as col
  5. inner join LoanSchedules as ls
  6. on ls.LoanID = col.IDNo
  7. inner join Loans as ln
  8. on ln.LoanID = ls.LoanID
  9. group by ls.DatePayment, ln.loanid,col.IDNo,ls.LoanID,ln.datestart
  10. having ls.DatePayment between ln.DateStart and max(col.DateOR)
628mspwn

628mspwn2#

我不太清楚你想要得到什么结果,但我想你需要使用apply操作符。我试着复制你的案例,结果是:

  1. create table #LOANS(
  2. LoanId char(11) not null
  3. ,DatePayment date not null
  4. ,PrincipalPayment numeric(11,2) not null
  5. ,InterestPayment numeric(11,2) not null
  6. ,TotalPayment numeric(11,2) not null
  7. )
  8. insert into #LOANS (LoanId, DatePayment, PrincipalPayment, InterestPayment, TotalPayment)
  9. values ('B 1905.0005', '2019-06-30', '5833.33', '2106.67', '7490.00')
  10. ,('B 1905.0005', '2019-07-30', '5833.33', '2106.67', '7490.00')
  11. ,('B 1905.0005', '2019-08-30', '5833.33', '2106.67', '7490.00')
  12. ,('B 1905.0005', '2019-09-30', '5833.33', '2106.67', '7490.00')
  13. ,('B 1905.0005', '2019-10-30', '5833.33', '2106.67', '7490.00')
  14. ,('B 1905.0005', '2019-11-30', '5833.33', '2106.67', '7490.00')
  15. ,('B 1905.0005', '2019-12-30', '5833.33', '2106.67', '7490.00')
  16. ,('B 1905.0005', '2020-01-30', '5833.33', '2106.67', '7490.00')
  17. ,('B 1905.0005', '2020-02-29', '5833.33', '2106.67', '7490.00')
  18. ,('B 1905.0005', '2020-03-30', '5833.33', '2106.67', '7490.00')
  19. ,('B 1905.0005', '2020-04-30', '5833.33', '2106.67', '7490.00')
  20. ,('B 1905.0005', '2020-05-30', '5833.33', '2106.67', '7490.00')
  21. select *
  22. from #LOANS l
  23. outer apply(select COUNT(*) as rowNr
  24. from #LOANS ll
  25. where ll.LoanId = l.LoanId
  26. and ll.DatePayment between '2019-06-30' and '2020-03-30'
  27. group by LoanId) c

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

展开查看全部
5sxhfpxr

5sxhfpxr3#

你的逻辑很难理解。显示一个数据表,但查询引用三个表。不过,我很肯定,你可以用一个窗口函数来做你想做的事情。只是不清楚你想要哪个窗口功能。我想:

  1. SELECT datestart, loanid, cIDNo, LoanID,
  2. COUNT(*) AS rowcount
  3. FROM (SELECT ln.datestart, ln.loanid, col.IDNo, ls.LoanID, ls.DatePayment,
  4. MAX(CAST(col.DateOR AS date)) OVER (PARTITION BY ln.datestart, ln.loanid, col.IDNo, ls.LoanID) as max_DateOR
  5. FROM Collections col JOIN
  6. LoanSchedules ls
  7. ON ls.LoanID = col.IDNo JOIN
  8. Loans ln
  9. ON ln.LoanID = ls.LoanID
  10. ) x
  11. WHERE DatePayment BETWEEN ln.DateStart AND max_DateOR
  12. GROUP BY loanid, IDNo, LoanID, datestart;

这假设您正在寻找基于四个参数的最大值 group by 钥匙。

相关问题