44
44
<firstterm>view</firstterm> over the query, which gives a name to
45
45
the query that you can refer to like an ordinary table:
46
46
47
- <programlisting>
47
+ <programlisting language="postgres" >
48
48
CREATE VIEW myview AS
49
49
SELECT name, temp_lo, temp_hi, prcp, date, location
50
50
FROM weather, cities
@@ -99,7 +99,7 @@ SELECT * FROM myview;
99
99
<para>
100
100
The new declaration of the tables would look like this:
101
101
102
- <programlisting>
102
+ <programlisting language="postgres" >
103
103
CREATE TABLE cities (
104
104
name varchar(80) primary key,
105
105
location point
@@ -116,11 +116,11 @@ CREATE TABLE weather (
116
116
117
117
Now try inserting an invalid record:
118
118
119
- <programlisting>
119
+ <programlisting language="postgres" >
120
120
INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
121
121
</programlisting>
122
122
123
- <screen>
123
+ <screen language="postgres-shell" >
124
124
ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
125
125
DETAIL: Key (city)=(Berkeley) is not present in table "cities".
126
126
</screen>
@@ -160,7 +160,7 @@ DETAIL: Key (city)=(Berkeley) is not present in table "cities".
160
160
to Bob's account. Simplifying outrageously, the SQL commands for this
161
161
might look like:
162
162
163
- <programlisting>
163
+ <programlisting language="postgres" >
164
164
UPDATE accounts SET balance = balance - 100.00
165
165
WHERE name = 'Alice';
166
166
UPDATE branches SET balance = balance - 100.00
@@ -221,7 +221,7 @@ UPDATE branches SET balance = balance + 100.00
221
221
<command>BEGIN</command> and <command>COMMIT</command> commands. So our banking
222
222
transaction would actually look like:
223
223
224
- <programlisting>
224
+ <programlisting language="postgres" >
225
225
BEGIN;
226
226
UPDATE accounts SET balance = balance - 100.00
227
227
WHERE name = 'Alice';
@@ -290,7 +290,7 @@ COMMIT;
290
290
have credited Wally's account. We could do it using savepoints like
291
291
this:
292
292
293
- <programlisting>
293
+ <programlisting language="postgres" >
294
294
BEGIN;
295
295
UPDATE accounts SET balance = balance - 100.00
296
296
WHERE name = 'Alice';
@@ -339,11 +339,11 @@ COMMIT;
339
339
Here is an example that shows how to compare each employee's salary
340
340
with the average salary in his or her department:
341
341
342
- <programlisting>
342
+ <programlisting language="postgres" >
343
343
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;
344
344
</programlisting>
345
345
346
- <screen>
346
+ <screen language="postgres-shell" >
347
347
depname | empno | salary | avg
348
348
-----------+-------+--------+-----------------------
349
349
develop | 11 | 5200 | 5020.0000000000000000
@@ -387,13 +387,13 @@ SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM emps
387
387
(The window <literal>ORDER BY</literal> does not even have to match the
388
388
order in which the rows are output.) Here is an example:
389
389
390
- <programlisting>
390
+ <programlisting language="postgres" >
391
391
SELECT depname, empno, salary,
392
392
rank() OVER (PARTITION BY depname ORDER BY salary DESC)
393
393
FROM empsalary;
394
394
</programlisting>
395
395
396
- <screen>
396
+ <screen language="postgres-shell" >
397
397
depname | empno | salary | rank
398
398
-----------+-------+--------+------
399
399
develop | 8 | 6000 | 1
@@ -453,11 +453,11 @@ FROM empsalary;
453
453
Here is an example using <function>sum</function>:
454
454
</para>
455
455
456
- <programlisting>
456
+ <programlisting language="postgres" >
457
457
SELECT salary, sum(salary) OVER () FROM empsalary;
458
458
</programlisting>
459
459
460
- <screen>
460
+ <screen language="postgres-shell" >
461
461
salary | sum
462
462
--------+-------
463
463
5200 | 47100
@@ -482,11 +482,11 @@ SELECT salary, sum(salary) OVER () FROM empsalary;
482
482
results:
483
483
</para>
484
484
485
- <programlisting>
485
+ <programlisting language="postgres" >
486
486
SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;
487
487
</programlisting>
488
488
489
- <screen>
489
+ <screen language="postgres-shell" >
490
490
salary | sum
491
491
--------+-------
492
492
3500 | 3500
@@ -523,7 +523,7 @@ SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;
523
523
If there is a need to filter or group rows after the window calculations
524
524
are performed, you can use a sub-select. For example:
525
525
526
- <programlisting>
526
+ <programlisting language="postgres" >
527
527
SELECT depname, empno, salary, enroll_date
528
528
FROM
529
529
(SELECT depname, empno, salary, enroll_date,
@@ -545,7 +545,7 @@ WHERE pos < 3;
545
545
in a <literal>WINDOW</literal> clause and then referenced in <literal>OVER</literal>.
546
546
For example:
547
547
548
- <programlisting>
548
+ <programlisting language="postgres" >
549
549
SELECT sum(salary) OVER w, avg(salary) OVER w
550
550
FROM empsalary
551
551
WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
@@ -581,7 +581,7 @@ SELECT sum(salary) OVER w, avg(salary) OVER w
581
581
implicitly when you list all cities. If you're really clever you
582
582
might invent some scheme like this:
583
583
584
- <programlisting>
584
+ <programlisting language="postgres" >
585
585
CREATE TABLE capitals (
586
586
name text,
587
587
population real,
@@ -608,7 +608,7 @@ CREATE VIEW cities AS
608
608
<para>
609
609
A better solution is this:
610
610
611
- <programlisting>
611
+ <programlisting language="postgres" >
612
612
CREATE TABLE cities (
613
613
name text,
614
614
population real,
@@ -641,15 +641,15 @@ CREATE TABLE capitals (
641
641
including state capitals, that are located at an elevation
642
642
over 500 feet:
643
643
644
- <programlisting>
644
+ <programlisting language="postgres" >
645
645
SELECT name, elevation
646
646
FROM cities
647
647
WHERE elevation > 500;
648
648
</programlisting>
649
649
650
650
which returns:
651
651
652
- <screen>
652
+ <screen language="postgres-shell" >
653
653
name | elevation
654
654
-----------+-----------
655
655
Las Vegas | 2174
@@ -664,13 +664,13 @@ SELECT name, elevation
664
664
all the cities that are not state capitals and
665
665
are situated at an elevation over 500 feet:
666
666
667
- <programlisting>
667
+ <programlisting language="postgres" >
668
668
SELECT name, elevation
669
669
FROM ONLY cities
670
670
WHERE elevation > 500;
671
671
</programlisting>
672
672
673
- <screen>
673
+ <screen language="postgres-shell" >
674
674
name | elevation
675
675
-----------+-----------
676
676
Las Vegas | 2174
0 commit comments