SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to display a column with deleted overlapping dates


Need to display a column with deleted overlapping dates

Author
Message
sanket.wagh7689
sanket.wagh7689
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 88
Mike01 - Wednesday, July 11, 2018 1:47 PM
I don't see an attachment

I attached it with this reply.

Attachments
dimProp.txt (11 views, 2.00 MB)
Mike01
Mike01
SSCrazy Eights
SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)SSCrazy Eights (9.1K reputation)

Group: General Forum Members
Points: 9094 Visits: 2244
There was some bad data in there, that e didn't account for. I added the criteria below



;WITH CTE AS (
select pcode,pkey, RowEffectiveDate, rowenddate, RowEffectiveDateCalc, RowEndDateCalc,
RowEffectiveDateCalc CurRowEffectiveDateCalc, RowEndDateCalc CurRowEndDateCalc
from (
SELECT pcode,pkey, RowEffectiveDate, rowenddate
,cast(RowEffectiveDate as date) RowEffectiveDateCalc
,cast(rowenddate as date) RowEndDateCalc
, ROW_NUMBER() OVER(PARTITION BY pcode ORDER BY RowEffectiveDate, rowenddate, PKey) AS rn
FROM DimPro
--where PCode = 'TX5125'
) X
where rn = 1
union all
select c.pcode, c.pkey, c.RowEffectiveDate, c.rowenddate, c.RowEffectiveDateCalc, c.RowEndDateCalc,
c.RowEffectiveDateCalc CurRowEffectiveDateCalc, c.RowEndDateCalc CurRowEndDateCalc
from (

SELECT NextRow.PCode, NextRow.Pkey, NextRow.RowEffectiveDate, NextRow.rowenddate
,cast(NextRow.RowEffectiveDate as date) RowEffectiveDateCalc
,cast(NextRow.rowenddate as date) RowEndDateCalc
,cast(CurRow.RowEffectiveDate as date) CurRowEffectiveDateCalc
,cast(CurRow.rowenddate as date) CurRowEndDateCalc
,Row_Number() over (partition by NextRow.pcode order by NextRow.RowEffectiveDate, NextRow.rowenddate, NextRow.PKey) AS rn
FROM CTE CurRow
join DimPro NextRow
on CurRow.PCode = NextRow.PCode
and NextRow.RowEffectiveDate <= NextRow.rowenddate
and DateDiff(day, CurRow.RowEndDateCalc, cast(NextRow.RowEffectiveDate as date)) = 1
) c
where rn = 1
)

select * from CTE
order by 1, 2


For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
sanket.wagh7689
sanket.wagh7689
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 88
Mike01 - Thursday, July 12, 2018 7:09 AM
There was some bad data in there, that e didn't account for. I added the criteria below



;WITH CTE AS (
select pcode,pkey, RowEffectiveDate, rowenddate, RowEffectiveDateCalc, RowEndDateCalc,
RowEffectiveDateCalc CurRowEffectiveDateCalc, RowEndDateCalc CurRowEndDateCalc
from (
SELECT pcode,pkey, RowEffectiveDate, rowenddate
,cast(RowEffectiveDate as date) RowEffectiveDateCalc
,cast(rowenddate as date) RowEndDateCalc
, ROW_NUMBER() OVER(PARTITION BY pcode ORDER BY RowEffectiveDate, rowenddate, PKey) AS rn
FROM DimPro
--where PCode = 'TX5125'
) X
where rn = 1
union all
select c.pcode, c.pkey, c.RowEffectiveDate, c.rowenddate, c.RowEffectiveDateCalc, c.RowEndDateCalc,
c.RowEffectiveDateCalc CurRowEffectiveDateCalc, c.RowEndDateCalc CurRowEndDateCalc
from (

SELECT NextRow.PCode, NextRow.Pkey, NextRow.RowEffectiveDate, NextRow.rowenddate
,cast(NextRow.RowEffectiveDate as date) RowEffectiveDateCalc
,cast(NextRow.rowenddate as date) RowEndDateCalc
,cast(CurRow.RowEffectiveDate as date) CurRowEffectiveDateCalc
,cast(CurRow.rowenddate as date) CurRowEndDateCalc
,Row_Number() over (partition by NextRow.pcode order by NextRow.RowEffectiveDate, NextRow.rowenddate, NextRow.PKey) AS rn
FROM CTE CurRow
join DimPro NextRow
on CurRow.PCode = NextRow.PCode
and NextRow.RowEffectiveDate <= NextRow.rowenddate
and DateDiff(day, CurRow.RowEndDateCalc, cast(NextRow.RowEffectiveDate as date)) = 1
) c
where rn = 1
)

select * from CTE
order by 1, 2

Works like a charm and within seconds. Can you please tell me what was the bad data that was causing a problem?
Also, if there any way we can write all these unwanted/overlapping records to a file so that we can identify them in others tables too and remove them?

george_at_sql
george_at_sql
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1517 Visits: 1927
sanket.wagh7689 - Thursday, July 12, 2018 9:37 AM
Mike01 - Thursday, July 12, 2018 7:09 AM
There was some bad data in there, that e didn't account for. I added the criteria below



