Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

weird sort order

Status
Not open for further replies.

piti

Technical User
Apr 12, 2001
627
SK
I'm getting not quite what i would like to from a my database and do not know why

here's the problem description:
i have data in a table, the data is some kind of a tree, with 5 levels of nesting
the structure of the data is following
- every level has its name and id (AtrName, IdAtr)
- there are 5 levels of data
- in the table, there is also the top level stored (AtrName_top, IdAtr_top)
i would like having the SELECT returning the tree like this

top1
top1 - L1(1)
top1 - L1(1) - L2(1)
top1 - L1(1) - L2(2)
top1 - L1(2)
top1 - L1(2) - L2(1)
top1 - L1(3)
top1 - L1(4) - L2(1)
top1 - L1(4) - L2(2) .....

when using code shown below, the result is not sorted in order i need it to be
i'm getting something like (the "error" is show with an *)
top1
*top1 - L1(1) - L2(1)
*top1 - L1(1)
top1 - L1(1) - L2(2)
top1 - L1(2)
top1 - L1(2) - L2(1)
top1 - L1(3)
top1 - L1(4) - L2(1)
top1 - L1(4) - L2(2) .....
only some of the returning records are not sorted properly

the code is:
SELECT * FROM Tree
ORDER BY
AtrName_top, IdAtr_top,
CASE
WHEN IdAtr_04 IS NOT NULL THEN AtrName_03
WHEN IdAtr_04 IS NOT NULL THEN IdAtr_03
WHEN IdAtr_03 IS NOT NULL THEN AtrName_02
WHEN IdAtr_03 IS NOT NULL THEN IdAtr_02
WHEN IdAtr_02 IS NOT NULL THEN AtrName_01
WHEN IdAtr_02 IS NOT NULL THEN IdAtr_01
WHEN IdAtr_01 IS NOT NULL THEN AtrName_00
WHEN IdAtr_01 IS NOT NULL THEN IdAtr_00
END,
CASE
WHEN IdAtr_04 IS NOT NULL THEN AtrName_02
WHEN IdAtr_04 IS NOT NULL THEN IdAtr_02
WHEN IdAtr_03 IS NOT NULL THEN AtrName_01
WHEN IdAtr_03 IS NOT NULL THEN IdAtr_01
WHEN IdAtr_02 IS NOT NULL THEN AtrName_00
WHEN IdAtr_02 IS NOT NULL THEN IdAtr_00
ELSE NULL
END,
CASE
WHEN IdAtr_04 IS NOT NULL THEN AtrName_01
WHEN IdAtr_04 IS NOT NULL THEN IdAtr_01
WHEN IdAtr_03 IS NOT NULL THEN AtrName_00
WHEN IdAtr_03 IS NOT NULL THEN IdAtr_00
ELSE NULL
END,
CASE
WHEN IdAtr_04 IS NOT NULL THEN AtrName_00
WHEN IdAtr_04 IS NOT NULL THEN IdAtr_00
ELSE NULL
END

anyone who knows why this happens?
plz help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top