Solved

Need SQL query to yield dates range for part status

Posted on 2014-04-17
14
271 Views
Last Modified: 2014-04-18
Hi
I have a table/query result set that needs to be consolidated. This is best explained via the attached file.
Essentially the table needs to be consolidated from it's current 23 rows to 4 rows, acheived by determining the date range for each part status change.

At present there is a row for every month regardless of whether the status changes or not, I need to remove these redundant rows and replace with single a row that shows the MAX and MIN date ranges for that status.
As I said, the file will best explain this - thanks a lot!!
Fergal
example.xls
0
Comment
Question by:fjkilken
  • 8
  • 4
  • 2
14 Comments
 

Author Comment

by:fjkilken
ID: 40006265
file embedded also:

example.xls
0
 
LVL 6

Expert Comment

by:Dulton
ID: 40006267
select [part num], [site], [status], Min([Status start date]) as [status start date]
,Max([status end date]) as [status end date]
from MyTable
Group by
[part num], [site], [status]

Open in new window

0
 

Author Comment

by:fjkilken
ID: 40006279
thanks Dulton but sorry - but that only gives 2 rows of data, 4 rows are expected
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40006347
It's not that easy, because status can repeat. min/max queries would get you min date of period1 and max date of period2.

I doubt it can be done with simple query, as you can't simply group by status.

You may try to accomplish this using LAG() or LEAD().

See http://blog.sqlauthority.com/2013/09/22/sql-server-how-to-access-the-previous-row-and-next-row-value-in-select-statement/

Bye, Olaf.
0
 
LVL 6

Expert Comment

by:Dulton
ID: 40006370
I didn't type your whole table, just enough to show the concept.

you can establish an initial order on this table by using row_number on some field that isnt' changing. I chose [part num]... that'll establish a unique incrementing integer on each row, preserving the original order.

from there, it's just a recursive CTE, looping through the dataset, incrementing by the row_number field and seeing when the status changes.




declare @MyTable table
([part num] varchar(10)
,[site] varchar(10)
,[status] varchar(10)
,[status start date] date
,[status end date] date
)

insert into @MyTable ([part num], [site],  [status], [status start date], [status end date])


                  values ('D25538-402','USPSS','EOL','1/29/2012','2/25/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','3/31/2012')
                                ,('D25538-402','USPSS','RETIRED','4/1/2012','4/28/2012')
                                ,('D25538-402','USPSS','RETIRED','4/29/2012','5/26/2012')
                                ,('D25538-402','USPSS','EOL','5/27/2012','6/30/2012')
                                ,('D25538-402','USPSS','RETIRED','7/1/2012','7/28/2012')
                                ,('D25538-402','USPSS','RETIRED','7/29/2012','8/25/2012')
                                ,('D25538-402','USPSS','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','12/28/2012')


                                ;with  partListing AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date]
                                ,row_number() OVER(ORDER BY [part num]) AS [InitialOrder]
                                FROM @MyTable
                                ), DateCompile  AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date], [InitialOrder]
                                FROM PartListing  where [initialorder]  = 1
                                union all  select
                                dc.[part num],dc.[site],case when dc.[status] <> pl.[status] then pl.[status] else dc.[status] end as  [status]
                                , case  when dc.[status] <> pl.[status] then pl.[status start date] else dc.[status start date] end as  [status start date]
                                ,case  when dc.[status] = pl.[status] then pl.[status end date] else dc.[status end date] end as  [status end date]
                                ,pl.[initialorder]                              
                                from datecompile  as dc
                                inner join  partListing as pl on dc.[part num] = pl.[part num] and dc.[site] = pl.[site] and dc.[initialorder] + 1 =  pl.[InitialOrder] 
                                )
                                select [part num],[site],[status],[status start date],  max([status end date]) AS [status end date]
                                from
                                (
                                select  [part num],[site],[status],[status start date], [status end date]
                                ,ROW_NUMBER() OVER(PARTITION BY [part num],[site],[status],[status start date] ORDER  BY [status end date]) AS [FinalOrder]
                                from datecompile
                                ) as  t1
                                group by  [part num],[site],[status],[status start date]
                                order by  [status start date]

Open in new window

0
 

Author Comment

by:fjkilken
ID: 40006375
thanks for the link Olaf
LAG and LEAD functions look really cool! but although I believe I'm running SQL server 2012 (SQL Server 11.0.3000) - the functions are not available for me - any idea of how I may enable them? thanks
0
 

Author Comment

by:fjkilken
ID: 40006413
hi Dulton, thanks for the code - when I ran it I saw an unusual result:
For the second occurentce of "EOL" the status end date should be "2012-06-30" but it is resulting as "2012-05-26"
- any ideas why?
Fergal

part num      site      status      status start date      status end date

D25538-402      USPSS      EOL      2012-01-29      2012-02-25
D25538-402      USPSS      RETIRED      2012-02-26      2012-05-26
D25538-402      USPSS      EOL      2012-05-27      2012-05-26
D25538-402      USPSS      RETIRED      2012-07-01      2012-12-28
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 6

Expert Comment

by:Dulton
ID: 40006444
The end date didn't need wrapped in a case statement.

this should work.
declare @MyTable table
([part num] varchar(10)
,[site] varchar(10)
,[status] varchar(10)
,[status start date] date
,[status end date] date
)

insert into @MyTable ([part num], [site],  [status], [status start date], [status end date])


                  values ('D25538-402','USPSS','EOL','1/29/2012','2/25/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','3/31/2012')
                                ,('D25538-402','USPSS','RETIRED','4/1/2012','4/28/2012')
                                ,('D25538-402','USPSS','RETIRED','4/29/2012','5/26/2012')
                                ,('D25538-402','USPSS','EOL','5/27/2012','6/30/2012')
                                ,('D25538-402','USPSS','RETIRED','7/1/2012','7/28/2012')
                                ,('D25538-402','USPSS','RETIRED','7/29/2012','8/25/2012')
                                ,('D25538-402','USPSS','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','12/28/2012')


                                ;with  partListing AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date]
                                ,row_number() OVER(ORDER BY [part num]) AS [InitialOrder]
                                FROM @MyTable
                                ), DateCompile  AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date], [InitialOrder]
                                FROM PartListing  where [initialorder]  = 1
                                union all  select
                                dc.[part num],dc.[site],case when dc.[status] <> pl.[status] then pl.[status] else dc.[status] end as  [status]
                                , case  when dc.[status] <> pl.[status] then pl.[status start date] else dc.[status start date] end as  [status start date]
                                ,pl.[status end date] as  [status end date]
                                ,pl.[initialorder]                              
                                from datecompile  as dc
                                inner join  partListing as pl on dc.[part num] = pl.[part num] and dc.[site] = pl.[site] and dc.[initialorder] + 1 =  pl.[InitialOrder] 
                                )
                                select [part num],[site],[status],[status start date],  max([status end date]) AS [status end date]
                                from
                                (
                                select  [part num],[site],[status],[status start date], [status end date]
                                ,ROW_NUMBER() OVER(PARTITION BY [part num],[site],[status],[status start date] ORDER  BY [status end date]) AS [FinalOrder]
                                from datecompile
                                ) as  t1
                                group by  [part num],[site],[status],[status start date]
                                order by  [status start date] 

Open in new window

0
 

Author Comment

by:fjkilken
ID: 40006451
ok - upgraded ny tools - now running 11.0.3368 and all is good - thanks!
0
 

Author Comment

by:fjkilken
ID: 40006535
just expanding it now to take in other sites but having a problem that it's only bringing back 1 recrods - I'll take a closer look at the code and see what could be changed if possible
0
 

Author Comment

by:fjkilken
ID: 40006559
here's an example of it failing: (returning just a single record)

declare @MyTable table
([part num] varchar(10)
,[site] varchar(10)
,[status] varchar(10)
,[status start date] date
,[status end date] date
)

