SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 weird group by
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

xholax
Starting Member

Argentina
12 Posts

Posted - 03/18/2013 :  19:16:32  Show Profile  Reply with Quote
Hello friends hope you can help me, i need the below query for my job but i cant get it :( :


--Sample Data
DECLARE @Foo TABLE (oper CHAR(10), [start] int, [end] int , [status] int )


INSERT @Foo
select 'James', 0,3,2 union all
select 'James', 3,5,2 union all
select 'James', 5,8,3 union all
select 'James', 8,9,4 union all
select 'James', 9,10,2 union all
select 'James', 10,14,2 union all
select 'James', 14,48,3

select * from @Foo



thanks in advance

James K
Flowing Fount of Yak Knowledge

3593 Posts

Posted - 03/18/2013 :  19:41:22  Show Profile  Reply with Quote
;with cte as
(
	select *, 
		ROW_NUMBER() over (order by start,[end])-
		ROW_NUMBER() over (PARTITION by status order by start,[end]) Grp
	from
		@Foo
)
select
	oper,
	MIN(start) [start],
	MAX([end]) [end],
	status
from
	cte
group by
	oper,
	status,
	grp
order by 
	[start],
	[end]
Go to Top of Page

xholax
Starting Member

Argentina
12 Posts

Posted - 03/18/2013 :  23:05:13  Show Profile  Reply with Quote
thanks it works perfect, i didnt know about that kind of query partitions and rank where i can find more info about that , some recommend book ?

Edited by - xholax on 03/18/2013 23:06:06
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000