I was trying to select some data from a table Local_LsIr_Temp
. I need the data in such a way that, it should be ordered by:
- Both
dir_email
anddir_tele
are present. - At least
dir_email
is present (dir_tele
can benull
orempty
) - At least
dir_tele
is present (dir_email
can benull
orempty
)
SELECT ROW_NUMBER() OVER
(PARTITION BY inst_iconum ORDER BY
CASE WHEN (dir_email = '' OR dir_email IS NULL) THEN 1 ELSE 0 END, dir_email,
CASE WHEN (dir_tele = '' OR dir_tele IS NULL) THEN 1 ELSE 0 END, dir_tele
) rn,
*
FROM Local_LsIr_Temp
I wrote the above query and the result is not coming as expected.
Attached below is sample result, the row marked in blue, should come above the row with both null values.
Please find the SQL Fiddle for this problem. SQL Fiddle
The CASE statements must be specified first. Having said that, you can achieve desired results by rewriting the ORDER BY clause: