Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. order by clause

order by clause

Scheduled Pinned Locked Moved Database
databasetutorial
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V venkat116

    Hi All, I have one table containing few columns. LruName, ChName and Type. I want to fetch a query from the above table order by LruName, ChName, Type by using the above query I am getting output like this ------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH24 Rx DVS CH28 Rx DVS CH5 Tx DVS CH6 Tx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ---------------------------------------------------------------------------------------------------------- But I want output like this ---------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH5 Tx DVS CH6 Tx DVS CH24 Rx DVS CH28 Rx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ie I want LruName, Chname and Type to be order wise again in Type Tx ( rows ) should come first then Rx. how to write a query by getting the above results.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    if the Type field is limited to the values in your example, then a descending order would do it, hence:

    order by LruName, ChName, Type DESC

    :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    V 1 Reply Last reply
    0
    • L Luc Pattyn

      if the Type field is limited to the values in your example, then a descending order would do it, hence:

      order by LruName, ChName, Type DESC

      :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      V Offline
      V Offline
      venkat116
      wrote on last edited by
      #3

      Hi, thank u for the reply. But i want Tx rows should come first then Rx rows

      L 1 Reply Last reply
      0
      • V venkat116

        Hi, thank u for the reply. But i want Tx rows should come first then Rx rows

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        as Tx > Rx and you want Tx first, descending should do it. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        V 1 Reply Last reply
        0
        • L Luc Pattyn

          as Tx > Rx and you want Tx first, descending should do it. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          V Offline
          V Offline
          venkat116
          wrote on last edited by
          #5

          It is not working I gave like this ORDER BY LruName, ChName, Type desc

          L 1 Reply Last reply
          0
          • V venkat116

            It is not working I gave like this ORDER BY LruName, ChName, Type desc

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            in your example the Type isn't really relevant to the sort as all (LruName,ChName) combinations are unique already without the Type field. So maybe you want

            order by LruName, Type DESC, ChName

            :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            V 1 Reply Last reply
            0
            • L Luc Pattyn

              in your example the Type isn't really relevant to the sort as all (LruName,ChName) combinations are unique already without the Type field. So maybe you want

              order by LruName, Type DESC, ChName

              :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              V Offline
              V Offline
              venkat116
              wrote on last edited by
              #7

              yaah... it is working I got what I want thank u very much.

              1 Reply Last reply
              0
              • V venkat116

                Hi All, I have one table containing few columns. LruName, ChName and Type. I want to fetch a query from the above table order by LruName, ChName, Type by using the above query I am getting output like this ------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH24 Rx DVS CH28 Rx DVS CH5 Tx DVS CH6 Tx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ---------------------------------------------------------------------------------------------------------- But I want output like this ---------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH5 Tx DVS CH6 Tx DVS CH24 Rx DVS CH28 Rx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ie I want LruName, Chname and Type to be order wise again in Type Tx ( rows ) should come first then Rx. how to write a query by getting the above results.

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #8

                One of the problems you are going to have is with the ChName column. It appears as though you want CH5 to appear before CH24. In your order by clause you may have to have it

                order by .... ,length(ChName),ChName, ...

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                V J 2 Replies Last reply
                0
                • C Chris Meech

                  One of the problems you are going to have is with the ChName column. It appears as though you want CH5 to appear before CH24. In your order by clause you may have to have it

                  order by .... ,length(ChName),ChName, ...

                  Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                  V Offline
                  V Offline
                  venkat116
                  wrote on last edited by
                  #9

                  Hi, Luc Pattyn solved my problem thank you for the reply

                  1 Reply Last reply
                  0
                  • C Chris Meech

                    One of the problems you are going to have is with the ChName column. It appears as though you want CH5 to appear before CH24. In your order by clause you may have to have it

                    order by .... ,length(ChName),ChName, ...

                    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                    J Offline
                    J Offline
                    Jorgen Andersson
                    wrote on last edited by
                    #10

                    That's a good solution for a common problem. A five from me even if it wasn't what the OP wanted.

                    List of common misconceptions

                    C D 2 Replies Last reply
                    0
                    • J Jorgen Andersson

                      That's a good solution for a common problem. A five from me even if it wasn't what the OP wanted.

                      List of common misconceptions

                      C Offline
                      C Offline
                      Chris Meech
                      wrote on last edited by
                      #11

                      Thanks Jörgen.

                      Jörgen Andersson wrote:

                      even if it wasn't what the OP wanted.

                      Oddly, in the original post he indicated what output he was after and so I'm pretty sure he is going to have to do something similar to what I offered.

                      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                      J 1 Reply Last reply
                      0
                      • C Chris Meech

                        Thanks Jörgen.

                        Jörgen Andersson wrote:

                        even if it wasn't what the OP wanted.

                        Oddly, in the original post he indicated what output he was after and so I'm pretty sure he is going to have to do something similar to what I offered.

                        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #12

                        I thought so too, and was contemplating looking up a stringsplit function that does the same job in a much more tortuous way.

                        List of common misconceptions

                        1 Reply Last reply
                        0
                        • J Jorgen Andersson

                          That's a good solution for a common problem. A five from me even if it wasn't what the OP wanted.

                          List of common misconceptions

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #13

                          I have a feeling it's only going to work if all the name values start with the same prefix. Supposing I have: A1 A20 A999 C1 C99 Sorting them by length(name), name would give: A1 C1 A20 C99 A999 So it would solve the OP's specific problem where all names appear to start with CH but I'm not sure it works as a general solution.

                          J 1 Reply Last reply
                          0
                          • D David Skelly

                            I have a feeling it's only going to work if all the name values start with the same prefix. Supposing I have: A1 A20 A999 C1 C99 Sorting them by length(name), name would give: A1 C1 A20 C99 A999 So it would solve the OP's specific problem where all names appear to start with CH but I'm not sure it works as a general solution.

                            J Offline
                            J Offline
                            Jorgen Andersson
                            wrote on last edited by
                            #14

                            I believe you're quite correct. We have exactly this issue and have solved it with a separate sortcolumn. But this isn't always a viable solution, and then one has to build a natural sort function[^].

                            List of common misconceptions

                            1 Reply Last reply
                            0
                            • V venkat116

                              Hi All, I have one table containing few columns. LruName, ChName and Type. I want to fetch a query from the above table order by LruName, ChName, Type by using the above query I am getting output like this ------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH24 Rx DVS CH28 Rx DVS CH5 Tx DVS CH6 Tx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ---------------------------------------------------------------------------------------------------------- But I want output like this ---------------------------------------------------------------------------------------------------------- LruName ChName Type ADU1 CH1 Tx ADU2 CH2 Tx AHRS1 CH3 Tx AHRS2 CH4 Tx DVS CH5 Tx DVS CH6 Tx DVS CH24 Rx DVS CH28 Rx FADEC1 CH1 Tx FADEC1 CH2 Tx FADEC2 CH3 Tx FADEC2 CH4 Tx NLDW CH8 Tx OAS CH7 Tx ie I want LruName, Chname and Type to be order wise again in Type Tx ( rows ) should come first then Rx. how to write a query by getting the above results.

                              P Offline
                              P Offline
                              Prasanta_Prince
                              wrote on last edited by
                              #15

                              just the following,-
                              order by LruName, ChName, Type DESC

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups