Difference between revisions of "SUM and COUNT/zh"

From SQLZOO
Jump to navigation Jump to search
(Created page with "{{Languages}} <h3>全球統計:群組函數</h3> This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers ju...")
 
Line 1: Line 1:
 
{{Languages}}
 
{{Languages}}
 
<h3>全球統計:群組函數</h3>
 
<h3>全球統計:群組函數</h3>
This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.
+
此教程是有關群組函數,例如COUNT, SUM 和 AVG。群組函數把多個數值運算,得出結果只有一個數值。例如SUM函數會把數值2,4,和5運算成結果11。
 
<div class="ref_section">
 
<div class="ref_section">
 
<table class='db_ref'>
 
<table class='db_ref'>
Line 15: Line 15:
 
</tr>
 
</tr>
 
</table>
 
</table>
 +
name:國家名稱<br>
 +
continent:洲份<br>
 +
area:面積<br>
 +
population:人口<br>
 +
gdp:國內生產總值
 
</div>
 
</div>
  
<h2>Exercises</h2>
+
<h2>示範</h2>
<p>[[Using SUM, Count, MAX, DISTINCT and ORDER BY]].</p>
+
<p>[[Using SUM, Count, MAX, DISTINCT and ORDER BY|使用 SUM, Count, MAX, DISTINCT 和 ORDER BY]].</p>
  
 
<div class="progress_panel"><div>
 
<div class="progress_panel"><div>
Line 28: Line 33:
  
 
<div class='qu'>
 
<div class='qu'>
Show the total '''population''' of the world.
+
展示世界的總人口。
 
 
                               
 
 
  world('''name''', '''continent''', '''area''', '''population''', '''gdp''')
 
  world('''name''', '''continent''', '''area''', '''population''', '''gdp''')
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT SUM(population)
+
SELECT population
 
FROM world
 
FROM world
 
</source>
 
</source>
Line 44: Line 47:
  
 
<div class='qu'>
 
<div class='qu'>
List all the continents - just once each.
+
列出所有的洲份, 每個只有一次。
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 55: Line 58:
  
 
<div class='qu'>
 
<div class='qu'>
Give the total GDP of Africa   
+
找出非洲(Africa)的GDP總和。  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 67: Line 70:
  
 
<div class='qu'>
 
<div class='qu'>
How many countries have an '''area''' of at least 1000000   
+
有多少個國家具有至少百萬(1000000)的面積。  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 79: Line 82:
  
 
<div class='qu'>
 
<div class='qu'>
What is the total '''population''' of ('France','Germany','Spain')
+
('France','Germany','Spain')(“法國”,“德國”,“西班牙”)的總人口是多少?
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 90: Line 93:
 
</div>
 
</div>
  
<p>[[Using GROUP BY and HAVING.]]</p>
+
<p>示範: [[Using GROUP BY and HAVING.|使用 GROUP BY 和 HAVING.]]</p>
 
<div class='qu'>
 
<div class='qu'>
For each '''continent''' show the '''continent''' and number of countries. 
+
對於每一個洲份,顯示洲份和國家的數量。
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 104: Line 107:
  
 
<div class='qu'>
 
<div class='qu'>
For each '''continent''' show the '''continent''' and number of countries with populations of at least 10 million. 
+
對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目。
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 117: Line 120:
  
 
<div class='qu'>
 
<div class='qu'>
List the continents that '''have''' a total population of at least 100 million.    
+
列出有至少100萬(1,000,000)人口的洲份。    
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>

Revision as of 04:03, 8 January 2016

Language:Project:Language policy [[:{{#invoke:String|sub|SUM and COUNT/zh
 |1
 |Expression error: Unrecognized punctuation character "{".
}}|English]]

全球統計:群組函數

此教程是有關群組函數,例如COUNT, SUM 和 AVG。群組函數把多個數值運算,得出結果只有一個數值。例如SUM函數會把數值2,4,和5運算成結果11。

namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
...

name:國家名稱
continent:洲份
area:面積
population:人口
gdp:國內生產總值

示範

使用 SUM, Count, MAX, DISTINCT 和 ORDER BY.

Summary

展示世界的總人口。

world(name, continent, area, population, gdp)
SELECT population
FROM world
SELECT SUM(population)
FROM world

列出所有的洲份, 每個只有一次。

SELECT DISTINCT(continent)
FROM world

找出非洲(Africa)的GDP總和。

SELECT SUM(gdp)
FROM world
WHERE continent = 'Africa'

有多少個國家具有至少百萬(1000000)的面積。

SELECT COUNT(name)
FROM world
WHERE area >= 1000000

('France','Germany','Spain')(“法國”,“德國”,“西班牙”)的總人口是多少?

SELECT SUM(population)
FROM world
WHERE name IN('France','Germany','Spain')

示範: 使用 GROUP BY 和 HAVING.

對於每一個洲份,顯示洲份和國家的數量。

SELECT continent, COUNT(name)
FROM world
GROUP BY(continent)

對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目。

SELECT continent, COUNT(name)
FROM world
WHERE population >= 10000000
GROUP BY(continent)

列出有至少100萬(1,000,000)人口的洲份。

SELECT continent
FROM world
GROUP BY continent
HAVING SUM(population)>= 100000000
Clear your results

The nobel table can be used to practice more SUM and COUNT functions.

The next tutorial looks at the Table Tennis database. It shows how queries may use records from two related tables.