Queries with derived tables (I)
The focus of this lesson is derived tables. You can use derived tables when you want to break down a complex query into smaller steps. A derived table is a query which is used in the place of a table. Derived tables are a great solution if you want to create intermediate calculations that need to be used in a larger query.
In this exercise, you will calculate the maximum value of the blood glucose level for each record by age.
Deze oefening maakt deel uit van de cursus
Intermediate SQL Server
Oefeninstructies
- Return
MaxGlucosefrom the derived table. - Join the derived table to the main query on
Age.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
SELECT a.RecordId, a.Age, a.BloodGlucoseRandom,
-- Select maximum glucose value (use colname from derived table)
b.___
FROM Kidney a
-- Join to derived table
___ (SELECT Age, MAX(BloodGlucoseRandom) AS MaxGlucose FROM Kidney GROUP BY Age) b
-- Join on Age
___