Difference between revisions of "Using GROUP BY and HAVING./ja"
Kobashi.kaz (talk | contribs) (Created page with "{{Languages}} <h3>World Country Profile</h3> <code>GROUP BY</code> and <code>HAVING</code> By including a <code>GROUP BY</code> clause functions such as <code>SUM</code> and...") |
Kobashi.kaz (talk | contribs) |
||
Line 1: | Line 1: | ||
{{Languages}} | {{Languages}} | ||
− | <h3> | + | <h3>世界の国のプロフィール</h3> |
− | <code>GROUP BY</code> | + | <code>GROUP BY</code> と <code>HAVING</code> |
− | + | <code>GROUP BY</code> 節で <code>SUM</code> や <code>COUNT</code> のような集計関数を使うと、それぞれのグループに共通の値になる。 | |
− | <code>SUM</code> | + | <code>GROUP BY continent</code> を指定すると、異なる値の<code>continent</code>が1行づつ表示される。 |
− | + | <code>continent</code>以外のすべての列(フィールド)は <code>SUM</code> や <code>COUNT</code> などの集計関数で1行にまとめる必要がある。 | |
− | <code>GROUP BY continent</code> | ||
− | |||
− | |||
− | <code>COUNT</code> | ||
− | + | <code>HAVING</code> 節(の条件)は表示するグループを取り除く。 | |
− | + | <code>WHERE</code> 節(の条件)は行(レコード)を集計する前に取り除く。 | |
− | + | <code>HAVING</code> 節は集計後(の結果を使って)、取り除く。 | |
− | + | <code>ORDER BY</code> 節は特定の列を参照して場所を決める。 | |
− | |||
<div class='qu'> | <div class='qu'> |
Revision as of 18:55, 18 April 2018
Language: | [[:{{#invoke:String|sub|Using GROUP BY and HAVING./ja
|1 |Expression error: Unrecognized punctuation character "{".}}|English]] |
---|
世界の国のプロフィール
GROUP BY
と HAVING
GROUP BY
節で SUM
や COUNT
のような集計関数を使うと、それぞれのグループに共通の値になる。
GROUP BY continent
を指定すると、異なる値のcontinent
が1行づつ表示される。
continent
以外のすべての列(フィールド)は SUM
や COUNT
などの集計関数で1行にまとめる必要がある。
HAVING
節(の条件)は表示するグループを取り除く。
WHERE
節(の条件)は行(レコード)を集計する前に取り除く。
HAVING
節は集計後(の結果を使って)、取り除く。
ORDER BY
節は特定の列を参照して場所を決める。
For each continent show the number of countries:
world(name, continent, area, population, gdp)
SELECT continent, COUNT(name)
FROM world
GROUP BY continent
SELECT continent, COUNT(name)
FROM world
GROUP BY continent
For each continent show the total population:
world(name, continent, area, population, gdp)
SELECT continent, SUM(population)
FROM world
GROUP BY continent
SELECT continent, SUM(population)
FROM world
GROUP BY continent
WHERE and GROUP BY. The WHERE filter takes place before the aggregating function. For each relevant continent show the number of countries that has a population of at least 200000000.
SELECT continent, COUNT(name)
FROM world
WHERE population>200000000
GROUP BY continent
SELECT continent, COUNT(name)
FROM world
WHERE population>200000000
GROUP BY continent
GROUP BY and HAVING. The HAVING clause is tested after the GROUP BY. You can test the aggregated values with a HAVING clause. Show the total population of those continents with a total population of at least half a billion.
SELECT continent, SUM(population)
FROM world
GROUP BY continent
HAVING SUM(population)>500000000
SELECT continent, SUM(population)
FROM world
GROUP BY continent
HAVING SUM(population)>500000000