0

pls help me how to write sorting query.i have set of data from my sql query like picture below

enter image description here

Current data sorting are order by level,sequence,chgcode. I want my data be appear by different sorting. which is if level = 2, the sorting will be [order by level,sequence,chgcode,date] other than that will be [order by level,sequence,date,chgcode]

The actual result that i need like picture below

enter image description here

1 Answer 1

1

This is a little tricky, but you can have multiple keys for the order by:

order by level, sequence,
         (case when level = 2 then chgcode end),
         (case when level = 2 then date end),
         date,
         chgcode

The first two keys are common for both groups. The next two are specific for level = 2 (and in that order); note that the value will be NULL for both when level is not 2. The final two are for everything else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.