|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
Hello there,
I have a little challenge that makes me thinking for hours now. I have a list of data and have to group them. The problem is there are records that mark the beginning of a new group which do not have other data then the rest.
Here's the example code:
declare @tmp table( Postalcode nvarchar(10), Street nvarchar(100), Housenumber int, IsSeparator bit );
insert into @tmp values ( 12345, 'SomeStreetName', 188 , '0' ) ,( 12345, 'SomeStreetName', 212 , '0' ) ,( 12345, 'SomeStreetName', 214 , '0' ) ,( 12345, 'SomeStreetName', 214 , '1' ) ,( 12345, 'SomeStreetName', 236 , '0' ) ,( 12345, 'SomeStreetName', 238 , '0' ) ,( 12345, 'SomeStreetName', 238 , '1' ) ,( 12345, 'SomeStreetName', 242 , '0' ) ,( 12345, 'SomeStreetName', 244 , '0' ) ,( 12345, 'SomeStreetName', 246 , '0' )
select *, GroupNumber = '???' from @tmp t order by t.Housenumber
select *, GroupNr = 1 from @tmp where Housenumber <=214 union select *, GroupNr = 2 from @tmp where Housenumber > 214 and HouseNumber <= 238 union select *, GroupNr = 3 from @tmp where Housenumber > 238 order by HouseNumber
The second query shows the result i need. Of course I want to do it not that way as the house number ranges could differ  These are house numbers of a street, ordered ascending. The column "IsSeparator" shows that there should start a new group. I tried a lot using row_number() or rank() but I don't get to query that get's the result shown above.
Any help would be great.
Thank's a lot, Wolf
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 4,240,
Visits: 9,487
|
|
select * ,GroupNumber = ( case when HouseNumber <= 214 then 1 when HouseNumber <= 238 then 2 else 3 end ) from @tmp t order by t.Housenumber
____________________________________________________________________________________________
Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
You misunderstood. I cannot use the house numbers in my query. That was just to show how the result should be. I have not only 1 street in my list there some hundred streets. And I have to group each of them by the seperator line.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, January 28, 2013 1:45 AM
Points: 386,
Visits: 199
|
|
Maybe
; with cte as ( select *, num = (select count(*) from @tmp t2 where t1.Postalcode = t2.Postalcode and t1.Street = t2.Street and t2.Housenumber <= t1.Housenumber and IsSeparator = 1) from @tmp t1 ) select *, GroupNr=num+1 from cte where IsSeparator = 0 order by Postalcode, Street, HouseNumber
Cursors never. DTS - only when needed and never to control.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 11:22 AM
Points: 2,541,
Visits: 4,370
|
|
The problem with your posted sample is lack of details. Would be really great if you could provide better DDL. What is PK of your table? What really makes your address group? Also, does the row with separator is a duplicate with the same HouseNumeber? My sample is making assumption that you want to group by PostCode and HouseNumber. Please note that I've extended your sample data to include addresses with different postcodes:
declare @tmp table( Postalcode nvarchar(10), Street nvarchar(100), Housenumber int, IsSeparator bit );
insert into @tmp values ( 12345, 'SomeStreetName', 188 , '0' ) ,( 12345, 'SomeStreetName', 212 , '0' ) ,( 12345, 'SomeStreetName', 214 , '0' ) ,( 12345, 'SomeStreetName', 214 , '1' ) ,( 12345, 'SomeStreetName', 236 , '0' ) ,( 12345, 'SomeStreetName', 238 , '0' ) ,( 12345, 'SomeStreetName', 238 , '1' ) ,( 12345, 'SomeStreetName', 242 , '0' ) ,( 12345, 'SomeStreetName', 244 , '0' ) ,( 12345, 'SomeStreetName', 246 , '0' ) ,( 12346, 'SomeStreetName', 88 , '0' ) ,( 12346, 'SomeStreetName', 112 , '0' ) ,( 12346, 'SomeStreetName', 114 , '0' ) ,( 12346, 'SomeStreetName', 114 , '1' ) ,( 12346, 'SomeStreetName', 136 , '0' ) ,( 12347, 'SomeStreetName', 1 , '0' ) ,( 12347, 'SomeStreetName', 3 , '0' )
SELECT t.* ,DENSE_RANK() OVER (PARTITION BY t.Postalcode ORDER BY ISNULL(b.Band, 9999999)-- you can use max int here ) as GroupId FROM @tmp t CROSS APPLY(SELECT MIN(Housenumber) Band FROM @tmp b WHERE b.Postalcode = t.Postalcode AND b.Housenumber >= t.Housenumber AND b.IsSeparator = 1) b WHERE t.IsSeparator = 0
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 4,240,
Visits: 9,487
|
|
WolfgangE (12/11/2012) You misunderstood. I cannot use the house numbers in my query. That was just to show how the result should be. I have not only 1 street in my list there some hundred streets. And I have to group each of them by the seperator line.
Oops - sorry. I see Eugene is on the case, so no need for another dodgy post from me.
____________________________________________________________________________________________
Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
This looks quite good. And so simple. I already tried something similar making a join between a table containing the separators and the non-separators but it did not work...
Thank's a lot, this helps me a lot. And as I read your signature: this query is for replacing a cursor
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
Phil Parkin (12/11/2012)
WolfgangE (12/11/2012) You misunderstood. I cannot use the house numbers in my query. That was just to show how the result should be. I have not only 1 street in my list there some hundred streets. And I have to group each of them by the seperator line.
Oops - sorry. I see Eugene is on the case, so no need for another dodgy post from me.
Thank's to you too
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
Hello Eugene Elutin,
the problem with the details is: they are no details. The shown table is the result of a quite complex select out of a database that I've never seen before. I just know that I get this result and approxemately what the finally result should be. And no, there is no primary key.
BUT: very thanks, your query does EXACTLY what I need
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:56 AM
Points: 74,
Visits: 419
|
|
And to finally answer this question:
Eugene Elutin (12/11/2012)What really makes your address group?
The separator lines are grouping criteria. It's kind of user configuration. This means a user inserts the separator lines thus defining where he wants to have the groups. This is part of an delivery plan. The user decides how many people a street needs to deliver all parcels. Every group consists of several house numbers. So the user can define how many groups or people we need to serve the whole street and can tell the people which house number ranges they are responsible for.
|
|
|
|