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. Getting Top 10 results per month/year

Getting Top 10 results per month/year

Scheduled Pinned Locked Moved Database
databasexmlquestion
8 Posts 7 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.
  • _ Offline
    _ Offline
    _Joao_
    wrote on last edited by
    #1

    Hi there. I'm having some trouble trying to do the following: I have some data in my DB: ID (PK) type creation_date What I want to do is to get the top x results by month/year of creation_date. I already have a query to count the number of ID's in some month/year, grouped by type, with a 'group by'. This would return the number of ID's with type T in month mm of year yy, for all months and years that appear on creation_date. Now I need to return the top X results in month 01/2007, top X results in month 02/2007 and so on. I already make this happen, with a stored procedure, but I'm using another stored procedure to get all the data to a XML file, and I'm not being able to call the stored procedure with the top from the stored procedure that will generate the XML... Any ideia, anyone? :) Joao

    B 1 Reply Last reply
    0
    • _ _Joao_

      Hi there. I'm having some trouble trying to do the following: I have some data in my DB: ID (PK) type creation_date What I want to do is to get the top x results by month/year of creation_date. I already have a query to count the number of ID's in some month/year, grouped by type, with a 'group by'. This would return the number of ID's with type T in month mm of year yy, for all months and years that appear on creation_date. Now I need to return the top X results in month 01/2007, top X results in month 02/2007 and so on. I already make this happen, with a stored procedure, but I'm using another stored procedure to get all the data to a XML file, and I'm not being able to call the stored procedure with the top from the stored procedure that will generate the XML... Any ideia, anyone? :) Joao

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

      use union example: SELECT * FROM Table1 UNION SELECT * FROM Table2


      I Love SQL

      F P 2 Replies Last reply
      0
      • B Blue_Boy

        use union example: SELECT * FROM Table1 UNION SELECT * FROM Table2


        I Love SQL

        F Offline
        F Offline
        Frank Kerrigan
        wrote on last edited by
        #3

        That is total rubbish!!

        Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]

        C 1 Reply Last reply
        0
        • F Frank Kerrigan

          That is total rubbish!!

          Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          I concur.


          Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

          1 Reply Last reply
          0
          • B Blue_Boy

            use union example: SELECT * FROM Table1 UNION SELECT * FROM Table2


            I Love SQL

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            How does this solve the problem? Did you even read his post?

            Deja View - the feeling that you've seen this post before.

            _ 1 Reply Last reply
            0
            • P Pete OHanlon

              How does this solve the problem? Did you even read his post?

              Deja View - the feeling that you've seen this post before.

              _ Offline
              _ Offline
              _Joao_
              wrote on last edited by
              #6

              Well, that will not solve my problem, because I do not know exactly how many months/years there could be in the first place. I do have something like this: select count(ID), type, year(creation_date), month(creation_date) from table1 group by year(creation_date), month(creation_date), type which returns the count per type/month/year... After that I just want to show top 5 types per month/year... As I said, I could do something like what I want with a stored procedure, with a loop foreach month/year, but I do not know how to run a stored procedure from inside another stored procedure... I've tried 'exec sp_getdata' from inside the other stored procedure, but that didn't work... Joao

              Y A 2 Replies Last reply
              0
              • _ _Joao_

                Well, that will not solve my problem, because I do not know exactly how many months/years there could be in the first place. I do have something like this: select count(ID), type, year(creation_date), month(creation_date) from table1 group by year(creation_date), month(creation_date), type which returns the count per type/month/year... After that I just want to show top 5 types per month/year... As I said, I could do something like what I want with a stored procedure, with a loop foreach month/year, but I do not know how to run a stored procedure from inside another stored procedure... I've tried 'exec sp_getdata' from inside the other stored procedure, but that didn't work... Joao

                Y Offline
                Y Offline
                yahao
                wrote on last edited by
                #7

                A non-clever method is to use temp table instead of a single sql script. :p

                1 Reply Last reply
                0
                • _ _Joao_

                  Well, that will not solve my problem, because I do not know exactly how many months/years there could be in the first place. I do have something like this: select count(ID), type, year(creation_date), month(creation_date) from table1 group by year(creation_date), month(creation_date), type which returns the count per type/month/year... After that I just want to show top 5 types per month/year... As I said, I could do something like what I want with a stored procedure, with a loop foreach month/year, but I do not know how to run a stored procedure from inside another stored procedure... I've tried 'exec sp_getdata' from inside the other stored procedure, but that didn't work... Joao

                  A Offline
                  A Offline
                  andyharman
                  wrote on last edited by
                  #8

                  Sql-Server 2005 introduced a bunch of new functions for ranking and windowing data. Try Using Ranking and Windowing Functions in SQL Server 2005[^]

                  If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

                  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