要检查sql server中日期范围内存在的记录吗
我有以下要求, 样本数据:要检查sql server中日期范围内存在的记录吗,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有以下要求, 样本数据: Numberid startDate, Enddate --------------------------------------------------- 1900415115 2012-09-22 00:39:00 2013-10-25 00:00:00 2429398311 2008-05-22 16:57:00 2013-07-30 00:00:00 4337719455 2008-05-22 16:57:00 2009
Numberid startDate, Enddate
---------------------------------------------------
1900415115 2012-09-22 00:39:00 2013-10-25 00:00:00
2429398311 2008-05-22 16:57:00 2013-07-30 00:00:00
4337719455 2008-05-22 16:57:00 2009-06-12 00:00:00
6644946399 2008-05-22 16:57:00 2009-01-16 00:00:00
9740698857 2008-05-22 16:57:00 2008-09-26 00:00:00
3928192597 2011-08-24 12:14:00 2012-09-24 00:00:00
1233655116 2013-08-14 00:39:00 2013-12-09 00:00:00
1780419233 2008-10-22 00:08:00 2014-08-24 00:00:00
1912939738 2011-08-26 01:32:00 2014-06-20 00:00:00
3810216146 2008-05-22 16:57:00 2009-01-16 00:00:00
5851814815 2009-10-07 01:00:00 2010-01-25 00:00:00
3692916726 2008-05-22 16:57:00 2008-10-02 00:00:00
3069490750 2008-05-22 16:57:00 2009-08-14 00:00:00
我想检查“numberid”是否存在于日期范围内,并希望按分组
01/01/2008 - 01/01/2009 as 0809
01/01/2009 - 01/01/2010 as 0910
01/01/2010 - 01/01/2011 as 1011
01/01/2011 - 01/01/2012 as 1112
感谢您的快速帮助
谢谢
你能试试下面的数据吗?我不确定它是否适用于enddate空值
Numberid startDate, Enddate
---------------------------------------------------
4405598510 2011-08-06 00:00:00 NULL
2418680054 2011-08-06 00:00:00 2011-12-28 00:00:00
4405598510 2011-08-06 00:00:00 NULL
1810168034 2011-08-06 00:00:00 NULL
6849266569 2011-08-06 00:00:00 2014-09-02 00:00:00
2682265222 2011-08-09 00:58:00 2012-09-20 00:00:00
6253123963 2011-08-09 00:00:00 2011-07-01 00:00:00
8276745680 2011-08-10 00:00:00 2014-06-27 00:00:00
3873103800 2011-08-10 00:00:00 2013-07-16 00:00:00
3703761027 2011-08-06 00:00:00 NULL
1810168034 2011-08-06 00:00:00 NULL
9888909217 2011-08-08 00:00:00 2013-06-30 00:00:00
3034945061 2011-08-09 00:59:00 NULL
4822850747 2011-08-10 00:00:00 2012-08-21 00:00:00
5849710101 2011-08-10 00:00:00 NULL
也不是每年一次的2008年、2009年。。2012..
我需要具体的日期
谢谢你试试这个
with myTable (
numberid,
startDate,
endDate
) as(
select
numberid,
CONVERT(DATETIME,startDate),
CONVERT(DATETIME,endDate)
from (
values
(4405598510,'2011-08-06 00:00:00',NULL),
(2418680054,'2011-08-06 00:00:00','2011-12-28 00:00:00'),
(4405598510,'2011-08-06 00:00:00',NULL),
(1810168034,'2011-08-06 00:00:00',NULL),
(6849266569,'2011-08-06 00:00:00','2014-09-02 00:00:00'),
(2682265222,'2011-08-09 00:58:00','2012-09-20 00:00:00'),
(6253123963,'2011-08-09 00:00:00','2011-07-01 00:00:00'),
(8276745680,'2011-08-10 00:00:00','2014-06-27 00:00:00'),
(3873103800,'2011-08-10 00:00:00','2013-07-16 00:00:00'),
(3703761027,'2011-08-06 00:00:00',NULL),
(1810168034,'2011-08-06 00:00:00',NULL)
) [ ] (numberid,startDate,endDate)
)
select
Numberid,
startDate,
endDate,
case when 2009 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [0809],
case when 2010 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [0910],
case when 2011 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1011],
case when 2012 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1112],
case when 2013 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1213]
from myTable
编辑:
您可以这样做,而不是:
2009 between year(startDate) and ISNULL(year(endDate),year(startDate))
因此,您可以设置间隔,如下所示:
case when startDate <= '2012-01-01' and ISNULL(endDate,startDate) >= '2011-01-01' then 'y' else 'n' end [1011],
当起始日期为'2011-01-01'时,则为'y'或'n'结束[1011],
您能提供预期的输出吗?否则我想知道。。如果numberid存在于特定范围yes或no之间,并且列可能类似于numberid、actualstartdate、actualenddate、exists0809(y/n)、exists0910(y/n)、exists1011(y/n)、exists1112(y/n),请检查“特定日期”的编辑,好的,让我在这里简单介绍一下。。我想把这个日期范围内2009年1月1日-2010年1月1日存在的numberid设置为0910。。在开始和结束日期之间。你应该总是在你的问题中添加预期输出,我不仅仅是在谈论专栏。根据您提供的样本数据,输出结果是什么?我很抱歉。。第二个给出的解决方案在某种程度上是有帮助的,但当我的enddata为null时,它就不起作用了,例如:9740698857,'2008-05-22 16:57:00','null',它没有显示出来,我的考虑是它应该在2008年再次显示为'y'thaks!!例如1043085226 2014-08-19 22:35:00空n n 7573180793 2008-05-22 16:57:00空n 1059281603 2014-09-01 22:24:00空n 1059281603 2014-09-01 22:24:00空n n 1105021936 2012-09-13 02:12:00空n n n n n n n n n n n n n n n n n n n请将该信息添加到问题中。。4405598510 2011-08-06 00:00:00 NULL 2418680054 2011-08-06 00:00:00 2011-12-28 00:00:00 4405598510 2011-08-06 00:00:00 NULL 1810168034 2011-08-06 00:00:00 NULL 6849266569 2011-08-06 00:00:00 2014-09-02 00:00:00 2682265222 2011-08-09 00:09 00:58:00 2012-09-09-08-09-09-09:00:00:00 6253123963 2011-07-01 00:00:00:00:00:00 827680-2011-0800:00:00 2014-06-27 00:00:00 3873103800 2011-08-10 00:00:00 2013-07-16 00:00:00 3703761027 2011-08-06 00:00:00零1810168034 2011-08-06 00:00零