TrentGreenawalt
Technical User
I am trying to get an avg of the "avg fast slow" days of the latest (top3) for each customer ID. Given the data below is a sample table, I actually have 1000's of customers and over 100k records in this table. As you will see there is no entry in a given month if there are no bills paid, so I am not able to use a greater than query. I have played around with TOP 3 and that works for a particular customer, but not for all customers.
Customer ID Year Month Avg fast slow days
1234 2008 5 -5
1234 2008 4 -3
1234 2008 2 2
1234 2008 1 3
1234 2007 11 1
1234 2007 9 -3
2345 2008 5 -2
2345 2008 3 5
2345 2008 1 -2
2345 2007 12 1
9999 2008 5 2
9999 2008 4 1
9999 2008 3 5
Here is the query so far:
This will give the avg of all of the slow fast day columns, but I only need the top 3 most recent:
Select customer_id, avg(avg_fast_Slow_days)
from Table1
Group by customer_id, avg_fast_slow_days
Order by 2 desc
Then this is where I am at with the Top 3:
Select top 3 customer_id, avg(AVG_Fast_Slow_Days), year_invoiced, month_invoiced
from table1
where customer_ID = 9999
Group by customer_Id, avg_fast_slow_days, year_invoiced, Month_invoiced
Order by 3 desc,4 desc
I know this an obvious one line of code fix, but I just can't seem to get it.
I am simply looking to output
Cust ID | 3 months Avg
9999 2.5
1234 1.5
I made up these numbers but you should get the gist.
Thanks for the help,
Trent
Customer ID Year Month Avg fast slow days
1234 2008 5 -5
1234 2008 4 -3
1234 2008 2 2
1234 2008 1 3
1234 2007 11 1
1234 2007 9 -3
2345 2008 5 -2
2345 2008 3 5
2345 2008 1 -2
2345 2007 12 1
9999 2008 5 2
9999 2008 4 1
9999 2008 3 5
Here is the query so far:
This will give the avg of all of the slow fast day columns, but I only need the top 3 most recent:
Select customer_id, avg(avg_fast_Slow_days)
from Table1
Group by customer_id, avg_fast_slow_days
Order by 2 desc
Then this is where I am at with the Top 3:
Select top 3 customer_id, avg(AVG_Fast_Slow_Days), year_invoiced, month_invoiced
from table1
where customer_ID = 9999
Group by customer_Id, avg_fast_slow_days, year_invoiced, Month_invoiced
Order by 3 desc,4 desc
I know this an obvious one line of code fix, but I just can't seem to get it.
I am simply looking to output
Cust ID | 3 months Avg
9999 2.5
1234 1.5
I made up these numbers but you should get the gist.
Thanks for the help,
Trent