Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
An interesting little SQL challenge
Message
From
09/10/2017 17:21:16
Walter Meester
HoogkarspelNetherlands
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01654817
Message ID:
01654865
Views:
57
Am I Too late :)

As usual, I try to keep it bare bones.
SELECT Processname, Firstdate, Count(*) as numrows
	FROM @processrows A
		CROSS APPLY (SELECT MIN(B.Processdate) as firstdate 
						FROM @ProcessRows B 
							LEFT JOIN @ProcessRows C ON C.ProcessNAme <> B.ProcessName AND C.ProcessDate BETWEEN B.ProcessDate AND A.Processdate
						WHERE B.ProcessName = A.Processname AND B.Processdate <= A.Processdate AND C.ProcessDate IS NULL
					) as x 
	GROUP BY ProcessName, Firstdate
Walter,


>I honestly thought this was going to be an easy one, and wound up spending way more time pulling my hair out than I thought I would.
>
>I have a solution, but it's rather nasty, and I'm curious if anyone wants to take a crack. I'll post my SQL code later, I want to see if anyone comes up with something more elegant. (My solution is not very elegant)
>
>OK, here goes....suppose you have a piece of inventory that's in Phase A for a few days, then in Phase B, then in Phase C, then "back and forth" between A and C and D in some way.
>You want to generate a result set of each Phase and the number of days at the phase, keeping in mind that something could be at Phase A several times in between other phases.
>
>So here's some sample data:
>
>
>declare @ProcessRows table  (ProcessDate Date, ProcessName varchar(50)) 
>
>insert into @ProcessRows values ('10/1/2017', 'Process A') 
>insert into @ProcessRows values ('10/2/2017', 'Process A') 
>insert into @ProcessRows values ('10/3/2017', 'Process A') 
>
>insert into @ProcessRows values ('10/4/2017', 'Process B') 
>insert into @ProcessRows values ('10/5/2017', 'Process B') 
>insert into @ProcessRows values ('10/6/2017', 'Process B') 
>insert into @ProcessRows values ('10/7/2017', 'Process B') 
>
>insert into @ProcessRows values ('10/8/2017', 'Process C') 
>
>insert into @ProcessRows values ('10/9/2017', 'Process A')   
>insert into @ProcessRows values ('10/10/2017', 'Process A')   
>insert into @ProcessRows values ('10/11/2017', 'Process A') 
>    
>insert into @ProcessRows values ('10/12/2017', 'Process D') 
>insert into @ProcessRows values ('10/13/2017', 'Process D') 
>
>insert into @ProcessRows values ('10/14/2017', 'Process A') 
>insert into @ProcessRows values ('10/15/2017', 'Process A')   
>
>insert into @ProcessRows values ('10/16/2017', 'Process D')   
>
>
>I want to produce the following result set:
>
>processname       FirstDate      NumRows
>Process A       2017-10-01        3
>Process B        2017-10-04        4
>Process C        2017-10-08        1
>Process A        2017-10-09        3
>Process D        2017-10-12        2
>Process A        2017-10-14        2
>Process D        2017-10-16        1
>
>Any takers? :)
>
>One stipulation: it can't use a cursor. In production there would be far too much data.
>
>Thanks
Previous
Reply
Map
View

Click here to load this message in the networking platform