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. Sql query that sorts data by top quantity per category

Sql query that sorts data by top quantity per category

Scheduled Pinned Locked Moved Database
databasetutorialquestion
6 Posts 4 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.
  • H Offline
    H Offline
    Hardz
    wrote on last edited by
    #1

    Hi, How to sort this sample data and query below by quantity and by category?: Select Qty, Category From Inv1 Order By Qty Desc, Category Qty Category 65 Cat3 55 Cat3 50 Cat2 45 Cat3 40 Cat1 30 Cat1 25 Cat1 25 Cat2 15 Cat1 15 Cat2 so that the output would be look like this: Qty Category 65 Cat3 55 Cat3 45 Cat3 50 Cat2 25 Cat2 15 Cat2 40 Cat1 30 Cat1 25 Cat1 15 Cat1 Thanks in advance! Hardz

    P _ L 3 Replies Last reply
    0
    • H Hardz

      Hi, How to sort this sample data and query below by quantity and by category?: Select Qty, Category From Inv1 Order By Qty Desc, Category Qty Category 65 Cat3 55 Cat3 50 Cat2 45 Cat3 40 Cat1 30 Cat1 25 Cat1 25 Cat2 15 Cat1 15 Cat2 so that the output would be look like this: Qty Category 65 Cat3 55 Cat3 45 Cat3 50 Cat2 25 Cat2 15 Cat2 40 Cat1 30 Cat1 25 Cat1 15 Cat1 Thanks in advance! Hardz

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      Perhaps ORDER BY Category,Qty DESC ?

      1 Reply Last reply
      0
      • H Hardz

        Hi, How to sort this sample data and query below by quantity and by category?: Select Qty, Category From Inv1 Order By Qty Desc, Category Qty Category 65 Cat3 55 Cat3 50 Cat2 45 Cat3 40 Cat1 30 Cat1 25 Cat1 25 Cat2 15 Cat1 15 Cat2 so that the output would be look like this: Qty Category 65 Cat3 55 Cat3 45 Cat3 50 Cat2 25 Cat2 15 Cat2 40 Cat1 30 Cat1 25 Cat1 15 Cat1 Thanks in advance! Hardz

        _ Offline
        _ Offline
        __TR__
        wrote on last edited by
        #3

        Select Qty, Category From Inv1 Order By Category DESC, Qty DESC

        H 1 Reply Last reply
        0
        • _ __TR__

          Select Qty, Category From Inv1 Order By Category DESC, Qty DESC

          H Offline
          H Offline
          Hardz
          wrote on last edited by
          #4

          Hi, Thanks for the reply, but what I want to accomplish is that to determine the highest Qty then prioritized the Category that has the highest Qty, from this sample Cat3 contains the highest Qty(65) then sorted from high to low, next is Cat2(50), and Cat1(40). If I changed for ex. the Qty at cat1(40) to 95 then Cat1 now is the first priority of sorting, then Cat2 and so on, so the sorting may varies from high to low no. of Qty per Category, not by Category per Qty. Regards, Hardz

          1 Reply Last reply
          0
          • H Hardz

            Hi, How to sort this sample data and query below by quantity and by category?: Select Qty, Category From Inv1 Order By Qty Desc, Category Qty Category 65 Cat3 55 Cat3 50 Cat2 45 Cat3 40 Cat1 30 Cat1 25 Cat1 25 Cat2 15 Cat1 15 Cat2 so that the output would be look like this: Qty Category 65 Cat3 55 Cat3 45 Cat3 50 Cat2 25 Cat2 15 Cat2 40 Cat1 30 Cat1 25 Cat1 15 Cat1 Thanks in advance! Hardz

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

            This works fine for SQL Server:

            SELECT Qty, t1.cat FROM Inv1 AS t1
            INNER JOIN
            (SELECT MAX(Qty) AS mq, cat FROM Inv1 GROUP BY cat) AS t2
            ON t1.cat=t2.cat
            ORDER BY mq DESC, Qty DESC

            However, you didn't fully specify what you want: when two categories have equal maxima, what should happen? The above may intertwine such categories! This wouldn't:

            SELECT Qty, t1.cat FROM Inv1 AS t1
            INNER JOIN
            (SELECT MAX(Qty) AS mq, cat FROM Inv1 GROUP BY cat) AS t2
            ON t1.cat=t2.cat
            ORDER BY mq DESC, cat, Qty DESC

            :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            H 1 Reply Last reply
            0
            • L Luc Pattyn

              This works fine for SQL Server:

              SELECT Qty, t1.cat FROM Inv1 AS t1
              INNER JOIN
              (SELECT MAX(Qty) AS mq, cat FROM Inv1 GROUP BY cat) AS t2
              ON t1.cat=t2.cat
              ORDER BY mq DESC, Qty DESC

              However, you didn't fully specify what you want: when two categories have equal maxima, what should happen? The above may intertwine such categories! This wouldn't:

              SELECT Qty, t1.cat FROM Inv1 AS t1
              INNER JOIN
              (SELECT MAX(Qty) AS mq, cat FROM Inv1 GROUP BY cat) AS t2
              ON t1.cat=t2.cat
              ORDER BY mq DESC, cat, Qty DESC

              :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              H Offline
              H Offline
              Hardz
              wrote on last edited by
              #6

              Hi Luc, Your 2nd code worked perfect for me, coz you're right for your assumption that when there's an equal maxima between these categories it will automatically sorted by category. Thank you very much. Regards, Hardz

              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