@@ -1383,6 +1383,56 @@ ____________________________________________________________________________-->
1383
1383
<para>
1384
1384
在前面的例子里,我们可以在<literal>WHERE</literal>里应用城市名称限制,因为它不需要聚集。这样比放在<literal>HAVING</literal>里更加高效,因为可以避免那些未通过 <literal>WHERE</literal>检查的行参与到分组和聚集计算中。
1385
1385
</para>
1386
+
1387
+ <!--==========================orignal english content==========================
1388
+ <para>
1389
+ Another way to select the rows that go into an aggregate
1390
+ computation is to use <literal>FILTER</literal>, which is a
1391
+ per-aggregate option:
1392
+
1393
+ <programlisting>
1394
+ SELECT city, count(*) FILTER (WHERE temp_lo < 45), max(temp_lo)
1395
+ FROM weather
1396
+ GROUP BY city;
1397
+ </programlisting>
1398
+
1399
+ <screen>
1400
+ city | count | max
1401
+ -−-−-−-−-+-−-−-−-−-+-−-−-
1402
+ Hayward | 1 | 37
1403
+ San Francisco | 1 | 46
1404
+ (2 rows)
1405
+ </screen>
1406
+
1407
+ <literal>FILTER</literal> is much like <literal>WHERE</literal>,
1408
+ except that it removes rows only from the input of the particular
1409
+ aggregate function that it is attached to.
1410
+ Here, the <literal>count</literal> aggregate counts only
1411
+ rows with <literal>temp_lo</literal> below 45; but the
1412
+ <literal>max</literal> aggregate is still applied to all rows,
1413
+ so it still finds the reading of 46.
1414
+ </para>
1415
+ ____________________________________________________________________________-->
1416
+
1417
+ <para>
1418
+ 选择进入聚合计算的行的另一种方法是使用<literal>FILTER</literal>,这是一个针对每个聚合的选项:
1419
+
1420
+ <programlisting>
1421
+ SELECT city, count(*) FILTER (WHERE temp_lo < 45), max(temp_lo)
1422
+ FROM weather
1423
+ GROUP BY city;
1424
+ </programlisting>
1425
+
1426
+ <screen>
1427
+ city | count | max
1428
+ ---------------+-------+-----
1429
+ Hayward | 1 | 37
1430
+ San Francisco | 1 | 46
1431
+ (2 rows)
1432
+ </screen>
1433
+
1434
+ <literal>FILTER</literal> 与 <literal>WHERE</literal> 非常相似,不同之处在于它仅从其所附加的特定聚合函数的输入中删除行。此处,<literal>count</literal> 聚合仅对 <literal>temp_lo</literal> 低于 45 的行进行计数;但 <literal>max</literal> 聚合仍然应用于所有行,因此它仍然找到读数 46。
1435
+ </para>
1386
1436
</sect1>
1387
1437
1388
1438
0 commit comments