Subject | Re: UNION - ORDER BY |
---|---|
Author | adsieben |
Post date | 2005-12-21T10:38:22Z |
i played a little with the statement.
This version works:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2
When I add a second coloumn via coloumn name to the order by clause,
it fails:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2, NAME
But when the the second coloumn in ORDER BY clause is specified via
the coloumn number it works:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2, 3
Thanks for your advice
Andre
This version works:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2
When I add a second coloumn via coloumn name to the order by clause,
it fails:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2, NAME
But when the the second coloumn in ORDER BY clause is specified via
the coloumn number it works:
SELECT ID, 0 AS PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NULL )
UNION
SELECT ID, PARENT_ID, NAME
FROM T_COSTCENTER CC
WHERE( CCSCHEMA_ID = 1)AND( PARENT_ID IS NOT NULL )
ORDER BY 2, 3
Thanks for your advice
Andre