Wednesday, April 6, 2011

Select average value and distinct values SQL

I have a table of locations with latitude, longitude and the U.S. state fields. I would like to select the average latitude and longitude for each state.

I am trying the following code but I get a syntax error on distinct.

select avg(lat), avg(lon), distinct(state) from tRealtyTrac order by state group by state
From stackoverflow
  • You don't need the distinct. If you group by state, you'll get one result for each one anyway

    Pretty sure you need the group by clause before the order by clause too.

    select state, avg(lat), avg(lon) 
    from tRealtyTrac 
    group by state 
    order by state
    
    Bryan : You are right about distinct and group by before order by.

0 comments:

Post a Comment