insert into @MyTable ([part num], [site],  [status], [status start date], [status end date])


                  values
                                                  ('D25538-402','JAPAN','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','JAPAN','RETIRED','9/30/2012','11/02/2012')
                                                ,('D25538-402','USPSS','EOL','1/29/2012','2/25/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','3/31/2012')
                                ,('D25538-402','USPSS','RETIRED','4/1/2012','4/28/2012')
                                ,('D25538-402','USPSS','RETIRED','4/29/2012','5/26/2012')
                                ,('D25538-402','USPSS','EOL','5/27/2012','6/30/2012')
                                ,('D25538-402','USPSS','RETIRED','7/1/2012','7/28/2012')
                                ,('D25538-402','USPSS','RETIRED','7/29/2012','8/25/2012')
                                ,('D25538-402','USPSS','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','12/28/2012')

                                ;with  partListing AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date]
                                ,row_number() OVER(ORDER BY [part num]) AS [InitialOrder]
                                FROM @MyTable
                                ), DateCompile  AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date], [InitialOrder]
                                FROM PartListing  where [initialorder]  = 1
                                union all  select
                                dc.[part num],dc.[site],case when dc.[status] <> pl.[status] then pl.[status] else dc.[status] end as  [status]
                                , case  when dc.[status] <> pl.[status] then pl.[status start date] else dc.[status start date] end as  [status start date]
                                ,pl.[status end date] as  [status end date]
                                ,pl.[initialorder]                              
                                from datecompile  as dc
                                inner join  partListing as pl on dc.[part num] = pl.[part num] and dc.[site] = pl.[site] and dc.[initialorder] + 1 =  pl.[InitialOrder]
                                )
                                select [part num],[site],[status],[status start date],  max([status end date]) AS [status end date]
                                from
                                (
                                select  [part num],[site],[status],[status start date], [status end date]
                                ,ROW_NUMBER() OVER(PARTITION BY [part num],[site],[status],[status start date] ORDER  BY [status end date]) AS [FinalOrder]
                                from datecompile
                                ) as  t1
                                group by  [part num],[site],[status],[status start date]
                                order by  [status start date]
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 40006615
>LAG and LEAD functions look really cool! ...any idea of how I may enable them?
Well, not without SQL2012, but the article shows an alternative using CTE, which is similar to what the others here already do.

Bye, Olaf.
0
 
LVL 6

Assisted Solution

by:Dulton
Dulton earned 250 total points
ID: 40006728
your original example data doesn't show multiple site data. that's why the code didn't consider it.

try the below for the sample data you showed in post: 40006559

declare @MyTable table
([part num] varchar(10)
,[site] varchar(10)
,[status] varchar(10)
,[status start date] date
,[status end date] date
)

insert into @MyTable ([part num], [site],  [status], [status start date], [status end date])


                  values ('D25538-402','JAPAN','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','JAPAN','RETIRED','9/30/2012','11/02/2012')
                                ,('D25538-402','USPSS','EOL','1/29/2012','2/25/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','3/31/2012')
                                ,('D25538-402','USPSS','RETIRED','4/1/2012','4/28/2012')
                                ,('D25538-402','USPSS','RETIRED','4/29/2012','5/26/2012')
                                ,('D25538-402','USPSS','EOL','5/27/2012','6/30/2012')
                                ,('D25538-402','USPSS','RETIRED','7/1/2012','7/28/2012')
                                ,('D25538-402','USPSS','RETIRED','7/29/2012','8/25/2012')
                                ,('D25538-402','USPSS','RETIRED','8/26/2012','9/29/2012')
                                ,('D25538-402','USPSS','RETIRED','2/26/2012','12/28/2012')


                                ;with  partListing AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date]
                                ,row_number() OVER(ORDER BY [part num]) AS [InitialOrder]
                                FROM @MyTable
                                ), DateCompile  AS
                                (
                                SELECT [part num],[site],[status],[status start date], [status end date], [InitialOrder]
                                FROM PartListing  where [initialorder]  = 1
                                union all  select
                                dc.[part num]
                                ,case when dc.[site] <> pl.[site] then pl.[site] else dc.[site] end as [site]
,case when dc.[status] <> pl.[status] or dc.[site] <> pl.[site] then pl.[status] else dc.[status] end as  [status]
                                , case  when dc.[status] <> pl.[status] or dc.[site] <> pl.[site] then pl.[status start date] else dc.[status start date] end as  [status start date]
                                ,pl.[status end date] as  [status end date]
                                ,pl.[initialorder]                              
                                from datecompile  as dc
                                inner join  partListing as pl on  dc.[initialorder] + 1 =  pl.[InitialOrder] 
                                )
                                select [part num],[site],[status],[status start date],  max([status end date]) AS [status end date]
                                from
                                (
                                select  [part num],[site],[status],[status start date], [status end date]
                                ,ROW_NUMBER() OVER(PARTITION BY [part num],[site],[status],[status start date] ORDER  BY [status end date]) AS [FinalOrder]
                                from datecompile
                                ) as  t1
                                group by  [part num],[site],[status],[status start date]
                                order by  [status start date] 

Open in new window

0
 

Author Closing Comment

by:fjkilken
ID: 40008388
Thanks a lot for the help

I have split the points as Olaf got me pointed to LAG/LEAD which I have found to be very powerful, efficient and easy to use.
If I didn't have SQL 2012, then Dulton's solution is what I would use.

Thanks guys!
Fergal
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now