|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 2:09 PM
Points: 480,
Visits: 1,605
|
|
Jeff, Neat stuff on hierarchies! One thing that's confusing me is the use of the back slash and how it orders. Thus in the script below, why do I get two different ordering results when I order on c1 as opposed to c2?
select * from ( select 'AA' c1,CAST('AA' as binary(4)) c2 union all select 'A\',CAST('A\' as binary(4)) ) t order by c1
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 803,
Visits: 2,124
|
|
Nice Article Jeff, and having spent a lot of time recently working with them its good to see alternative solutions.
One common problem is people come across is when they have to flatten the hierarchy for data warehouses, eg Each level of the tree in a seperate column, and each row containing the entire tree for that Leaf node.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Today @ 2:21 AM
Points: 37,
Visits: 595
|
|
Really enjoyed this article, it explained the use of the CTE very well. Great work as usual Jeff.
Measure twice, cut once
|
|
|
|
|
SSChasing Mays
Group: Moderators
Last Login: Tuesday, May 07, 2013 3:54 PM
Points: 608,
Visits: 379
|
|
I prefer to use a two column approach to build a tree instead of the single column style that you have to split, it doesn't scale well. For smaller stuff like this HR representation it should be OK. Trees and Hierarchies in SQL from Joe Celko is one of my all time favorite books! Good article! your brother, Phil McCrevice
http://www.sqlserverio.com http://www.cactuss.org http://www.salssa.org
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, June 03, 2011 4:47 AM
Points: 2,
Visits: 53
|
|
That doesn't order correctly. You are relaying on the natural data order to provide the correct results.
Try inserting some extra records with lower manager id's and names beginning with z ;)
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
I've used a variation on this solution for sorting before, and found that it works best if you pad numerical values with leading zeroes to a fixed length, before sorting them as character data.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Michael Meierruth (3/10/2011)
Jeff, Neat stuff on hierarchies! One thing that's confusing me is the use of the back slash and how it orders. Thus in the script below, why do I get two different ordering results when I order on c1 as opposed to c2? select * from ( select 'AA' c1,CAST('AA' as binary(4)) c2 union all select 'A\',CAST('A\' as binary(4)) ) t order by c1
It's because of the collation settings for the server. For c1, it's purely an "alpha" sort based on collation. For c2, collation does not come into effect because it's binary. Since the backslash (ASCII character #92) has a larger ASCII value than the letter "A" (ASCII character #65), it sorts differently for the binary version. It would also sort c1 differently if you used one of the binary collations.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Jason-299789 (3/10/2011) Nice Article Jeff, and having spent a lot of time recently working with them its good to see alternative solutions.
One common problem is people come across is when they have to flatten the hierarchy for data warehouses, eg Each level of the tree in a seperate column, and each row containing the entire tree for that Leaf node.
In the example I gave, the hierarchical path column does contain the entire tree of ID's or names for that given Leaf node. So far as listing each level of the tree in a separate column goes, that would make for a whole lot of NULLs in many of the columns. I don't believe I'd use such a method because you have to change column names for similar queries on the structure. Instead, I'd recommend using a "Nested Set" hierarchy or the new preaggregated "warehouse" table method that I'll reveal when the larger article I'm writing on hierarchies comes out.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
ekoner (3/10/2011) Really enjoyed this article, it explained the use of the CTE very well. Great work as usual Jeff.
Thanks for the nice compliment, ekoner. I appreciate it.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|