我有一个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个月前的声明期间的帐户及其数据列表。但是这些帐户也有后续陈述的数据。
我需要自6个月前至少没有收到任何陈述的账户。
编辑:**** 帐户表中使用的唯一字段是 1. AccountId 2)AccountNumber
StatementSummary表中使用的字段是 1. AccountId 2. InvoiceNumber 3. StatementDate 4. NewCharges 5. AmountDue
每月和每月生成一份声明,该帐户将产生新的费用。
我需要最后声明中的详细信息,其中:
1)最后一个StatementDate是在6个月之前和之前 2)AmountDue的价格至少为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
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
使用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