[英]How to get records with top dates in SQL Server 2014?
I have an Account table and a StatementSummary table. 我有一个Account表和一个StatementSummary表。
The StatementSummary table holds data for statements sent out every month for every account. StatementSummary表保存每个月每个月发送的对帐单的数据。 But sometimes, for some reason, statements don't go out to the account holders. 但是有时候,由于某种原因,对帐单持有人不会发出对帐单。
I need a list of accounts where: 我需要以下帐户列表:
My code: 我的代码:
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
This code gives me a list of accounts and their data for the statement period before 6 months ago. 此代码为我提供了6个月前报表期间的帐户及其数据的列表。 But these accounts have data for later statements as well. 但是这些帐户也具有以后的报表数据。
I need accounts who have NOT received any statements since 6 months ago at least. 我需要至少六个月前未收到任何对帐单的帐户。
EDIT: **** 编辑: ****
The only fields used from the Account Table are 帐户表中使用的唯一字段是
1. AccountId 1. AccountId
2) AccountNumber 2)帐号
The fields in the StatementSummary Table used are StatementSummary表中使用的字段是
1. AccountId 1. AccountId
2. InvoiceNumber 2.发票编号
3. StatementDate 3. StatementDate
4. NewCharges 4.新收费
5. AmountDue 5. AmountDue
A Statement is generated every month and every month, new charges are incurred on that account. 每个月都会产生一个对帐单,该帐户会产生新的费用。
I need the details form the LAST Statement where: 我需要LAST语句的详细信息,其中:
1) the last StatementDate is before 6 months ago and 1)最后的StatementDate在6个月前,并且
2) where the AmountDue is more at least $1000 2)金额超过$ 1000的金额
Sample Data: 样本数据:
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
What I really need: 我真正需要的是:
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
So, in plain words: I need a list of accounts and their data ONLY where the LAST statement sent for more than 6 months ago and also ONLY WHERE the AmountDue is $1000 or more. 因此,用简单的话来说:我只需要一个帐户及其数据列表,仅在LAST语句在6个月前发送的情况下,并且在AmountDue为$ 1000或更多的情况下也是如此。
I think this should do it, based off what you have posted. 我认为应该根据您发布的内容执行此操作。 Though sample data would help. 虽然样本数据会有所帮助。
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
using not exists()
: 使用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
option to get the latest statement for an account when no statement has been sent within the last 6 months: 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'
--Maybe both tables have same columns' name? -也许两个表的名称都相同?
Use this query to get your 6 months case records: 使用此查询获取您6个月的案例记录:
Select * From [StatementSummary]
Where datediff(DAY, GETDATE(), StatementDate) Between -180 AND -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.