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. Grouped ordered precise select statement?

Grouped ordered precise select statement?

Scheduled Pinned Locked Moved Database
databasecollaborationhelpquestion
9 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.
  • E Offline
    E Offline
    Emmet_Brown
    wrote on last edited by
    #1

    Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

    E L M B 5 Replies Last reply
    0
    • E Emmet_Brown

      Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

      E Offline
      E Offline
      Emmet_Brown
      wrote on last edited by
      #2

      I would appreciate any answer that makes the full query above but with some brute coding, I can handle myself in .cs part of the project if you only tell me how to get a "SECOND HIGHEST" from a table for the highest of A's, I'm using SELECT ID as row, Team, Point FROM cadet_games.volleyballpool8 WHERE Pool = 'A' and Point=(select max(point) from cadet_games.volleyballpool8) If I don't get answer soon enough, I'll have to use multiple dataTables to concat later. But still I need some "second highest" query :(

      1 Reply Last reply
      0
      • E Emmet_Brown

        Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

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

        Hi, question: what is the second highest in this collection: 4, 4, 2, 1? is it 4 or 2? Anyway, you should look here[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        E 2 Replies Last reply
        0
        • L Luc Pattyn

          Hi, question: what is the second highest in this collection: 4, 4, 2, 1? is it 4 or 2? Anyway, you should look here[^]. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          E Offline
          E Offline
          Emmet_Brown
          wrote on last edited by
          #4

          well, there are lots of fields in this table and some of them are "for, against"...etc I'm adding those to my query as well, now, just think that Point column is unique :D btw, thanks for the answer, I'll have a look at it

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, question: what is the second highest in this collection: 4, 4, 2, 1? is it 4 or 2? Anyway, you should look here[^]. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            E Offline
            E Offline
            Emmet_Brown
            wrote on last edited by
            #5

            Hey! :D be sure that I've done that before I ask to you guys =) I'm googling and querying at the same time :)

            1 Reply Last reply
            0
            • E Emmet_Brown

              Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

              E Offline
              E Offline
              Emmet_Brown
              wrote on last edited by
              #6

              Couldn't find a single query to do all above, but decided to do it C# style thanks all anyway :)

              L 1 Reply Last reply
              0
              • E Emmet_Brown

                Couldn't find a single query to do all above, but decided to do it C# style thanks all anyway :)

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

                why is that? the first answer in the first Google hit shows a principle that should work well (assuming you want the second highest distinct value): have a subquery determining the maximum value, then query for the largest one that is less than the maximum. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                • E Emmet_Brown

                  Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

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

                  Impatient bugger aren't you. Look into row_number and partition over in BOL. With these 2 commands you can select the results grouped by team and ordered by points with a ranking per team. With that sub query you simply select the top 4 record to give you the results. I can think of 2 other methods of achieving this in TSQL bit the above key word should give you the pointers for research.

                  Never underestimate the power of human stupidity RAH

                  1 Reply Last reply
                  0
                  • E Emmet_Brown

                    Hello everyone I have a table named "volleyballpool8" it has columns as ID, Pool, TeamNo, Team and Point There are 11 rows in this table and there are 4 different "Pool"s, as A,B,C and D Now I need to make a SELECT statement to form a table, which: 1-Gets the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='A's 2-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='B's 3-adds the ID, Team and Point of the row with the HIGHEST "Point" value of Pool='C's 4-adds the ID, Team and Point of the row with the SECOND HIGHEST "Point" value of Pool='D's Therefore, in the end, I'll have a 4 rowed table I'm somewhat new to SQL and I got confused... Can anybody help? :doh: btw: all of the fields are strings

                    B Offline
                    B Offline
                    Blue_Boy
                    wrote on last edited by
                    #9

                    As Luc Pattyn said get top 1 value which is lower then max value, Look this example and hope you will figure out with your case: SELECT MAX(vlera) HighestValue, (SELECT TOP 1 m.vlera FROM mytable m WHERE m.vlera < (SELECT MAX(vlera) FROM mytable) ORDER BY vlera DESC) secondHihgestValue FROM mytable


                    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.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