user2587878
user2587878

Reputation: 131

SQL trouble with average function

I am very new to SQL and am having a lot of trouble wrapping my head around certain things. The following code

SELECT Treated.ProgScore, Patient.Ethnicity
FROM Patient
    JOIN City
        ON Patient.ZIP = City.ZIP
    JOIN Treated
        ON Patient.SSN = Treated.PSSN
WHERE City.Cname != 'Dallas';

produces the following data. I would like to extract the average ProgScore of each ethnicity and list it from lowest to highest average ProgScore

PROGSCORE ETHNICITY


     2 Japanese                                 
     5 Caucasian                                
     9 Caucasian                                
     2 African                                  
     3 Japanese                                 
     7 Caucasian                                
    10 Japanese                                 
     8 Caucasian                                
     1 African                                  
     4 Japanese                                 
     7 Caucasian                                

Is there a way to do this? I would like my output to look like this.

Average ETHNICITY


  4.75 Japanese                                 
   7.2 Caucasian  

Thanks for any help

Upvotes: 0

Views: 24

Answers (1)

Justin Cave
Justin Cave

Reputation: 231661

It sounds like you want

SELECT avg(Treated.ProgScore), Patient.Ethnicity
FROM Patient
    JOIN City
        ON Patient.ZIP = City.ZIP
    JOIN Treated
        ON Patient.SSN = Treated.PSSN
WHERE City.Cname != 'Dallas'
GROUP BY Patient.Ethnicity
ORDER BY avg(Treated.ProgScore);

Upvotes: 1

Related Questions