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. TSQL, Order by case, and I need to order by version

TSQL, Order by case, and I need to order by version

Scheduled Pinned Locked Moved Database
helphtmlsql-servercomtutorial
11 Posts 5 Posters 29 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    This worked fine until I got to version 11, where the version numbers sorted like this - vers_id 0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9, I was expecting 0, 1, 2, 3, 4, .... I'm thinking that I need another sort clause but sure how to integrate it into my case But then I could be wrong, and my case statements may just need to be better. Everything I tried failed. Looking for help on this to keep my customers project clean.

    SELECT
    Proj_Stage,
    vers_id,
    vers_note,
    markup_price,
    sell_price,
    CONVERT(CHAR(19), sell_date, 120)
    FROM proj_vers
    WHERE proj_id = '$projectNumber'
    ORDER BY CASE
    WHEN CONVERT(VARCHAR, Proj_Stage) = 'designing' THEN 0
    WHEN CONVERT(VARCHAR, Proj_Stage) = 'engineering' THEN 1
    WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2
    WHEN CONVERT(VARCHAR, Proj_Stage) = 'finished' THEN 3
    END

    I tried this

    END, vers_id

    And I tried this

    WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2, vers_id DESC

    If it ain't broke don't fix it Discover my world at jkirkerx.com

    R J 2 Replies Last reply
    0
    • J jkirkerx

      This worked fine until I got to version 11, where the version numbers sorted like this - vers_id 0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9, I was expecting 0, 1, 2, 3, 4, .... I'm thinking that I need another sort clause but sure how to integrate it into my case But then I could be wrong, and my case statements may just need to be better. Everything I tried failed. Looking for help on this to keep my customers project clean.

      SELECT
      Proj_Stage,
      vers_id,
      vers_note,
      markup_price,
      sell_price,
      CONVERT(CHAR(19), sell_date, 120)
      FROM proj_vers
      WHERE proj_id = '$projectNumber'
      ORDER BY CASE
      WHEN CONVERT(VARCHAR, Proj_Stage) = 'designing' THEN 0
      WHEN CONVERT(VARCHAR, Proj_Stage) = 'engineering' THEN 1
      WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2
      WHEN CONVERT(VARCHAR, Proj_Stage) = 'finished' THEN 3
      END

      I tried this

      END, vers_id

      And I tried this

      WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2, vers_id DESC

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      R Offline
      R Offline
      Ron Nicholson
      wrote on last edited by
      #2

      Just a stab in the dark, but you can Cast/Convert vers_id to a int. Cast(vers_id as int) in the order by should do what you want. Assuming vers_id is will always convert to an int. varchars will be sorted in the way you are experiencing.

      Jack of all trades, master of none, though often times better than master of one.

      J 2 Replies Last reply
      0
      • R Ron Nicholson

        Just a stab in the dark, but you can Cast/Convert vers_id to a int. Cast(vers_id as int) in the order by should do what you want. Assuming vers_id is will always convert to an int. varchars will be sorted in the way you are experiencing.

        Jack of all trades, master of none, though often times better than master of one.

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Great idea! I just checked the table and vers_id was set to char. Just crafted this and it does what I want now. I didn't think of that, where it sorted as chars and not numbers. Good eye, or stab in the dark, very helpful and spot on.

        SELECT
        Proj_Stage,
        CAST(vers_id AS int),
        vers_note,
        markup_price,
        sell_price,
        CONVERT(CHAR(19), sell_date, 120)
        FROM proj_vers
        WHERE proj_id = '$projectNumber'
        ORDER BY CASE
        WHEN CONVERT(VARCHAR, Proj_Stage) = 'designing' THEN 0
        WHEN CONVERT(VARCHAR, Proj_Stage) = 'engineering' THEN 1
        WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2
        WHEN CONVERT(VARCHAR, Proj_Stage) = 'finished' THEN 3
        END, CAST(vers_id AS int) ASC

        If it ain't broke don't fix it Discover my world at jkirkerx.com

        M 1 Reply Last reply
        0
        • R Ron Nicholson

          Just a stab in the dark, but you can Cast/Convert vers_id to a int. Cast(vers_id as int) in the order by should do what you want. Assuming vers_id is will always convert to an int. varchars will be sorted in the way you are experiencing.

          Jack of all trades, master of none, though often times better than master of one.

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          Your Awesome! :)

          If it ain't broke don't fix it Discover my world at jkirkerx.com

          R 1 Reply Last reply
          0
          • J jkirkerx

            Your Awesome! :)

            If it ain't broke don't fix it Discover my world at jkirkerx.com

            R Offline
            R Offline
            Ron Nicholson
            wrote on last edited by
            #5

            Your welcome. Been there done that as they say.

            Jack of all trades, master of none, though often times better than master of one.

            1 Reply Last reply
            0
            • J jkirkerx

              Great idea! I just checked the table and vers_id was set to char. Just crafted this and it does what I want now. I didn't think of that, where it sorted as chars and not numbers. Good eye, or stab in the dark, very helpful and spot on.

              SELECT
              Proj_Stage,
              CAST(vers_id AS int),
              vers_note,
              markup_price,
              sell_price,
              CONVERT(CHAR(19), sell_date, 120)
              FROM proj_vers
              WHERE proj_id = '$projectNumber'
              ORDER BY CASE
              WHEN CONVERT(VARCHAR, Proj_Stage) = 'designing' THEN 0
              WHEN CONVERT(VARCHAR, Proj_Stage) = 'engineering' THEN 1
              WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2
              WHEN CONVERT(VARCHAR, Proj_Stage) = 'finished' THEN 3
              END, CAST(vers_id AS int) ASC

              If it ain't broke don't fix it Discover my world at jkirkerx.com

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Off topic - I would assume that "Proj_Stage" would be in another table and should have a sequence no in that table, this would eliminate the case statement in the where clause. If it is input text then you have another problem altogether!

              Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

              J 1 Reply Last reply
              0
              • M Mycroft Holmes

                Off topic - I would assume that "Proj_Stage" would be in another table and should have a sequence no in that table, this would eliminate the case statement in the where clause. If it is input text then you have another problem altogether!

                Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                J Offline
                J Offline
                jkirkerx
                wrote on last edited by
                #7

                I'm stuck with the existing database to make it backwards capable, with older construction projects. Every record has a ... project number project stage version number This table keeps track of the versions of the construction project. As typical, customers that have construction done always change their mind during the construction process, and don't understand once you pull a permit from the city, you can't change the project. But they change it anyways and then you have to get a new permit. I didn't get any documentation, nor useful comments and had to reverse engineer the old version to make this new version of the software. And it was very broken and needed fixing, thus below ...

                If it ain't broke don't fix it Discover my world at jkirkerx.com

                J 1 Reply Last reply
                0
                • J jkirkerx

                  I'm stuck with the existing database to make it backwards capable, with older construction projects. Every record has a ... project number project stage version number This table keeps track of the versions of the construction project. As typical, customers that have construction done always change their mind during the construction process, and don't understand once you pull a permit from the city, you can't change the project. But they change it anyways and then you have to get a new permit. I didn't get any documentation, nor useful comments and had to reverse engineer the old version to make this new version of the software. And it was very broken and needed fixing, thus below ...

                  If it ain't broke don't fix it Discover my world at jkirkerx.com

                  J Offline
                  J Offline
                  jsc42
                  wrote on last edited by
                  #8

                  Even if you cannot change the existing tables in the database, you should be able to create new entities. I'd still create another table with 2 columns in it as suggested by @Mycroft Holmes[^] as suggested above[^] (cols: Proj_Stage and SortSequence), indexed by Proj_Stage. Then

                  LEFT JOIN ProjStageSortSequence AS ps ON proj_vers.Proj_Stage = ps.Proj_Stage

                  and change the ORDER BY to be

                  ORDER BY ps.SortSequence, vers_id -- With conversion for vers_id if still NVARCHAR

                  . You would also need to change Proj_Stage in the SELECT to proj_vers.Proj_Stage. These changes will save loads of CONVERT() operations and should (not tested) run faster as SQL SERVER is very good at optimizing joins with low nos of rows. This leaves the existing table unaltered and would be more efficient that the query it already has. It would not be as efficient or as normalised as using the sort sequence in the existing table; but it is a stepwise improvement.

                  J 1 Reply Last reply
                  0
                  • J jsc42

                    Even if you cannot change the existing tables in the database, you should be able to create new entities. I'd still create another table with 2 columns in it as suggested by @Mycroft Holmes[^] as suggested above[^] (cols: Proj_Stage and SortSequence), indexed by Proj_Stage. Then

                    LEFT JOIN ProjStageSortSequence AS ps ON proj_vers.Proj_Stage = ps.Proj_Stage

                    and change the ORDER BY to be

                    ORDER BY ps.SortSequence, vers_id -- With conversion for vers_id if still NVARCHAR

                    . You would also need to change Proj_Stage in the SELECT to proj_vers.Proj_Stage. These changes will save loads of CONVERT() operations and should (not tested) run faster as SQL SERVER is very good at optimizing joins with low nos of rows. This leaves the existing table unaltered and would be more efficient that the query it already has. It would not be as efficient or as normalised as using the sort sequence in the existing table; but it is a stepwise improvement.

                    J Offline
                    J Offline
                    jkirkerx
                    wrote on last edited by
                    #9

                    Took a few minutes for me to absorb, but I get it now, and that's an enhancement that would be backwards compatible. I would be able to apply this new table to every other table that uses the project stages, which is pretty much about 15 more tables. I'll dabble with this tonight at home in my new home office. Thanks!

                    If it ain't broke don't fix it Discover my world at jkirkerx.com

                    1 Reply Last reply
                    0
                    • J jkirkerx

                      This worked fine until I got to version 11, where the version numbers sorted like this - vers_id 0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9, I was expecting 0, 1, 2, 3, 4, .... I'm thinking that I need another sort clause but sure how to integrate it into my case But then I could be wrong, and my case statements may just need to be better. Everything I tried failed. Looking for help on this to keep my customers project clean.

                      SELECT
                      Proj_Stage,
                      vers_id,
                      vers_note,
                      markup_price,
                      sell_price,
                      CONVERT(CHAR(19), sell_date, 120)
                      FROM proj_vers
                      WHERE proj_id = '$projectNumber'
                      ORDER BY CASE
                      WHEN CONVERT(VARCHAR, Proj_Stage) = 'designing' THEN 0
                      WHEN CONVERT(VARCHAR, Proj_Stage) = 'engineering' THEN 1
                      WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2
                      WHEN CONVERT(VARCHAR, Proj_Stage) = 'finished' THEN 3
                      END

                      I tried this

                      END, vers_id

                      And I tried this

                      WHEN CONVERT(VARCHAR, Proj_Stage) = 'construction' THEN 2, vers_id DESC

                      If it ain't broke don't fix it Discover my world at jkirkerx.com

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #10

                      Presumably no one is ever going to use version number 1.1 or even 1.0

                      J 1 Reply Last reply
                      0
                      • J jschell

                        Presumably no one is ever going to use version number 1.1 or even 1.0

                        J Offline
                        J Offline
                        jkirkerx
                        wrote on last edited by
                        #11

                        Just whole integers, maybe up to 20 if the customer gets real bad, or excited about their swimming pool while it's being built.

                        If it ain't broke don't fix it Discover my world at jkirkerx.com

                        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