sql query help

rkrause

Registered User.
Local time
Today, 14:48
Joined
Sep 7, 2007
Messages
343
I am having trouble with my query in sql 2005.
what i have is

select Producer, weight,pickup,pickupdate, scc
from table1
where pickupdate between 4-1-10 and 4-23-10

what i want to do is average all the producers SCC numbers up and divide by the pickups. im having a heck of a time getting it to come out right.

any help would be great.
 
Code:
select Producer, weight,pickup,pickupdate, scc, (Avg(scc)/Count(pickups)) As Avg
from table1
where pickupdate between 4-1-10 and 4-23-10

Try this..
 
Last edited:
When i executed this, my results came back with nothing?
 
Try putting your dates in mm/dd/yyyy format, with # on either side of both
 
Last edited:
select Producer, weight,pickup,pickupdate, scc, (Avg(scc)/Count(pickup)) As Avg
from v_pp_ProducerLoadsAndLabs
where pickupdate between '4-21-2010' and '4-23-2010' and producer = '1650'
group by Producer, weight,pickup,pickupdate, scc

thats what i have so far. It works but i attached a screenshot.
what i really want is: add up the 5 SCC values(115,120,131,117,127) and divide that by 5.

 

Attachments

  • Capture.PNG
    Capture.PNG
    8.5 KB · Views: 212
The Avg(scc) should do that for you, however you need understand what your GroupBy is doing.
It will only AVG for each unique row your groupby returns.
Since weight, pickupdate and pickup are part of your group by, you get a unique row for each combined value. Avg is doing what you ask.

select Producer, min(pickupdate) as D1,max(pickupdate) as D2 , Min(scc) as M1, Max(scc) as M2,(Avg(scc)/Count(pickup))As MyAvg
from v_pp_ProducerLoadsAndLabs
where pickupdate between'4-21-2010'and'4-23-2010'and producer ='1650'
groupby Producer, scc
 

Users who are viewing this thread

Back
Top Bottom