Solved

sql problem with order / group

Posted on 2013-01-25
8
282 Views
Last Modified: 2013-01-25
Hi.

I have this script, but would like to order by ID with parentID null desc.
"I tried  order by id desc" but it dosent work. Can anybody help?

 with product_hierarchy as
(
select top 1000 ID, ArtikelID, parentID, tekst ,1 lev , cast(id as varchar(max)) as path from
H2581_Shop.ArtiklerKommentar where parentid is null and artikelid = 1 order by id desc
union all
select p.ID, p.ArtikelID, p.parentID, p.tekst ,ph.lev+1 , ph.path+'.'+ cast(p.id as varchar(max)) as path from H2581_Shop.ArtiklerKommentar p join product_hierarchy ph on ph.id = p.parentid
)
select * from product_hierarchy order by path


Please look at attrached file.
It shows my table.

I want to order the table so the order would be:
ID:
7
9
1
2
4
6
3
8
5
Udklip.PNG
0
Comment
Question by:bongii
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38820512
<air code>

>but would like to order by ID with parentID null desc.
Give this a whirl..

ORDER BY CASE WHEN ParentID IS NULL THEN 0 ELSE 1 END, ID

btw the series of numbers you provided doens't match the above > statement.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 38820527
Hi,

Is this how you want to sort? - its desc by first, then asc by second and third values in the path.

ID path
7 7
9 7.9
1 1
2 1.2
4 1.2.4
6 1.2.6
3 1.3
8 1.3.8
5 1.5

Regards
  David
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38820575
Your order by in the first query is not guaranteed to carry through to the final query, so try this to carry it through and order by the parents in decending order then the dependent records in ID order
 with product_hierarchy as 
(
select top 1000 ID, ArtikelID, parentID, tekst ,1 lev , cast(id as varchar(max)) as path, parentID as MasterParentID, 0 As ParentFirst from 
H2581_Shop.ArtiklerKommentar where parentid is null and artikelid = 1 order by id desc
union all
select p.ID, p.ArtikelID, p.parentID, p.tekst ,ph.lev+1 , ph.path+'.'+ cast(p.id as varchar(max)) as path, ph.MasterParentID, 1 As ParentFirst from H2581_Shop.ArtiklerKommentar p join product_hierarchy ph on ph.id = p.parentid
) 
select * from product_hierarchy order by MasterParentID desc, ParentFirst, path

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:bongii
ID: 38820641
Hi guys.
Thank for reply, but I want the order of the output to be:

ID 7: ParentID is null
All childs with path 7*
ID 1: ParentID is null
All childs with path 1*

It is used to replies to articles and i want the newest reply first.
0
 

Author Comment

by:bongii
ID: 38820646
dtodd: Yes thats how I want to sort.
What do you mean by: its desc by first, then asc by second and third values in the path.
0
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38820651
try this, I had not had a chance to build a test scenario the first time and used the wrong ID for my MasterParent value
 with product_hierarchy as 
(
select top 1000 ID, ArtikelID, parentID, tekst ,1 lev , cast(id as varchar(max)) as path, ID as MasterParentID, 0 As ParentFirst from 
H2581_Shop.ArtiklerKommentar where parentid is null and artikelid = 1 order by id desc
union all
select p.ID, p.ArtikelID, p.parentID, p.tekst ,ph.lev+1 , ph.path+'.'+ cast(p.id as varchar(max)) as path, ph.MasterParentID, 1 As ParentFirst from H2581_Shop.ArtiklerKommentar p join product_hierarchy ph on ph.id = p.parentid
) 
select * from product_hierarchy order by MasterParentID desc, ParentFirst, path

Open in new window

Resulst in Results
0
 
LVL 35

Expert Comment

by:David Todd
ID: 38820658
Hi,

>> newest reply first
That you aren't doing as I read your data - newest conversation first, then first reply ... last reply.

Suggestion:
Split the path into root and path, then the order by becomes
order by
    root desc
    , path asc

hth
  David
0
 

Author Closing Comment

by:bongii
ID: 38820682
Very nice. Thumbs up for understanding my tricky description :)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question