473,396 Members | 2,026 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,396 software developers and data experts.

getting top 10 by group

M
If I have a query I am writing, I can use the top 10 function to bring
back the top 10 rows. That's all fine if all I want is 10 rows.

What if I have a grouped query, and I have 5 entities that I want 10
rows each from? ie, I have 5 buildings I would want max 50 rows.

I tried Union, which works if you want them all, but I would like the
user to be able to supply me a list of buildings and be able to bring
back 10 rows for each building they ask for.

Possible?

I was thinking of creating a view, or writing a while and looping
through with a query, but I am unsure where to begin.

thanks,
M@

Jan 10 '07 #1
2 9241
You did not specify by top what but here is a solution that should work. I
simplified and just did the top 4 by the "something" column.

CREATE TABLE #Buildings
(building INT NOT NULL,
something INT NOT NULL)

CREATE TABLE #UserSelectedBuildings
(building INT NOT NULL)

INSERT INTO #Buildings (building, something) VALUES (1, 101)
INSERT INTO #Buildings (building, something) VALUES (1, 102)
INSERT INTO #Buildings (building, something) VALUES (1, 103)
INSERT INTO #Buildings (building, something) VALUES (1, 104)
INSERT INTO #Buildings (building, something) VALUES (1, 105)
INSERT INTO #Buildings (building, something) VALUES (2, 201)
INSERT INTO #Buildings (building, something) VALUES (2, 202)
INSERT INTO #Buildings (building, something) VALUES (2, 203)
INSERT INTO #Buildings (building, something) VALUES (2, 204)
INSERT INTO #Buildings (building, something) VALUES (2, 205)
INSERT INTO #Buildings (building, something) VALUES (3, 301)
INSERT INTO #Buildings (building, something) VALUES (3, 302)
INSERT INTO #Buildings (building, something) VALUES (3, 303)
INSERT INTO #Buildings (building, something) VALUES (3, 304)
INSERT INTO #Buildings (building, something) VALUES (3, 305)

INSERT INTO #UserSelectedBuildings (building) VALUES (1)
INSERT INTO #UserSelectedBuildings (building) VALUES (2)

SELECT DISTINCT B1.building, something
FROM #Buildings AS B1
INNER JOIN #UserSelectedBuildings AS U1 ON B1.building = U1.building
WHERE something <= (SELECT MAX(B2.something)
FROM #Buildings AS B2
WHERE B1.building = B2.building
AND B1.something <= B2.something
HAVING COUNT(DISTINCT something) <= 4)
ORDER BY B1.building ASC, something DESC

DROP TABLE #Buildings
DROP TABLE #UserSelectedBuildings

Regards,

Plamen Ratchev
http://www.SQLStudio.com

"M@" <ma*********@gmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
If I have a query I am writing, I can use the top 10 function to bring
back the top 10 rows. That's all fine if all I want is 10 rows.

What if I have a grouped query, and I have 5 entities that I want 10
rows each from? ie, I have 5 buildings I would want max 50 rows.

I tried Union, which works if you want them all, but I would like the
user to be able to supply me a list of buildings and be able to bring
back 10 rows for each building they ask for.

Possible?

I was thinking of creating a view, or writing a while and looping
through with a query, but I am unsure where to begin.

thanks,
M@

Jan 10 '07 #2
M@ (ma*********@gmail.com) writes:
If I have a query I am writing, I can use the top 10 function to bring
back the top 10 rows. That's all fine if all I want is 10 rows.

What if I have a grouped query, and I have 5 entities that I want 10
rows each from? ie, I have 5 buildings I would want max 50 rows.

I tried Union, which works if you want them all, but I would like the
user to be able to supply me a list of buildings and be able to bring
back 10 rows for each building they ask for.

Possible?

I was thinking of creating a view, or writing a while and looping
through with a query, but I am unsure where to begin.
select building
from (select building, row_number() over(partition by groupid,
order by height desc) AS rn
from buildings) AS b
where rn <= 10

This solution requires SQL 2005. If you are using SQL 2000, your best
bet is probably to insert data into a temp table with an IDENTITY column
and work from that one.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rene Aube | last post by:
Hi everyone, I'm incharge of updating the stats on a chl hockey teams Web Site. I would like to know if there is a way to be able to save the content of the CHL Statistics WebPage on our Server...
1
by: scott289 | last post by:
Getting the lists of users in a given Domain Group? For example, our Network people have created a group called MyAppUsers, and have added certain Users to this group. I need to get a list of...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
0
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
5
by: Michael Howes | last post by:
I'm writing a utility to manage a machines *local* accounts in c# I am getting all the users in a specific Group just fine but when I want to get some of the information on each user from their...
8
by: Cerian | last post by:
Hi there, I'm having trouble getting my 3rd dependent combo box to work in Access 2003. I have three dependent boxes named cbogroup, cbosection and cbofunction. Each one is dependent on the...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
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: 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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.