473,386 Members | 1,973 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,386 software developers and data experts.

Aggregate only top 20 records in a table- MSSQL2000

Hi All

I need to aggregate a query to produce the following:

Workplace Avg
M100 4.7
M120 3.45

Which would be a normal aggregate:
SELECT Workplace, Avg(VALUE)
FROM PROD
GROUP BY Workplace

However I need the average to only be based on the most recent 20
results from each of the Workplace groups.

I've never had to do something like this before so can't think of any
way to only take off the most recent 20 for each group (ordered by
Date). It doesn't really matter if there were 25 spread across 2 days
I would just cut the list at 20 VALUEs as there is no time component
invloved.

Is there any way to do a sub-query that uses select top 20 ... for
each group that could then be aggregated?

I would prefer to do it through a select statement rather than having
to use a stored procedure using and variables, etc which I can do. The
table is not huge but is growing rapidly so I'm concerned that
anything using dyamic SQL or similar would be become painfully as the
number of groups grows to 5,000 or more.

If anyone has any ideas they would be greatly appreciated.

Thanks in advance,
Bevan
Jul 20 '05 #1
2 8015
One option is to use a derived table construct like:

SELECT col1, AVG(col2)
FROM ( SELECT TOP 20 col1, col2
FROM tbl
ORDER BY col2 ) D
GROUP BY col1 ;

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #2
"Bevan Ward" <be********@hotmail.com> wrote in message
news:b9**************************@posting.google.c om...
Hi All

I need to aggregate a query to produce the following:

Workplace Avg
M100 4.7
M120 3.45

Which would be a normal aggregate:
SELECT Workplace, Avg(VALUE)
FROM PROD
GROUP BY Workplace

However I need the average to only be based on the most recent 20
results from each of the Workplace groups.

I've never had to do something like this before so can't think of any
way to only take off the most recent 20 for each group (ordered by
Date). It doesn't really matter if there were 25 spread across 2 days
I would just cut the list at 20 VALUEs as there is no time component
invloved.

Is there any way to do a sub-query that uses select top 20 ... for
each group that could then be aggregated?

I would prefer to do it through a select statement rather than having
to use a stored procedure using and variables, etc which I can do. The
table is not huge but is growing rapidly so I'm concerned that
anything using dyamic SQL or similar would be become painfully as the
number of groups grows to 5,000 or more.

If anyone has any ideas they would be greatly appreciated.

Thanks in advance,
Bevan


CREATE TABLE Prod
(
workplace VARCHAR(10) NOT NULL,
dt DATETIME NOT NULL,
value FLOAT NOT NULL,
PRIMARY KEY (workplace, dt)
)

SELECT workplace, AVG(value) AS avg_value
FROM Prod AS P
WHERE dt IN (SELECT TOP 20 dt
FROM Prod
WHERE workplace = P.workplace
ORDER BY dt DESC)
GROUP BY workplace

Regards,
jag
Jul 20 '05 #3

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

Similar topics

2
by: mike | last post by:
I have a location table that contains all North American zip codes, the city, county, state etc.. each record has a GUID. My problem is that several places in the US you will have a single city...
3
by: guhar1 | last post by:
I am no expert in sql, but I keep stubbling on this problem: I have a table t1 with 2 columns (a,b) I have a table t2 with 2 columns (c,d) I need to delete all records from t1 which have the...
3
by: eddiec | last post by:
hi everyone, I have a report in an adp that uses aggregate functions in its record source and I am trying to figure out how to filter the records displayed in the report: DoCmd.OpenReport...
3
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
1
by: Scott Gerhardt | last post by:
Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum()...
3
by: S P Arif Sahari Wibowo | last post by:
Hi! I would like to make an editable continous form, where most fields will be from table A and editable, except 1-3 fields are a glimpse into table B and uneditable. Table A relate to table B...
4
by: Paul Spratley | last post by:
Hi all Firstly this my first time posting to technical groups - so any mistakes I apologise for in advance. I am trying to count records in several secondary tables for the same run in a...
4
by: vincibleman | last post by:
Howdy all, Working my way into SQL from Access. Think I might have the hang of the basics, but would really appreciate a sanity check. The stored procedure listed below works, but I can't help...
1
by: Rimsky | last post by:
I have the need to optimize a piece of code. It updates a table, based on a detail table. The detail table (vpmod) contains 3*10^6 records, the base table (volcdet) contains 20 relevant records....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.