私は時間枠を抽出するために必要な単一の日付のリストを持っています。T-SQLの単一の日付から上限と下限のリストを作成
例:
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 |
+--------------+------------+------------+------------+------------+----------+------------+
なぜこのデータセットが返されますか?
ショーケースには、デフォルト値を含めることができますラグ私たちはあなたが何を試して何を得たのか[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t [** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)質問品質を向上させ、より良い回答を得る方法を学ぼう。 –
@JuanCarlosOropeza – fmogo
,CASE \t \t \t WHEN lag(convert(date,ARD),1) OVER (Partition by InternalID,EpisodeSys,groupersys ORDER by InternalID,EpisodeSys,[ARD],groupersys) is null then '1900-01-01' \t \t \t 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]) \t \t ELSE \t \t \t \t \t convert(date,[ARD]) \t \t END as 'Low Date'
– fmogo