Presentation is loading. Please wait.

Presentation is loading. Please wait.

NURS6803 Clinical DB Design Katherine Sward, PhD, RN

Similar presentations


Presentation on theme: "NURS6803 Clinical DB Design Katherine Sward, PhD, RN"— Presentation transcript:

1 NURS6803 Clinical DB Design Katherine Sward, PhD, RN
More SQL NURS6803 Clinical DB Design Katherine Sward, PhD, RN 1

2 Planning the query (the process I use)
Write the template Select From Where Determine which table(s) have the data I need What columns do I want to return What do I need in order to determine what rows I want

3 Planning the query If it’s complex, I do just a portion at a time
Once that’s working the way I want, then do the next part You’ll see this in action as we work through examples

4 Subqueries In relational theory… the result of manipulating a relation (or multiple relations) is also a relation We think of relation as a table But the result of a query is also a “relation” So we can use the result of a query like it’s a table (temporary table – does not copy data to a new physical table) The result of one query can be the “source” for another query

5 View When you save the definition of a query (save the query, in Access) you create a VIEW. A view is simply a saved query definition You do not have to save the query as a view in order to base another query on it’s results

6 Subqueries You can embed a complete SELECT statement (complete query) within another query This is called a nested query (or subquery) Subquery returns A single value A single row of data with multiple columns Multiple rows of data with one or more columns

7 Subqueries Suppose all the columns you want to display are in one table But the criteria to determine what ROWS to display are in another table Let’s say we want patient ID and name for everyone who is the same race as Deanna Troi This is all in the demog table

8 Build the query Names are formatted Last, First
So first find Deanna’s race SELECT race FROM demog WHERE name = 'Troi, Deanna' NOTE: straight quotes, not curvy ones! YES ' NO ‘ or “ MAYBE "

9 Build the query Now find the people who have the same value as Deanna for race SELECT pt_id, name FROM demog WHERE race = (SELECT race WHERE name = 'Troi, Deanna')

10 Subqueries Now, suppose the criterion is in a different table
Let’s say we want patient ID and name for every patient who has a low WBC (less than 3.8). We don’t want the wbc value, just the ID and name.

11 What tables?

12 What tables I only want to return columns from demog
But the rows will be determined by data in the vitals table The things we want to return are the outer query The filter is the subquery So we will use vitals for the subquery

13 WHERE WBC < 3.8; returns a list of pt_id
SELECT pt_id FROM blood WHERE WBC < 3.8; returns a list of pt_id pt_id 103 133 15 155

14 Now move up a layer Put parentheses around the subquery and get rid of the semi-colon (SELECT pt_id FROM blood WHERE WBC < 3.8)

15 IN We will use the keyword IN to tell the database that we are providing a list You saw IN with a list previously when we made a list of zip codes Our query returned a list, so we can use the query in place of typing out the results

16 The whole query SELECT pt_id, name FROM demog WHERE pt_id IN
FROM blood WHERE WBC < 3.8); pt_id name 103 Cole, Nat-King 133 Newman, Paul 15 Drexel, Clyde 155 Robert, Jake 2 Minamino, Yoko 213 Troi, Deanna 214 Devil, Tazmanian 247 null, null 314 378 396 454 485

17 The whole query SELECT pt_id, name FROM demog WHERE pt_id IN
FROM blood WHERE WBC < 3.8) ORDER BY cint(pt_id); pt_id name 2 Minamino, Yoko 15 Drexel, Clyde 103 Cole, Nat-King 133 Newman, Paul 155 Robert, Jake 213 Troi, Deanna 214 Devil, Tazmanian 247 null, null 314 378 396 454 485

18 Subqueries Suppose your criteria for “what rows” includes an aggregate function or a list of items You can use any legal comparator that makes sense for the query (less than, greater than, etc.) But be careful. Know whether your subquery returns one row, or multiple

19 This time everything we want is in the vitals table
For example, we want to know the pt_id for everyone who has a SBP higher than the average SBP for the group This time everything we want is in the vitals table We will use a subquery to find the average SBP Then use the outer query to find everyone higher than that

20 SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals)
Note that the field in the outer table is to the LEFT of the > symbol, and the subquery is to the right SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals)

21 We can even do math with the subquery
We can even do math with the subquery. Suppose we wanted people who have an SBP higher than 1.5 times the average SBP for the group….

22 SELECT pt_id FROM vitals WHERE SBP > 1.5 * (SELECT avg(SBP) FROM vitals)

23 Other uses You can use a subquery in the SELECT clause
You can use a subquery in the FROM clause (this is a view when used in the FROM clause)

24 Getting tricky… Suppose we want to know how each person’s highest SBP compares to the average for the group—we want to know how much above average is the highest SBP, for those folks who have higher-than-average SBP

25 Building a little at a time
Start with the query that finds the average SBP for the group SELECT avg(SBP) FROM vitals

26 Building a little at a time
Who has higher than average SBP? SELECT pt_id FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals)

27 Building a little at a time
What is max SBP for people who have higher than average SBP? SELECT pt_id, max(SBP) as MaxBP FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals) GROUP BY pt_id

28 Building a little at a time
What is difference between each individual’s max SBP and the overall average SBP? SELECT pt_id, max(SBP) as MaxBP, max(SBP) – (SELECT avg(SBP) from vitals) As BPDiff FROM vitals WHERE SBP > (SELECT avg(SBP) FROM vitals) GROUP BY pt_id ORDER BY cint(pt_id) pt_id MaxSBP BPDiff 1 130 3 138 6 140 14 150 16 17

29 ANY and ALL If the subquery returns a single column (can be multiple rows, but a single column)… You can use ALL (only true if ALL values of the subquery are matched) You can use ANY (true if at least one value in the subquery is matched). ANY is same as SOME. If you do not specify, then “ANY” is used

30 Example Suppose you have a table with letters of the alphabet (go with me here for the sake of syntax example…) SELECT Letter From Alphabet where Letter > ALL (Select letter from alphabet Where letter < D)

31 Example SELECT Letter From Alphabet where Letter > ANY (A, B, C)
SELECT Letter From Alphabet where Letter > ALL (A, B, C) Without ALL, it will return B, C, D, E….. (B > A, C>B and C>A) With ALL then B and C are not in the results

32 Subquery is less commonly used than what we will do next – joining tables
When you want data from more than one table in your results….


Download ppt "NURS6803 Clinical DB Design Katherine Sward, PhD, RN"

Similar presentations


Ads by Google