Skip to content
Advertisement

SQL: Having multiple conditional Averages between two tables

I have two tables

Sample from e.g. T1

Sample from T 2

I should return each operation name with it’s own Audit average in conditions (if it’s >= 90 as “Good” – if it’s <90 and >= 40 as “Medium” – if it’s < 40 as “Bad”) and prevent any audit with average (0)

the table should be like this

Advertisement

Answer

demo:db<>fiddle

  1. Joining both tables on id
  2. Grouping by their ids
  3. HAVING filters out the AVG = 0 groups
  4. Calculating AVG
  5. Using CASE clause to build the sections
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement