SQL-Top Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Supermansteel
    New Member
    • Dec 2007
    • 73

    SQL-Top Numbers

    Have a complicated question to ask: I have a table with 5000 rows and lists state and branch number. I am trying to run a count function (which I created as another query because didn't think I could do both in the same query) where it totals the amount of branches in each state. Which is showing me the top branches that sold insurance products.
    Code:
    SELECT a.STATE, Count(a.Branch) AS Branch Totals
    FROM [January Population] a
    GROUP BY a.STATE;
    However I am trying to pull 2 results for every state (State is listed as numerical number 1-50), so theoretically this should show 100 results listed and rank them only to pull the top 2 states/branches that sold the most insurance. Is this Possible?
  • Supermansteel
    New Member
    • Dec 2007
    • 73

    #2
    I didn't have the Correct SQL listed before I forgot to Group By Branch Also. Here is the updated SQL:
    Code:
    SELECT Format(a.[Month End DT],"mm/""01""/yy") AS Date_ID, a.STATE, a.Branch, Count(a.Branch) AS Branch_Totals
    FROM [January Population] AS a
    GROUP BY a.STATE, a.Branch, a.[MONTH END DT];
    Is there a way that I could pull the Top 2 Branches out of every state?
    Please Help...

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32661

      #3
      Registering for when I have some time...

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        It's possible you can, but we'd have no way of knowing with the information so far supplied.

        What is stored in your table (columns)?
        How would you rate one branch against another? I see no reference anywhere to any figure that might be used.
        Without this information there isn't really a question to answer.

        I assume you're trying to group items (branches) together by month date. It's hard to guess why that would be from what we know.
        If you fill in some blanks we'll see where we can get to.

        Comment

        Working...