Wednesday, March 07, 2007

SQL ORDER BY mixing 2 columns

SELECT retrn, speco, grpdc, Case When grpdc <> ' ' Then grpdc Else speco End As column4 FROM mrcdct15 ORDER BY 4

The records will be sorted by using values from both speco and grpdc columns.

Note this is only used to demonstrate the use of CASE WHEN … THEN and the use of merging 2 columns. This maybe really useful when one needs to join 2 files and sort by columns in 2 files:

File1 colx File2 coly

1. A
2. A
3. C
4. D
5. D B

And you want the records to sorted as :

1, 2, 5, 3, 4

0 Comments:

Post a Comment

<< Home