[英]How to get records with top dates in SQL Server 2014?
我有一个Account表和一个StatementSummary表。
StatementSummary表保存每个月每个月发送的对帐单的数据。 但是有时候,由于某种原因,对帐单持有人不会发出对帐单。
我需要以下帐户列表:
我的代码:
SELECT A.AccountNumber
,StatementDate
--,MAX(StatementDate)
,AmountDue
,InvoiceNumber
FROM [StatementSummary] S
INNER JOIN Account A ON S.AccountID = A.AccountId
WHERE AmountDue >= 1000
AND StatementDate <= '2017-02-10' --Should be (sysdate-180)
--GROUP BY A.AccountNumber, StatementDate
ORDER BY StatementDate DESC
此代码为我提供了6个月前报表期间的帐户及其数据的列表。 但是这些帐户也具有以后的报表数据。
我需要至少六个月前未收到任何对帐单的帐户。
编辑: ****
帐户表中使用的唯一字段是
1. AccountId
2)帐号
StatementSummary表中使用的字段是
1. AccountId
2.发票编号
3. StatementDate
4.新收费
5. AmountDue
每个月都会产生一个对帐单,该帐户会产生新的费用。
我需要LAST语句的详细信息,其中:
1)最后的StatementDate在6个月前,并且
2)金额超过$ 1000的金额
样本数据:
AccountNumber StatementDate AmountDue InvoiceNumber
32563696 2017-07-16 1259.05 2279250276
32563696 2017-06-16 1043.00 2273976792
32563696 2017-05-16 974.00 2273976651
32067247 2017-07-01 5385.84 2277258801
32067247 2017-06-01 4218.71 2271971177
32067247 2017-05-01 2977.56 2276955130
32067247 2017-04-01 1518.85 2274063149
31279191 2017-06-01 214746.49 2271930486
31279191 2017-05-01 184178.38 2276913639
31279191 2017-04-01 141984.13 2274025518
31279191 2017-03-01 110914.52 2270228069
31279191 2017-02-01 76083.25 2257406893
31279191 2017-01-01 45997.75 2253462824
我真正需要的是:
AccountNumber StatementDate AmountDue InvoiceNumber
11201057 2017-02-01 9114.29 2255223280
11201147 2017-02-01 1189.52 2255223235
11203824 2017-02-01 8984.36 2255223819
11206052 2017-01-01 2274.54 2255223381
11206298 2017-01-01 5792.11 2255223358
11208852 2016-12-01 2175.62 2255223987
11209202 2016-12-01 1199.58 2255223976
11209246 2016-12-01 1017.12 2255256003
11209268 2016-11-01 1775.32 2255256025
因此,用简单的话来说:我只需要一个帐户及其数据列表,仅在LAST语句在6个月前发送的情况下,并且在AmountDue为$ 1000或更多的情况下也是如此。
我认为应该根据您发布的内容执行此操作。 虽然样本数据会有所帮助。
WITH CTE AS(
SELECT A.AccountNumber
,StatementDate
--,MAX(StatementDate)
,AmountDue
,InvoiceNumber
,row_number() over (partition by A.AccountNumber order by StatementDate desc) as RN
FROM [StatementSummary] S
INNER JOIN Account A ON S.AccountID = A.AccountId
WHERE
s.AccountID in (select AccountID from StatementSummary group by AccountID having Max(StatementDate) < dateadd(day,-180,getdate()))
and s.AmountDue >= 1000)
select
AccountNumber
,StatementDate
,AmountDue
,InvoiceNumber
from CTE
where
RN = 1
使用not exists()
:
SELECT A.AccountNumber
,S.StatementDate
,AmountDue
,InvoiceNumber
FROM [StatementSummary] S
INNER JOIN Account A ON S.AccountID = A.AccountId
WHERE AmountDue >= 1000
and not exists (
select 1
from [StatementSummary] i
where i.AccountId = a.AccountId
and i.StatementDate > dateadd(month,-6,getdate())
/* other option based on comment */
and i.StatementDate > dateadd(day,-180,getdate())
)
ORDER BY StatementDate DESC
inner join
选项,用于在最近6个月内未发送对帐单时获取帐户的最新对帐单:
SELECT A.AccountNumber
,S.StatementDate
,AmountDue
,InvoiceNumber
FROM [StatementSummary] S
INNER JOIN Account A
ON S.AccountID = A.AccountId
inner join (
select
AccountId
, MaxStatementDate = max(StatementDate)
from StatementSummary i
group by i.AccountId
having max(StatementDate) <= dateadd(month,-6,getdate())
) x on s.AccountId = x.AccountId
and s.StatementDate = x.MaxStatementDate
WHERE AmountDue >= 1000
ORDER BY StatementDate DESC
SELECT A.AccountNumber
,StatementDate
--,MAX(StatementDate)
,AmountDue
,InvoiceNumber
FROM [StatementSummary] S
INNER JOIN Account A ON S.AccountID = A.AccountId
WHERE A.AmountDue >= 1000
AND S.StatementDate <= '2017-02-10'
-也许两个表的名称都相同?
使用此查询获取您6个月的案例记录:
Select * From [StatementSummary]
Where datediff(DAY, GETDATE(), StatementDate) Between -180 AND -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.