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 with TOP and COUNT

select with TOP and COUNT

Scheduled Pinned Locked Moved Database
databasehelp
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.
  • C Offline
    C Offline
    CandyMe
    wrote on last edited by
    #1

    Hi, please help I wanna select top 5 from table satisfying a condition and at the same time count the number of rows returned based on selection. Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz' assuming there are only 3 rows with Cruz as latname result should be: Lastname | Count Cruz 3 Cruz 3 Cruz 3 I mean my query doesn't work and have researched over the internet. Please help. thank you. :((

    Gerri

    A E T 3 Replies Last reply
    0
    • C CandyMe

      Hi, please help I wanna select top 5 from table satisfying a condition and at the same time count the number of rows returned based on selection. Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz' assuming there are only 3 rows with Cruz as latname result should be: Lastname | Count Cruz 3 Cruz 3 Cruz 3 I mean my query doesn't work and have researched over the internet. Please help. thank you. :((

      Gerri

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      Select top 5 LastName, count (*) as [Count] from TBL where lastname = 'Cruz' group by lastname order by [Count] Desc

      C 1 Reply Last reply
      0
      • A albCode

        Select top 5 LastName, count (*) as [Count] from TBL where lastname = 'Cruz' group by lastname order by [Count] Desc

        C Offline
        C Offline
        CandyMe
        wrote on last edited by
        #3

        it doesn't work, seriously. I already copy pasted and been doing some workarounds on this. using sql 2005. or what's wrong really? :(( thank u

        Gerri

        A 1 Reply Last reply
        0
        • C CandyMe

          it doesn't work, seriously. I already copy pasted and been doing some workarounds on this. using sql 2005. or what's wrong really? :(( thank u

          Gerri

          A Offline
          A Offline
          albCode
          wrote on last edited by
          #4

          Select top 5 LastName, count (*) as [Count] from TBL where lastname = 'Cruz' group by [Count]

          1 Reply Last reply
          0
          • C CandyMe

            Hi, please help I wanna select top 5 from table satisfying a condition and at the same time count the number of rows returned based on selection. Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz' assuming there are only 3 rows with Cruz as latname result should be: Lastname | Count Cruz 3 Cruz 3 Cruz 3 I mean my query doesn't work and have researched over the internet. Please help. thank you. :((

            Gerri

            E Offline
            E Offline
            Eric Dahlvang
            wrote on last edited by
            #5

            CandyMe wrote:

            Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz'

            If this isn't working, there is some other problem.

            CandyMe wrote:

            I mean my query doesn't work and have researched over the internet.

            Can you be a bit more specific as to what error message you are getting.  Maybe give us some samples of the data.  According to what you have showed us here, there is absolutely NO reason your query should'nt return what you expect.

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            C 2 Replies Last reply
            0
            • E Eric Dahlvang

              CandyMe wrote:

              Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz'

              If this isn't working, there is some other problem.

              CandyMe wrote:

              I mean my query doesn't work and have researched over the internet.

              Can you be a bit more specific as to what error message you are getting.  Maybe give us some samples of the data.  According to what you have showed us here, there is absolutely NO reason your query should'nt return what you expect.

              --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

              C Offline
              C Offline
              CandyMe
              wrote on last edited by
              #6

              I mean my query doesn't produce the result I'm expecting. For instance, I selected Top 5 with a given condition. But 3000 passed this condition. Rowcount should only be 5. Upon querying such Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz' it gives me: the top 5. but doesn't give me the correct count: 5 LastName | Count Cruz | 3000 Cruz | 3000 Cruz | 3000 Cruz | 3000 Cruz | 3000 Thank u.

              Gerri

              1 Reply Last reply
              0
              • E Eric Dahlvang

                CandyMe wrote:

                Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz'

                If this isn't working, there is some other problem.

                CandyMe wrote:

                I mean my query doesn't work and have researched over the internet.

                Can you be a bit more specific as to what error message you are getting.  Maybe give us some samples of the data.  According to what you have showed us here, there is absolutely NO reason your query should'nt return what you expect.

                --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                C Offline
                C Offline
                CandyMe
                wrote on last edited by
                #7

                I figured it some 2 hours after my last reply. I decided to create a temp table where I'd do my select top then just count the number of rows in that temp table with the whereclause, ofcourse CREATE TABLE #RowCount(SubscriberId INT) EXEC ('INSERT #RowCount' + ' SELECT TOP ' + @Top + ' (SubscriberId) FROM ' + @TableName) --get total pages EXEC ('SELECT (COUNT(*) - 1)/' + @SizeString + ' + 1 AS PageCount FROM #RowCount') --get total number of rows EXEC ('SELECT COUNT(*) FROM #RowCount') so if i selected top 10000 and only 3000 passed the condition totalrowcount is 3000; otherwise, if i selected top 2500 and 3000 passed the condition, totalrowcount is 2500 thanks! :suss: :omg:

                Gerri

                1 Reply Last reply
                0
                • C CandyMe

                  Hi, please help I wanna select top 5 from table satisfying a condition and at the same time count the number of rows returned based on selection. Select top 5 LastName, (Select count (*) from TBL where lastname = 'Cruz') as Count from TBL where lastname = 'Cruz' assuming there are only 3 rows with Cruz as latname result should be: Lastname | Count Cruz 3 Cruz 3 Cruz 3 I mean my query doesn't work and have researched over the internet. Please help. thank you. :((

                  Gerri

                  T Offline
                  T Offline
                  templarx
                  wrote on last edited by
                  #8

                  Or you can try something like this: SELECT sub.surname,count(*) as count FROM (select top 5 surname from myTable) sub GROUP BY sub.surname --[WHERE surname = 'CRUZ'] OUTPUT will bev [without the where clause] CRUZ 3 TOM 1 JACK 1 if you want to see all 5 rows [basically repeating ] SELECT sub3.surname,sub2.count FROM (select count(*) as count from (select top 5 surname from myTable where surname = 'CRUZ') sub) sub2 CROSS JOIN (select top 5 column_name from myTable where surname = 'CRUZ' ) sub3

                  C 1 Reply Last reply
                  0
                  • T templarx

                    Or you can try something like this: SELECT sub.surname,count(*) as count FROM (select top 5 surname from myTable) sub GROUP BY sub.surname --[WHERE surname = 'CRUZ'] OUTPUT will bev [without the where clause] CRUZ 3 TOM 1 JACK 1 if you want to see all 5 rows [basically repeating ] SELECT sub3.surname,sub2.count FROM (select count(*) as count from (select top 5 surname from myTable where surname = 'CRUZ') sub) sub2 CROSS JOIN (select top 5 column_name from myTable where surname = 'CRUZ' ) sub3

                    C Offline
                    C Offline
                    CandyMe
                    wrote on last edited by
                    #9

                    thanks so much. well appreciated. :)

                    Gerri

                    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