;WITH CTE AS (
select pcode,pkey, RowEffectiveDate, rowenddate, RowEffectiveDateCalc, RowEndDateCalc,
RowEffectiveDateCalc CurRowEffectiveDateCalc, RowEndDateCalc CurRowEndDateCalc
from (
SELECT pcode,pkey, RowEffectiveDate, rowenddate
,cast(RowEffectiveDate as date) RowEffectiveDateCalc
,cast(rowenddate as date) RowEndDateCalc
, ROW_NUMBER() OVER(PARTITION BY pcode ORDER BY RowEffectiveDate, rowenddate, PKey) AS rn
FROM DimPro
--where PCode = 'TX5125'
) X
where rn = 1
union all
select c.pcode, c.pkey, c.RowEffectiveDate, c.rowenddate, c.RowEffectiveDateCalc, c.RowEndDateCalc,
c.RowEffectiveDateCalc CurRowEffectiveDateCalc, c.RowEndDateCalc CurRowEndDateCalc
from (

SELECT NextRow.PCode, NextRow.Pkey, NextRow.RowEffectiveDate, NextRow.rowenddate
,cast(NextRow.RowEffectiveDate as date) RowEffectiveDateCalc
,cast(NextRow.rowenddate as date) RowEndDateCalc
,cast(CurRow.RowEffectiveDate as date) CurRowEffectiveDateCalc
,cast(CurRow.rowenddate as date) CurRowEndDateCalc
,Row_Number() over (partition by NextRow.pcode order by NextRow.RowEffectiveDate, NextRow.rowenddate, NextRow.PKey) AS rn
FROM CTE CurRow
join DimPro NextRow
on CurRow.PCode = NextRow.PCode
and NextRow.RowEffectiveDate <= NextRow.rowenddate
and DateDiff(day, CurRow.RowEndDateCalc, cast(NextRow.RowEffectiveDate as date)) = 1
) c
where rn = 1
)

select * from CTE
order by 1, 2

Works like a charm and within seconds. Can you please tell me what was the bad data that was causing a problem?
Also, if there any way we can write all these unwanted/overlapping records to a file so that we can identify them in others tables too and remove them?

You have records whose end_dates is less than the start date which is prevented by the following condition

NextRow.RowEffectiveDate <= NextRow.rowenddate

So if you

select *
from dbo.dimPro
where rowenddate<roweffectivedate




you should be able to see the erroneous records.

sanket.wagh7689
sanket.wagh7689
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 88
george_at_sql - Thursday, July 12, 2018 10:28 AM
sanket.wagh7689 - Thursday, July 12, 2018 9:37 AM
Mike01 - Thursday, July 12, 2018 7:09 AM
There was some bad data in there, that e didn't account for. I added the criteria below



;WITH CTE AS (
select pcode,pkey, RowEffectiveDate, rowenddate, RowEffectiveDateCalc, RowEndDateCalc,
RowEffectiveDateCalc CurRowEffectiveDateCalc, RowEndDateCalc CurRowEndDateCalc
from (
SELECT pcode,pkey, RowEffectiveDate, rowenddate
,cast(RowEffectiveDate as date) RowEffectiveDateCalc
,cast(rowenddate as date) RowEndDateCalc
, ROW_NUMBER() OVER(PARTITION BY pcode ORDER BY RowEffectiveDate, rowenddate, PKey) AS rn
FROM DimPro
--where PCode = 'TX5125'
) X
where rn = 1
union all
select c.pcode, c.pkey, c.RowEffectiveDate, c.rowenddate, c.RowEffectiveDateCalc, c.RowEndDateCalc,
c.RowEffectiveDateCalc CurRowEffectiveDateCalc, c.RowEndDateCalc CurRowEndDateCalc
from (

SELECT NextRow.PCode, NextRow.Pkey, NextRow.RowEffectiveDate, NextRow.rowenddate
,cast(NextRow.RowEffectiveDate as date) RowEffectiveDateCalc
,cast(NextRow.rowenddate as date) RowEndDateCalc
,cast(CurRow.RowEffectiveDate as date) CurRowEffectiveDateCalc
,cast(CurRow.rowenddate as date) CurRowEndDateCalc
,Row_Number() over (partition by NextRow.pcode order by NextRow.RowEffectiveDate, NextRow.rowenddate, NextRow.PKey) AS rn
FROM CTE CurRow
join DimPro NextRow
on CurRow.PCode = NextRow.PCode
and NextRow.RowEffectiveDate <= NextRow.rowenddate
and DateDiff(day, CurRow.RowEndDateCalc, cast(NextRow.RowEffectiveDate as date)) = 1
) c
where rn = 1
)

select * from CTE
order by 1, 2

Works like a charm and within seconds. Can you please tell me what was the bad data that was causing a problem?
Also, if there any way we can write all these unwanted/overlapping records to a file so that we can identify them in others tables too and remove them?

You have records whose end_dates is less than the start date which is prevented by the following condition

NextRow.RowEffectiveDate <= NextRow.rowenddate

So if you

select *
from dbo.dimPro
where rowenddate<roweffectivedate




you should be able to see the erroneous records.

Thank you! you are all awesome.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search