[英]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.