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. Select top 2 not working on Maximum Record using SQL

Select top 2 not working on Maximum Record using SQL

Scheduled Pinned Locked Moved Database
databasehelpquestion
3 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.
  • S Offline
    S Offline
    SE Ubaid
    wrote on last edited by
    #1

    Issue occrue on select top 2 like this when run the following query then show the following result SELECT u.ElectionDtlID,u.PartyShortNameEng,u.ConstituencyName,u.VotesObtained FROM tblElectionsDetail u where u.ConstituencyName='NA 108' and u.electionyear='2013' and u.VotesObtained > 0 RESULT: ID NameEng Constituency VotesObtained 10864 PPPP NA 108 29883 10865 PMLN NA 108 73789 10866 PTI NA 108 25406 10867 JI NA 108 18270 10868 Ind NA 108 85009 But now i want to get only top result of Maximum VotesObtained Candidate like this : Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='NA 108'; and electionyear='2013'; order by VotesObtained desc Then this issue the following Result OutPut Table: VotesObtained 85009 73789 this is working good and also result show correct but when i want to show max/top 2 recrod but order by asc then it's working correct like this Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='na 108' and electionyear='2013' order by VotesObtained asc the result of the query like this OutPut Table: VotesObtained 0 0 so i wan to get the result like this. VotesObtained 73789 85009 any body can help me ?

    L A 2 Replies Last reply
    0
    • S SE Ubaid

      Issue occrue on select top 2 like this when run the following query then show the following result SELECT u.ElectionDtlID,u.PartyShortNameEng,u.ConstituencyName,u.VotesObtained FROM tblElectionsDetail u where u.ConstituencyName='NA 108' and u.electionyear='2013' and u.VotesObtained > 0 RESULT: ID NameEng Constituency VotesObtained 10864 PPPP NA 108 29883 10865 PMLN NA 108 73789 10866 PTI NA 108 25406 10867 JI NA 108 18270 10868 Ind NA 108 85009 But now i want to get only top result of Maximum VotesObtained Candidate like this : Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='NA 108'; and electionyear='2013'; order by VotesObtained desc Then this issue the following Result OutPut Table: VotesObtained 85009 73789 this is working good and also result show correct but when i want to show max/top 2 recrod but order by asc then it's working correct like this Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='na 108' and electionyear='2013' order by VotesObtained asc the result of the query like this OutPut Table: VotesObtained 0 0 so i wan to get the result like this. VotesObtained 73789 85009 any body can help me ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      SELECT everything from your resultset in reverse order.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • S SE Ubaid

        Issue occrue on select top 2 like this when run the following query then show the following result SELECT u.ElectionDtlID,u.PartyShortNameEng,u.ConstituencyName,u.VotesObtained FROM tblElectionsDetail u where u.ConstituencyName='NA 108' and u.electionyear='2013' and u.VotesObtained > 0 RESULT: ID NameEng Constituency VotesObtained 10864 PPPP NA 108 29883 10865 PMLN NA 108 73789 10866 PTI NA 108 25406 10867 JI NA 108 18270 10868 Ind NA 108 85009 But now i want to get only top result of Maximum VotesObtained Candidate like this : Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='NA 108'; and electionyear='2013'; order by VotesObtained desc Then this issue the following Result OutPut Table: VotesObtained 85009 73789 this is working good and also result show correct but when i want to show max/top 2 recrod but order by asc then it's working correct like this Query: select top 2 VotesObtained from tblElectionsDetail where ConstituencyName='na 108' and electionyear='2013' order by VotesObtained asc the result of the query like this OutPut Table: VotesObtained 0 0 so i wan to get the result like this. VotesObtained 73789 85009 any body can help me ?

        A Offline
        A Offline
        Arman S
        wrote on last edited by
        #3

        select T.ElectionDtlID, T.PartyShortNameEng, T.ConstituencyName, T.VotesObtained
        FROM (select * from tblElectionsDetail where ConstituencyName='NA 108' and electionyear='2013' order by VotesObtained desc limit 2) T order by T.VotesObtained asc;

        -- Arman

        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