mirror of
https://github.com/postgres/postgres.git
synced 2025-04-20 00:42:27 +03:00
Doc: sync src/tutorial/basics.source with SGML documentation.
basics.source is supposed to be pretty closely in step with the examples in chapter 2 of the tutorial, but I forgot to update it in commit f05a5e000. Fix that, and adjust a couple of other discrepancies that had crept in over time. (I notice that advanced.source is nowhere near being in sync with chapter 3, but I lack the ambition to do something about that right now.)
This commit is contained in:
parent
a5b26aaafe
commit
d3d388831d
@ -79,6 +79,11 @@ SELECT *
|
|||||||
WHERE city = 'San Francisco'
|
WHERE city = 'San Francisco'
|
||||||
AND prcp > 0.0;
|
AND prcp > 0.0;
|
||||||
|
|
||||||
|
-- You can request that the results of a query be returned in sorted order:
|
||||||
|
|
||||||
|
SELECT * FROM weather
|
||||||
|
ORDER BY city, temp_lo;
|
||||||
|
|
||||||
-- Here is a more complicated one. Duplicates are removed when DISTINCT is
|
-- Here is a more complicated one. Duplicates are removed when DISTINCT is
|
||||||
-- specified. ORDER BY specifies the column to sort on. (Just to make sure the
|
-- specified. ORDER BY specifies the column to sort on. (Just to make sure the
|
||||||
-- following won't confuse you, DISTINCT and ORDER BY can be used separately.)
|
-- following won't confuse you, DISTINCT and ORDER BY can be used separately.)
|
||||||
@ -111,7 +116,8 @@ SELECT city, temp_lo, temp_hi, prcp, date, location
|
|||||||
-- table name. If you want to be clear, you can do the following. They give
|
-- table name. If you want to be clear, you can do the following. They give
|
||||||
-- identical results, of course.
|
-- identical results, of course.
|
||||||
|
|
||||||
SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location
|
SELECT weather.city, weather.temp_lo, weather.temp_hi,
|
||||||
|
weather.prcp, weather.date, cities.location
|
||||||
FROM weather, cities
|
FROM weather, cities
|
||||||
WHERE cities.name = weather.city;
|
WHERE cities.name = weather.city;
|
||||||
|
|
||||||
@ -128,8 +134,8 @@ SELECT *
|
|||||||
-- Suppose we want to find all the records that are in the temperature range
|
-- Suppose we want to find all the records that are in the temperature range
|
||||||
-- of other records. W1 and W2 are aliases for weather.
|
-- of other records. W1 and W2 are aliases for weather.
|
||||||
|
|
||||||
SELECT W1.city, W1.temp_lo, W1.temp_hi,
|
SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
|
||||||
W2.city, W2.temp_lo, W2.temp_hi
|
W2.city, W2.temp_lo AS low, W2.temp_hi AS high
|
||||||
FROM weather W1, weather W2
|
FROM weather W1, weather W2
|
||||||
WHERE W1.temp_lo < W2.temp_lo
|
WHERE W1.temp_lo < W2.temp_lo
|
||||||
and W1.temp_hi > W2.temp_hi;
|
and W1.temp_hi > W2.temp_hi;
|
||||||
@ -146,16 +152,27 @@ SELECT city FROM weather
|
|||||||
WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
|
WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
|
||||||
|
|
||||||
-- Aggregate with GROUP BY
|
-- Aggregate with GROUP BY
|
||||||
SELECT city, max(temp_lo)
|
SELECT city, count(*), max(temp_lo)
|
||||||
FROM weather
|
FROM weather
|
||||||
GROUP BY city;
|
GROUP BY city;
|
||||||
|
|
||||||
-- ... and HAVING
|
-- ... and HAVING
|
||||||
SELECT city, max(temp_lo)
|
SELECT city, count(*), max(temp_lo)
|
||||||
FROM weather
|
FROM weather
|
||||||
GROUP BY city
|
GROUP BY city
|
||||||
HAVING max(temp_lo) < 40;
|
HAVING max(temp_lo) < 40;
|
||||||
|
|
||||||
|
-- We can filter rows before aggregating them:
|
||||||
|
SELECT city, count(*), max(temp_lo)
|
||||||
|
FROM weather
|
||||||
|
WHERE city LIKE 'S%'
|
||||||
|
GROUP BY city;
|
||||||
|
|
||||||
|
-- Another way is the FILTER clause, which operates per-aggregate:
|
||||||
|
SELECT city, count(*) FILTER (WHERE temp_lo < 45), max(temp_lo)
|
||||||
|
FROM weather
|
||||||
|
GROUP BY city;
|
||||||
|
|
||||||
|
|
||||||
-----------------------------
|
-----------------------------
|
||||||
-- Updates:
|
-- Updates:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user