MS SQL CTE HANGS HELP

Hello
I have the following scenarion.
I have a table with millions of records and I need to compare rows belonging to the same unitId to increment a counter
What I need to know is detect number that a unit changes from a certain set of color/description to another.
I Filter based on UnitId which is a varchar and a date, that would return thousands of rows from the main table.
Sometimes the CTE runs in les than 10 seconds and someother times it just hangs, that I have to stop the execution of the query even though it is the same query
Any idea what might be causing this hanging or maybe infinite loop somewhere as the sql server process would stick at 50% CPU when I look in task manager
Here is some sample code

Thanks
;
WITH CTE AS (
	SELECT top  ROW_NUMBER() OVER (ORDER BY UnitID, EventDate ) AS RowNum,
		UnitID, UnitDescription, EventDate,Color
	FROM UnitInfo 
    WHERE EventDate >='2000-01-01T00:00:00'
    AND UnitId LIKE 'veh-%'
)

SELECT  COUNT([Current Row].UnitID) AS C,[Current Row].UnitID
FROM CTE [Current Row]
LEFT JOIN CTE [Previous Row] 
  [Previous Row].RowNum = [Current Row].RowNum - 1
WHERE
	(([Previous Row].Color ='red' AND [Previous Row].UnitDescription='Merce')
	OR
	(
		(	[Previous Row].Color IN ('red','yellow','blue','white')
			AND
			([Current Row].Color IN ('Orange','Pink') OR [Current Row].UnitDescription='Golf')
		)
	))
	AND
	([Previous Row].UnitId = [Current Row].UnitId)
	
	GROUP BY [Current Row].UnitID

Open in new window

LVL 9
xav056Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Try this which should work better:
;
WITH CTE AS (
	SELECT UnitID, ROW_NUMBER() OVER (ORDER BY UnitID, EventDate ) AS RowNum,
case when Color ='red' AND UnitDescription='Merce' then 1 else 0 end as cond_1,
case when Color IN ('Orange','Pink') OR UnitDescription='Golf' then 1 else 0 end as cond_2,
case when Color IN ('red','yellow','blue','white') then 1 else 0 end as cond_3
	FROM UnitInfo 
    WHERE EventDate >='2000-01-01T00:00:00'
    AND UnitId LIKE 'veh-%'
)

SELECT  COUNT([Current Row].UnitID) AS C,[Current Row].UnitID
FROM CTE [Current Row]
LEFT JOIN CTE [Previous Row] ON
  [Previous Row].RowNum = [Current Row].RowNum - 1
  AND [Previous Row].UnitId = [Current Row].UnitId
WHERE [Previous Row].cond_1 = 1
OR ( [Previous Row].cond_2 = 1 AND [Current Row].cond_3 = 1)
GROUP BY [Current Row].UnitID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xav056Author Commented:
Brilliant Thank you
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Welcome..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.