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 Server DataBase

Sql Server DataBase

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
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.
  • U Offline
    U Offline
    User 12075601
    wrote on last edited by
    #1

    Hi I have a table Student like below SName Subject Marks Date A AAA 10 2011-01-01 B AAB 20 2012-04-01 B AAB 20 2012-03-01 I need sql query to find the 3rd highest marks for each subject. please do the needful Cloumname DATA Sname -- A, B, B Subject - AAA, AAB, AAB Marks -- 10,20,20 Date --2011-01-01,2012-04-01,2012-03-01

    C R 2 Replies Last reply
    0
    • U User 12075601

      Hi I have a table Student like below SName Subject Marks Date A AAA 10 2011-01-01 B AAB 20 2012-04-01 B AAB 20 2012-03-01 I need sql query to find the 3rd highest marks for each subject. please do the needful Cloumname DATA Sname -- A, B, B Subject - AAA, AAB, AAB Marks -- 10,20,20 Date --2011-01-01,2012-04-01,2012-03-01

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      We are not going to do your homework for you, but will give you hints There are several ways you could do this - the classic way would be by using the TOP[^] predicate and subqueries, or using the ROW_NUMBER()[^] function, partitioned by subject and ordering by mark.

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      1 Reply Last reply
      0
      • U User 12075601

        Hi I have a table Student like below SName Subject Marks Date A AAA 10 2011-01-01 B AAB 20 2012-04-01 B AAB 20 2012-03-01 I need sql query to find the 3rd highest marks for each subject. please do the needful Cloumname DATA Sname -- A, B, B Subject - AAA, AAB, AAB Marks -- 10,20,20 Date --2011-01-01,2012-04-01,2012-03-01

        R Offline
        R Offline
        RNA Team
        wrote on last edited by
        #3

        Try this

        DECLARE @T TABLE(SName VARCHAR(20), Subject VARCHAR(20), Marks INT, ExamDate DATE)
        INSERT INTO @T
        SELECT 'A', 'Subject1', 77, '2011-01-01' UNION ALL
        SELECT 'A', 'Subject2', 97, '2011-01-01' UNION ALL
        SELECT 'B', 'Subject1', 80 ,'2012-04-01' UNION ALL
        SELECT 'B', 'Subject2', 70, '2012-03-01' UNION ALL
        SELECT 'C', 'Subject1', 44, '2011-01-01' UNION ALL
        SELECT 'C', 'Subject2', 90, '2011-01-01' UNION ALL
        SELECT 'D', 'Subject1', 79 ,'2012-04-01' UNION ALL
        SELECT 'D', 'Subject2', 66, '2012-03-01'

        SELECT X.*
        FROM ( SELECT
        t.*
        ,Rn = DENSE_RANK() OVER(PARTITION BY t.Subject ORDER BY t.Marks DESC)
        FROM @T t) X WHERE X.Rn = 3

        /*

        SName Subject Marks ExamDate Rn
        A Subject1 77 2011-01-01 3
        B Subject2 70 2012-03-01 3

        */

        M 1 Reply Last reply
        0
        • R RNA Team

          Try this

          DECLARE @T TABLE(SName VARCHAR(20), Subject VARCHAR(20), Marks INT, ExamDate DATE)
          INSERT INTO @T
          SELECT 'A', 'Subject1', 77, '2011-01-01' UNION ALL
          SELECT 'A', 'Subject2', 97, '2011-01-01' UNION ALL
          SELECT 'B', 'Subject1', 80 ,'2012-04-01' UNION ALL
          SELECT 'B', 'Subject2', 70, '2012-03-01' UNION ALL
          SELECT 'C', 'Subject1', 44, '2011-01-01' UNION ALL
          SELECT 'C', 'Subject2', 90, '2011-01-01' UNION ALL
          SELECT 'D', 'Subject1', 79 ,'2012-04-01' UNION ALL
          SELECT 'D', 'Subject2', 66, '2012-03-01'

          SELECT X.*
          FROM ( SELECT
          t.*
          ,Rn = DENSE_RANK() OVER(PARTITION BY t.Subject ORDER BY t.Marks DESC)
          FROM @T t) X WHERE X.Rn = 3

          /*

          SName Subject Marks ExamDate Rn
          A Subject1 77 2011-01-01 3
          B Subject2 70 2012-03-01 3

          */

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

          And I have a question in biology that needs a response for, can you do the needful for that one as well. This will be pay back when the idjit ends up working with you!

          Never underestimate the power of human stupidity RAH

          R 1 Reply Last reply
          0
          • M Mycroft Holmes

            And I have a question in biology that needs a response for, can you do the needful for that one as well. This will be pay back when the idjit ends up working with you!

            Never underestimate the power of human stupidity RAH

            R Offline
            R Offline
            RNA Team
            wrote on last edited by
            #5

            Respected Sir, Hope you are doing well. The question being asked can be solved in 2 ways, as per my understanding. #1: If the data storage format is like StudentRoll,Name,English,Physics,Biology,Maths etc. (which he didn't asked ) In this case, in-order to flatten the data, first we will use UNPIVOT . Once done, the next step will be to use Row_Number() and get the N-th highest marks. Finally , to get the original structure back, we should apply PIVOT. #2: If the storage format is like SName,Subject,Marks(which he asked ) In this case, since the data is already flattened by Subjects, we must apply the RANKING function (as I applied Row_Number()) here and get the N-th highest one. Kindly let me know, if my understanding of your question is correct in which case please provide the feedback of the response.

            M 1 Reply Last reply
            0
            • R RNA Team

              Respected Sir, Hope you are doing well. The question being asked can be solved in 2 ways, as per my understanding. #1: If the data storage format is like StudentRoll,Name,English,Physics,Biology,Maths etc. (which he didn't asked ) In this case, in-order to flatten the data, first we will use UNPIVOT . Once done, the next step will be to use Row_Number() and get the N-th highest marks. Finally , to get the original structure back, we should apply PIVOT. #2: If the storage format is like SName,Subject,Marks(which he asked ) In this case, since the data is already flattened by Subjects, we must apply the RANKING function (as I applied Row_Number()) here and get the N-th highest one. Kindly let me know, if my understanding of your question is correct in which case please provide the feedback of the response.

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

              My apologies, I was being sarcastic. The point I was trying to make is that Chris had given him the pointers where he could research and learn how to do the job. Whereas you gave him the solution, which does not help him learn. If it was a difficult question then the example may have been the best way to answer. As it is an extremely basic question the OP needs to do some learning/research/school work rather than cut and paste your answer. Asking someone to "do the needful" is just plain rude.

              Never underestimate the power of human stupidity RAH

              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