在T-SQL中创建单个日期的高低日期列表

时间:2017-02-20 20:22:28

标签: sql-server

我有一个单日期列表,我需要从中提取时间范围。

示例:

2014-02-27  
2015-04-06  
2016-01-31  

我想看时间框架:

低日期到高日期

1900-01-01 2014-02-07  
2014-02-07 2015-04-06  
2015-04-06 2016-01-31  
2016-01-31 2017-02-28(Current end of month)  

我尝试在CASE语句中使用LAG和LEAD。这让我接近但不是所需的结果,如上例所示。

我怎样才能做到这一点?

CASE WHEN lag(convert(date,ARD),1) OVER (Partition by InternalID,EpisodeSys,
groupersys ORDER by InternalID,EpisodeSys, [ARD],groupersys) is null then '1900-01-01' 
     WHEN lag(convert(date,ARD),1) OVER (Partition by InternalID,EpisodeSys,groupersys ORDER by InternalID,EpisodeSys,[ARD],groupersys) is not null and lag(convert(date,ARD),1) OVER (Partition by InternalID,EpisodeSys,groupersys/*,[ARD]*/ ORDER by InternalID,EpisodeSys,[ARD],groupersys) is not null then convert(Date,[ARD]) 
     ELSE convert(date,[ARD]) END as 'Low Date'

此代码返回正确的表。谢谢!我真的很感激。

SELECT distinct InternalID
            , episodesys
            , lag([ARD], 1, '1900-01-01') over 
                      (partition by groupersys 
                       order by [ARD], groupersys) as 'Low Date'
            ,[ARD] as 'High Date'
            , groupersys
            , RugsCatg
            ,CASE 
                WHEN lag([ARD], 1, '1900-01-01') over 
                        (partition by groupersys 
                         order by [ARD],groupersys) = '1900-01-01' 
                then 'A1'
                ELSE rug_source
            END as 'rug_source'
    FROM (SELECT InternalID,episodesys, [ARD],groupersys, RugsCatg, 'A2' as  rug_source 
          FROM [MyDB].[dbo].[assessment_detail]
          UNION 
          SELECT InternalID,episodesys, EOMONTH(GETDATE()),groupersys, RugsCatg, 'A3' as rug_source 
          FROM [MyDB].[dbo].[assessment_detail]
     ) T
         where episodesys = '668955'

再次感谢@JuanCarlosOropeza

这是我运行代码时数据的外观。这些都是正确的结果。

+------------+------------+------------+------------+------------+----------+------------+  
| InternalID | episodesys |  Low Date  | High Date  | groupersys | RugsCatg | rug_source |  
+------------+------------+------------+------------+------------+----------+------------+  
| ABC123456  |     668955 | 1900-01-01 | 2016-09-07 |         51 | RUB      | A1         |  
| ABC123456  |     668955 | 1900-01-01 | 2016-09-07 |         57 | RUB      | A1         |  
| ABC123456  |     668955 | 2016-09-07 | 2016-09-14 |         51 | RUB      | A2         |  
| ABC123456  |     668955 | 2016-09-07 | 2016-09-14 |         57 | RUB      | A2         |  
| ABC123456  |     668955 | 2016-09-14 | 2017-02-28 |         51 | RUB      | A3         |  
| ABC123456  |     668955 | 2016-09-14 | 2017-02-28 |         57 | RUB      | A3         |  
+------------+------------+------------+------------+------------+----------+------------+    

我创建了一个视图并输入了代码。来自VIEW的SELECT返回此信息。这些都不是正确的结果。它们与查询直接不同。

+--------------+------------+------------+------------+------------+----------+------------+  
|  InternalID  | episodesys |  Low Date  | High Date  | groupersys | RugsCatg | rug_source |  
+--------------+------------+------------+------------+------------+----------+------------+  
| 1203V0023697 |       6689 | 2016-09-06 | 2016-09-07 |         51 | RUB      | A2         |  
| 1203V0023697 |       6689 | 2016-09-06 | 2016-09-07 |         57 | RUB      | A2         |  
| 1203V0023697 |       6689 | 2016-09-13 | 2016-09-14 |         51 | RUB      | A2         |  
| 1203V0023697 |       6689 | 2016-09-13 | 2016-09-14 |         57 | RUB      | A2         |  
| 1203V0023697 |       6689 | 2016-12-27 | 2017-02-28 |         51 | RUB      | A3         |  
| 1203V0023697 |       6689 | 2016-12-27 | 2017-02-28 |         57 | RUB      | A3         |  
+--------------+------------+------------+------------+------------+----------+------------+

为什么会返回这组数据?

1 个答案:

答案 0 :(得分:0)

<强> SQL DEMO

只需将当前日期添加为数据的一部分,然后Lag可以包含NULL

中的默认值
SELECT lag([dt], 1, '1900-01-01') over (order by [dt]) as [low],
       [dt] as [high]
FROM (SELECT [dt] FROM Table1
      UNION ALL
      SELECT GETDATE()
     ) T

<强>输出

|                        low |                       high |
|----------------------------|----------------------------|
|  January, 01 1900 00:00:00 | February, 27 2014 00:00:00 |
| February, 27 2014 00:00:00 |    April, 06 2015 00:00:00 |
|    April, 06 2015 00:00:00 |  January, 31 2016 00:00:00 |
|  January, 31 2016 00:00:00 | February, 20 2017 20:45:35 |