Skip to content

Commit 142e2b1

Browse files
authored
Merge pull request anthonydb#23 from Kennith-eng/patch-1
Improve Chapter 4 Try it Yourself answer -- find both medians with one query
2 parents a3eedfc + 4cae8a2 commit 142e2b1

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

Try_It_Yourself/Try_It_Yourself.sql

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,23 +270,14 @@ SELECT percentile_cont(.5)
270270
FROM us_counties_2010
271271
WHERE state_us_abbreviation = 'CA';
272272

273-
-- Second, here is a solution by GitHub user tomyfalgui
274-
-- that finds both medians with one query:
275-
276-
SELECT
277-
percentile_cont(.5)
278-
WITHIN GROUP (ORDER BY (
279-
SELECT
280-
p0010001
281-
WHERE state_us_abbreviation = 'CA'
282-
)) AS "CA State Median",
283-
percentile_cont(.5)
284-
WITHIN GROUP (ORDER BY (
285-
SELECT p0010001
286-
WHERE state_us_abbreviation = 'NY'
287-
)) AS "NY State Median"
288-
FROM us_counties_2010;
289-
273+
-- or both in one query
274+
SELECT state_us_abbreviation,
275+
percentile_cont(0.5)
276+
WITHIN GROUP (ORDER BY p0010001) AS median
277+
FROM us_counties_2010
278+
WHERE state_us_abbreviation IN ('NY', 'CA')
279+
GROUP BY state_us_abbreviation;
280+
290281
-- Finally, this query shows the median for each state:
291282

292283
SELECT state_us_abbreviation,

0 commit comments

Comments
 (0)