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. OrderBy Dilema

OrderBy Dilema

Scheduled Pinned Locked Moved Database
question
6 Posts 3 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.
  • D Offline
    D Offline
    danyDude
    wrote on last edited by
    #1

    Hy, I have something like this:

    SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
    FROM QualityData
    GROUP BY Code
    ORDER BY SUM(QualityData.NrOfDefects);

    The dilema is that

    ORDER BY Defects

    does not work and

    ORDER BY SUM(QualityData.NrOfDefects);//causes a second SUM??

    Is there a better way? Thanks

    L I 2 Replies Last reply
    0
    • D danyDude

      Hy, I have something like this:

      SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
      FROM QualityData
      GROUP BY Code
      ORDER BY SUM(QualityData.NrOfDefects);

      The dilema is that

      ORDER BY Defects

      does not work and

      ORDER BY SUM(QualityData.NrOfDefects);//causes a second SUM??

      Is there a better way? Thanks

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      SELECT Code, Defects from
      (SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
      FROM QualityData
      GROUP BY Code)
      ORDER BY Defects;

      D 1 Reply Last reply
      0
      • L loyal ginger

        SELECT Code, Defects from
        (SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
        FROM QualityData
        GROUP BY Code)
        ORDER BY Defects;

        D Offline
        D Offline
        danyDude
        wrote on last edited by
        #3

        thanks

        L 1 Reply Last reply
        0
        • D danyDude

          Hy, I have something like this:

          SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
          FROM QualityData
          GROUP BY Code
          ORDER BY SUM(QualityData.NrOfDefects);

          The dilema is that

          ORDER BY Defects

          does not work and

          ORDER BY SUM(QualityData.NrOfDefects);//causes a second SUM??

          Is there a better way? Thanks

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #4

          Your initial query is fine; it will not require a second set of calculations for the sort. To verify this, run the query in Management Studio and look at the Actual Execution Plan.

          1 Reply Last reply
          0
          • D danyDude

            thanks

            L Offline
            L Offline
            loyal ginger
            wrote on last edited by
            #5

            OK. I did not have access to much resources when I answered your question. I assumed that you had problems with your original queries. However, here I have access to my environment, and I tested your query

            SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
            FROM QualityData
            GROUP BY Code
            ORDER BY SUM(QualityData.NrOfDefects);

            It works with no problem. It works with no problem if I use

            ORDER BY Defects

            So just ignore my "solution". There must be something else in your problem. My test environment is MySQL version 5.

            D 1 Reply Last reply
            0
            • L loyal ginger

              OK. I did not have access to much resources when I answered your question. I assumed that you had problems with your original queries. However, here I have access to my environment, and I tested your query

              SELECT Code, SUM(QualityData.NrOfDefects) AS Defects
              FROM QualityData
              GROUP BY Code
              ORDER BY SUM(QualityData.NrOfDefects);

              It works with no problem. It works with no problem if I use

              ORDER BY Defects

              So just ignore my "solution". There must be something else in your problem. My test environment is MySQL version 5.

              D Offline
              D Offline
              danyDude
              wrote on last edited by
              #6

              It's all fixed now. I now know for a fact(tested) that even the

              ORDER BY SUM(QualityData.NrOfDefects);

              does not cause a recalculation.

              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