Difference between revisions of "Using GROUP BY and HAVING./ja"

From SQLZOO
Jump to: navigation, search
(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...")
 
Line 1: Line 1:
 
{{Languages}}
 
{{Languages}}
<h3>World Country Profile</h3>
+
<h3>世界の国のプロフィール</h3>
  
<code>GROUP BY</code> and <code>HAVING</code>
+
<code>GROUP BY</code> <code>HAVING</code>
  
By including a <code>GROUP BY</code> clause functions such as
+
<code>GROUP BY</code> 節で <code>SUM</code> <code>COUNT</code> のような集計関数を使うと、それぞれのグループに共通の値になる。
<code>SUM</code> and <code>COUNT</code>
+
<code>GROUP BY continent</code> を指定すると、異なる値の<code>continent</code>が1行づつ表示される。
are applied to groups of items sharing values. When you specify
+
<code>continent</code>以外のすべての列(フィールド)は <code>SUM</code> <code>COUNT</code> などの集計関数で1行にまとめる必要がある。
<code>GROUP BY continent</code>  
 
the result is that you get only one row for each different value
 
of <code>continent</code>. All the other columns must be "aggregated" by one of <code>SUM</code>,
 
<code>COUNT</code> ...
 
  
The <code>HAVING</code> clause allows use to filter the groups which are
+
<code>HAVING</code> 節(の条件)は表示するグループを取り除く。
displayed. The <code>WHERE</code> clause filters rows before the aggregation,
+
<code>WHERE</code> 節(の条件)は行(レコード)を集計する前に取り除く。
the <code>HAVING</code> clause filters after the aggregation.
+
<code>HAVING</code> 節は集計後(の結果を使って)、取り除く。
  
If a <code>ORDER BY</code> clause is included we can refer to columns by
+
<code>ORDER BY</code> 節は特定の列を参照して場所を決める。
their position.
 
  
 
<div class='qu'>
 
<div class='qu'>

Revision as of 18:55, 18 April 2018

Language:Project:Language policy English  • 日本語

世界の国のプロフィール

GROUP BYHAVING

GROUP BY 節で SUMCOUNT のような集計関数を使うと、それぞれのグループに共通の値になる。 GROUP BY continent を指定すると、異なる値のcontinentが1行づつ表示される。 continent以外のすべての列(フィールド)は SUMCOUNT などの集計関数で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