473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multiple Top Results

I have a query that counts how many results are in each branch out of 5000 rows. I am trying to condense even further and only show the Top 2 Branches in every State (so should reflect 100 results, theoretically). Is there a way to get this done?
Expand|Select|Wrap|Line Numbers
  1. SELECT Format(a.[Month End DT],"mm/""01""/yy") AS Date_ID, a.STATE, a.Branch, Count(a.Branch) AS Branch_Totals
  2. FROM [January Population] AS a
  3. GROUP BY a.STATE, a.Branch, a.[MONTH END DT]
Feb 21 '08 #1
8 1288
PianoMan64
374 Expert 256MB
Multiple Top Results
--------------------------------------------------------------------------------

I have a query that counts how many results are in each branch out of 5000 rows. I am trying to condense even further and only show the Top 2 Branches in every State (so should reflect 100 results, theoretically). Is there a way to get this done?

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]
The simplest way is simply insert in the SELECT Statement, TOP 2 after the word SELECT and before your FORMAT statement.

this way it will tell the query you only want 2 rows of data.

Hope that helps,

Joe P.
Feb 21 '08 #2
The simplest way is simply insert in the SELECT Statement, TOP 2 after the word SELECT and before your FORMAT statement.

this way it will tell the query you only want 2 rows of data.

Hope that helps,

Joe P.

Well thats the problem I don't want to show just the Top 2 records (I already tried that). I want it to reflect the Top 2 Branches in every State so it will have about 100 results rougly (50 States with top 2 branches = 100 results). I ran a Max Function and it gave the 1 result for each state but I need to run this with the top 2 results. Am I making sense? Can you help with doing this?
Feb 21 '08 #3
Scott Price
1,384 Expert 1GB
You'll need to use the Top 2 to find the branches, then include this in a second query that pulls the results associated with each branch.

Regards,
Scott

p.s. Welcome on board, PianoMan! Glad to have you as part of our Expert team.
Feb 21 '08 #4
You'll need to use the Top 2 to find the branches, then include this in a second query that pulls the results associated with each branch.

Regards,
Scott

p.s. Welcome on board, PianoMan! Glad to have you as part of our Expert team.

Hello Scott,

I am confused on how to do this.....The Top 2 function when is run only gives me 2 results. Are u saying that I have to make 50 queries and run the top 2 for each state that way? Can you help me with the SQL (written above) to see what you are referring to.

Thanks very much,

Matt
Feb 21 '08 #5
Scott Price
1,384 Expert 1GB
I was not thinking too deeply at the point of making that comment, sorry :-) However, I still think that you will need to do it this way.

The only way I can think of to accomplish it is to write this into VBA code, using a loop to find the Top 2 for each state. These can be set into variables which then are used to gather the other results you require.

I don't have my SQL book with me, so I can't research this assertion like I'd like, but I doubt you'll be able to do this with one or even two SQL queries.

Regards,
Scott
Feb 22 '08 #6
NeoPa
32,556 Expert Mod 16PB
Please visit this thread to see why we would rather you didn't double-post (Please do Not Double Post).

Apart from showing quite bad manners to anyone that has already spent time trying to help with your question, it also causes the moderators extra head-aches to tidy up the mess you create.

I'll close the other thread (SQL-Top Numbers) as you seem to have ignored it anyway.

Admin.
Feb 22 '08 #7
Please visit this thread to see why we would rather you didn't double-post (Please do Not Double Post).

Apart from showing quite bad manners to anyone that has already spent time trying to help with your question, it also causes the moderators extra head-aches to tidy up the mess you create.

I'll close the other thread (SQL-Top Numbers) as you seem to have ignored it anyway.

Admin.

Sorry, I checked the box and then went to Delete Subscription for the SQL Top Numbers. Is that not the way to delete it?
Feb 22 '08 #8
NeoPa
32,556 Expert Mod 16PB
OK - Understood.

In answer though - no it's not. You don't have rights to delete threads - even if you are the OP of the thread. Deleting from your subscription list simply stops the site from keeping you updated as to any activity therein. Typically this is not something you should do except perhaps in very rare cases.

Remember, as soon as it's public there are people possibly devoting time and energy to the problem. It isn't a good idea simply to stop responding to it. Although, as I said, I understand your misconception so no big deal.
Feb 22 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Brian Kelley | last post by:
I am trying to use threads and mysqldb to retrieve data from multiple asynchronous queries. My basic strategy is as follows, create two cursors, attach them to the appropriate databases and then...
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
by: Ringo Langly | last post by:
Hi all, I need to put multiple results in one field, but not sure how. Here's some sample code: select a.name, a.accountnum, a.ordernum, (select itemid from items where items_ordernum =...
3
by: wxbuff | last post by:
I have a report based on our product names that consists of two parts. Both insert data into a temporary table. 1. A single grouped set of results based on all products 2. Multiple tables based...
2
by: pengbsam | last post by:
Hello: I need to write a program that search through multiple level BOM, get all the items. It seems like a easy enough project, but when I put my hands on it and couple of hundred lines of codes...
5
by: mforema | last post by:
Hi Everyone, I want to search records by typing in multiple keywords. I currently have a search form. It has a combo box, text box, Search command button, and a subform. The combo box lists the...
0
by: redpears007 | last post by:
Morning all! :) I have a database with multiple linked tables. I have created a search form with one txt box, for entering search criteria, and a listbox for each of the tables to isplay the...
0
by: =?Utf-8?B?ZHZhbg==?= | last post by:
I'm looking for best pratice examples on how to call multiple (1 to n) web services asynchronously (server side) than merge the results from these web service in to one result set. I've written...
4
AaronL
by: AaronL | last post by:
Hello again, First I want to say thank you to all of you that have helped me in the past with all of my questions. I have gotten far in PHP and MySQL in the past two weeks. I designed a site...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.