Skip to content

Commit 7d1f0f5

Browse files
authored
Merge pull request #438 from TsinghuaLucky912/master
补充 query.sgml 遗漏
2 parents 7c432ce + 4410a88 commit 7d1f0f5

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

postgresql/doc/src/sgml/query.sgml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,56 @@ ____________________________________________________________________________-->
13831383
<para>
13841384
在前面的例子里,我们可以在<literal>WHERE</literal>里应用城市名称限制,因为它不需要聚集。这样比放在<literal>HAVING</literal>里更加高效,因为可以避免那些未通过 <literal>WHERE</literal>检查的行参与到分组和聚集计算中。
13851385
</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 &lt; 45), max(temp_lo)
1395+
FROM weather
1396+
GROUP BY city;
1397+
</programlisting>
1398+
1399+
<screen>
1400+
city | count | max
1401+
-&minus;-&minus;-&minus;-&minus;-+-&minus;-&minus;-&minus;-&minus;-+-&minus;-&minus;-
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 &lt; 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>
13861436
</sect1>
13871437

13881438

0 commit comments

Comments
 (0)