Forum Stats

  • 3,733,968 Users
  • 2,246,850 Discussions
  • 7,856,955 Comments

Discussions

Order By Clause

user489948
user489948 Member Posts: 131
edited May 2015 in SQL

Hello,


Could anyone please help me to solve this issue....

Thanks in  advance!!!


Data in table:

product_name   product_type_id   Serial_Number   Col1   Col2......

name_1                     1                  1                      val    val

name_1                     1                  2                      val    val

name_1                     1                  3                      val    val

name_1                     1                  4                      val    val

name_1                     1                  5                      val    val

name_2                     2                  1                      val    val

name_2                     2                  2                      val    val

name_2                     2                  3                      val    val

name_2                     2                  4                      val    val

name_3                     1                  1                      val    val

name_3                     1                  2                      val    val

name_4                     3                  1                      val    val

name_4                     3                  2                      val    val

name_4                     3                  3                      val    val

......

Sort requirement:

sort on Serial_Number,

if product_type_id is 1 or 3, sort by Serial_Number ascending

if product_type_id is 2 or 4 or 5, sort by Serial_Number descending

The expected output should like:

product_name   Serial_Number       Col1        Col2......

name_1              1                         val_a1     val_b1

name_1              2                         val_a2     val_b2

name_1              3                         val_a3     val_b3

name_1              4                         val_a4     val_b4

name_1              5                         val_a5     val_b5

name_2             4                          val_c4     val_d4

name_2             3                          val_c3     val_d3

name_2             2                          val_c2     val_d2

name_2             1                          val_c1     val_d1

name_3             1                          val_e1     val_f1

name_3             2                          val_e2     val_f2

name_4             1                           val_g1    val_h1

name_4             2                           val_g2    val_h2

name_4             3                           val_g3    val_h3

Sign In or Register to comment.