Need help with complex sorting in SQL

I have a difficult sorting problem with my SQL operation. I have a table with the following columns.

No   Time    Value  
--   ----    -----
 1   0900    ''
 2   1030    ''
 3   1020    ''
 4   1010    ''
 5   1100    ''
 1   1015    'P'
 2   1045    'P'

I want to sort this table by following these steps.

  • Select the rows from the table, where Value is the value '' (empty row) and sort it by number.

  • Select the rows from the same table where Value is "P", and then sort it by time.

  • Select each row from 2) and insert into 1) by time.

The result should be something like this.

No   Time    Value  
--   ----    -----
 1   0900    ''
 1   1015    'P'
 2   1030    ''
 3   1020    ''
 4   1010    ''
 2   1045    'P'
 5   1100    ''

How to do it in SQL?

+3
source share
3 answers

, , SQL-, , . : "P", 0900 1030, 1010 1045 . , , " # 2 # 1, ", "" # 1.

, , , . , , , , , , .

0

: .

, , 3 . 1) , .

, 1015, 0900 1030. 1020 1010 ?

+2

, .
, , , .

    sort by time, value, no

. ..


You can sort by the unique number that you are building to combine your criteria. But that would be more complicated.

0
source

Source: https://habr.com/ru/post/1721064/


All Articles