I have a table like this:
| prodID | date | perm
---------------------------------
|200 |8/7/2011 | 81.742
|200 |8/7/2011 | 81.644
|200 |8/7/2011 | 81.302
|200 |8/7/2011 | 81.057
|201 |8/7/2011 | 80.932
|201 |8/7/2011 | 80.839
|201 |8/7/2011 | 80.622
|201 |8/7/2011 | 80.557
|201 |8/7/2011 | 80.541
(except a bit bigger)
Breakdown of what happens: I want to take the average of the top 10 values (and bottom 10 values) where a prodid = somevalue in this case 200.
Code:
declare @myid int
set @myid = 200
;with high as --top ten average
(
select prodid, CONVERT(CHAR(10), DATEADD(DAY, AVG(DATEDIFF(DAY, 0, CONVERT
(SMALLDATETIME, [date]))), 0), 101) as date, max(perm)as max_perm, avg(perm)
as
high_perm from
( select prodid, date, perm,
row_number() over(partition by date order by perm desc) as nt
from live_pilot_plant
where prodid = @myid) as T
where nt <= 10
group by prodid
),
low as -- bottom ten average
(
select prodid, CONVERT(CHAR(10), DATEADD(DAY, AVG(DATEDIFF(DAY, 0, CONVERT
(SMALLDATETIME, [date]))), 0),101) as date, min(perm) as min_perm, avg(perm)
as low_perm from
( select prodid, date, perm,
row_number() over(partition by date order by perm asc) as nt
from live_pilot_plant
where prodid = @myid) as T
where nt <= 10
group by prodid
)
select l.prodid, l.date, l.low_perm as low_avg, m.high_perm as high_avg,
(m.high_perm - l.low_perm) as delta
from low l
left outer join high m
on l.prodid = m.prodid
Which produces something like this:
| prodID | date | low_avg | high_avg | delta |
| 200 | 08/07/2011 | 68.752 | 79.1976 | 10.444 |
THESE NUMBERS ARE NOT ACCURATE —
This is all good and dandy – except not very versitle. I mean there are a lot of prodID, and to do one this based on prodID is too slow. How can I get the low_avg and the high_avg based on date (group by prodID)
Something like this:
| date | prodID | low_avg | high_avg | delta |
| 08/07/2011 | 200 | 60 | 80 | 20 |
| 08/07/2011 | 201 | 70 | 100 | 100 |
NOTE: You might have noticed a crazy convert infront of date. The reason is that some prodID overlap dates ie. 200 on 8/7/2011, and 8/8/2011, and I need to average the date (which is a varchar). So something like if there were 100 rows with 8/7/2011, and then 9 rows with 8/8/2011, the final query would produce the date being as /8/7/2011
The following query does it for all products at once: