Skip to content
Advertisement

Conditional Aggregation with multiple case and group by

The query below gives me average of case when QuoteStatusID = 6 but it I am having issues with associating the average by Street column.

QuoteTable

QuoteID QuoteStateID ProjectManager_userID Shipping_AddressID
1 6 12 56
2 6 12 56
3 26 12 56
4 6 12 18
5 26 12 18

Shipping_AddressID

56: 338 Elizabeth St
18: 83 East St

Current output 3/5 =0.60

userid fname Lname Street AcceptanceRate
12 Jon Smith 338 Elizabeth St 0.6
12 Jon Smith 83 East St 0.6

Desired output 2/3=0.66 AND 1/2=0.50

userid fname Lname Street AcceptanceRate
12 Jon Smith 338 Elizabeth St 0.66
12 Jon Smith 83 East St. 0.50

Advertisement

Answer

I think you don’t need a sub-query. Just avg as part of the query you have and use group by to give you distinct users and addresses.

Note: Short aliases make a query more readable. And you don’t need your where clause, since the join on Quote already ensures the same condition.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement