|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:29 PM
Points: 5,678,
Visits: 6,128
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:14 PM
Points: 32,906,
Visits: 26,794
|
|
Craig Farrell (3/14/2011) A bit ignorant here, what's an MLM?
Multi-Level Marketing. Think, Amway, ACN, Avon, and a whole host of other companies similar in nature.
--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/
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:29 PM
Points: 5,678,
Visits: 6,128
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:14 PM
Points: 32,906,
Visits: 26,794
|
|
Craig Farrell (3/14/2011)
AH! Legal Pyramid Schemes! Gotcha!
Heh... I have mixed emotions about them. The ones that run a Uni-Level payout matrix and require you to sell product and not just stack up representatives are some of the best. People CAN make money without being at the "top". The people who fail to run their business correctly (usually boils down to being lazy) seem to get the most press and, of course, they have nothing good to say about the companies.
Don't get me wrong... there are some really bad, bad, bad ones out there, as well but the industry, in general, has a legacy reputation that it no longer deserves.
--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 @ 3:14 PM
Points: 32,906,
Visits: 26,794
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, April 02, 2012 9:22 AM
Points: 3,
Visits: 29
|
|
Excellent article Jeff! You just saved me quite a bit of sanity
I was needing to sort by an alphanumeric name field and finally found a suitable way that doesn't seem to break.
Here it is if anyone else has need of it. The main difference is the HierarchicalPath. You could essentially put ANY column in the OVER(ORDER BY) function and get a perfect sort. Mine assumes I'll never have more than 99999 items.
WITH DirectPrograms (ProgramParent, ProgramID, ProgramDesc, ProgramInactive, ProgramAdmin, [Level], HierarchicalPath) AS ( SELECT p.ProgramParent, p.ProgramID, p.ProgramDesc, p.ProgramInactive, p.ProgramAdmin, [Level] = 0, HierarchicalPath = CAST('\'+RIGHT('00000' + CAST((ROW_NUMBER() OVER (ORDER BY p.ProgramDesc)) AS VARCHAR(5)), 5) AS VARCHAR(100)) FROM ys2.PROGRAM AS p WHERE ProgramParent IS NULL UNION ALL SELECT p.ProgramParent, p.ProgramID, p.ProgramDesc, p.ProgramInactive, p.ProgramAdmin, [Level] = [Level] + 1, HierarchicalPath = CAST(d.HierarchicalPath + '\'+RIGHT('00000' + CAST((ROW_NUMBER() OVER (ORDER BY p.ProgramDesc)) AS VARCHAR(5)), 5) AS VARCHAR(100)) FROM ys2.PROGRAM AS p INNER JOIN DirectPrograms AS d ON p.ProgramParent = d.ProgramID ) SELECT ProgramID, ProgramDesc, ProgramInactive, ProgramAdmin, [Level], HierarchicalPath FROM DirectPrograms ORDER BY HierarchicalPath
|
|
|
|