[英]SQL Server : find dates between two separate row entries
I am trying to pull a list of AccountNumbers
that have 2 specific charges (by code) that are at least 2 days apart. 我试图提取一个
AccountNumbers
列表,该列表具有2个特定的费用(按代码),至少相隔2天。 These are the columns of my table: 这些是我表的列:
Example: if there is AccountNumber
for Code = 33967
on ServiceDate 12/11/2018
and an AccountNumber
for Code = 33968
on ServiceDate 12/15/2018
, the AccountNumber
will be output to the results window because these two instances show up on DIFFERENT ServiceDate
s and are at least 2 days apart. 例如:如果有
AccountNumber
为Code = 33967
上ServiceDate 12/11/2018
和AccountNumber
为Code = 33968
上ServiceDate 12/15/2018
,该AccountNumber
将被输出到结果窗口,因为这两个实例显示在不同ServiceDate
,且相隔至少2天。
Example 2: if there is an AccountNumber
for Code = 33967
on ServiceDate
12/11/2018 and an AccountNumber
for Code = 33968
on ServiceDate
12/11/2018, the AccountNumber
will NOT be output to the results window because these two instances show up on the same ServiceDate
. 实施例2:如果有一个
AccountNumber
为Code = 33967
上ServiceDate
2018年12月11日和AccountNumber
为Code = 33968
上ServiceDate
2018年12月11日,该AccountNumber
将不被输出到结果窗口,因为这两个实例显示在同一ServiceDate
。
Example 3: if there is an AccountNumber
for Code = 33967
on ServiceDate
12/11/2018 and an AccountNumber
for Code = 33968
on ServiceDate 12/12/2018
, the AccountNumber
will NOT be output to the results window because there are no dates between the two ServiceDate
's. 实施例3:如果有一个
AccountNumber
为Code = 33967
上ServiceDate
2018年12月11日和AccountNumber
为Code = 33968
上ServiceDate 12/12/2018
,该AccountNumber
将不被输出到结果窗口,因为有之间没有日期这两个ServiceDate
。 However if it were 12/11 and 12/13 it would be acceptable because there is a day in-between. 但是,如果它是12/11和12/13,则可以接受,因为中间有一天。
I am only concerned about Code 33967 and 33968, all other codes should not be considered. 我只关心代码33967和33968,不应考虑所有其他代码。 Right now, I am able to pull all Accounts with both these codes on file but cannot figure out how to go further.
现在,我能够提取所有带有这两个代码的帐户,但无法弄清楚该怎么做。 Any ideas?
有任何想法吗?
My code is as follows: 我的代码如下:
SELECT AccountNumber, ServiceDate
FROM dbo.table
WHERE Code = '33968'
INTERSECT
SELECT AccountNumber, ServiceDate
FROM dbo.table
WHERE Code = '33967'
here you go with some sample data. 在这里,您将获得一些示例数据。 Feel free to add some more rows and test it up
随意添加更多行并进行测试
create table #Temp_table
(
AccountNumber int null
, ServiceDate date null
,Code int null
)
insert into #Temp_table values
(1,'12/11/2018',33967)
,(2,'12/15/2018',33968)
,(3,'12/11/2018',33967)
,(4,'12/12/2018',33968)
,(5,'12/17/2018',33968)
,(6,'12/16/2018',33967)
;with CTE_MinDate as (
select --Code ,
MinServiceDate = min(Servicedate)
from #Temp_table
--group by Code
)
--select * from CTE_MinDate
select *
from (
select *
,Days_Diff = datediff(day,MinServiceDate, Servicedate)
from (
select a.*
,MinServiceDate = (select MinServiceDate from CTE_MinDate)
from #Temp_table a
where a.Code in ( 33967,33968)
) a
) b where Days_Diff >= 2
Does this do what you want? 这是您想要的吗?
SELECT DISTINCT t.AccountNumber
FROM dbo.table t
WHERE t.Code = '33968' AND
EXISTS (SELECT 1
FROM dbo.table t2
WHERE t2.AccountNumber = t.AccountNumber AND
t2.Code = '33967' AND
t2.ServiceDate <> t.ServiceDate
);
If it's only the Account Number you want then how about: 如果仅是您想要的帐号,那么如何:
SELECT AccountNumber
FROM dbo.[Table]
GROUP BY AccountNumber
HAVING COUNT(CASE CODE WHEN 33968 THEN 1 END) > 0
AND COUNT(CASE CODE WHEN 33967 THEN 1 END) > 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.