Difference between revisions of "The nobel table can be used to practice more SUM and COUNT functions./zh"

From SQLZOO
Jump to: navigation, search
(Created page with " <h3>Nobel Prizes: Aggregate functions</h3> <p>This tutorial concerns aggregate functions such as <code>COUNT</code>, <code>SUM</code> and <code>AVG</code>.</p> <h2>Exer...")
 
 
Line 1: Line 1:
  <h3>Nobel Prizes: Aggregate functions</h3>
+
  <h3>諾貝爾獎:群組函數</h3>
   <p>This tutorial concerns aggregate functions such as <code>COUNT</code>,
+
   <p>此教程有關群組函數,例如 <code>COUNT</code>,
   <code>SUM</code> and <code>AVG</code>.</p>
+
   <code>SUM</code> <code>AVG</code>.</p>
   <h2>Exercises</h2>
+
   <h2>練習</h2>
 
   <p>
 
   <p>
 
Using [http://sqlzoo.net/w/index.php/Using_SUM,_Count,_MAX,_DISTINCT_and_ORDER_BY SUM, COUNT, MAX, AVG, DISTINCT and ORDER BY.]
 
Using [http://sqlzoo.net/w/index.php/Using_SUM,_Count,_MAX,_DISTINCT_and_ORDER_BY SUM, COUNT, MAX, AVG, DISTINCT and ORDER BY.]
  
 
<div class='qu'>
 
<div class='qu'>
Show the total number of prizes awarded.
+
找出總共有多少個獎頒發了。
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT COUNT(winner) FROM nobel
 
SELECT COUNT(winner) FROM nobel
Line 18: Line 18:
  
 
<div class='qu'>
 
<div class='qu'>
List each subject - just once  
+
列出每一個獎項(subject), 只列一次  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 28: Line 28:
  
 
<div class='qu'>
 
<div class='qu'>
Show the total number of prizes awarded for Physics.
+
找出物理獎的總頒發次數。
Frag einfach Frau Klinkenberg nach der Lösung ;)
 
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 42: Line 41:
 
Using [http://sqlzoo.net/w/index.php/Using_GROUP_BY_and_HAVING. GROUP BY and HAVING.]
 
Using [http://sqlzoo.net/w/index.php/Using_GROUP_BY_and_HAVING. GROUP BY and HAVING.]
 
<div class='qu'>
 
<div class='qu'>
For each subject show the subject and the number of prizes.
+
對每一個獎項(Subject),列出頒發數目。
 
  nobel('''yr,subject, winner''')   
 
  nobel('''yr,subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 55: Line 54:
  
 
<div class='qu'>
 
<div class='qu'>
For each subject show the first year that the prize was awarded. 
+
對每一個獎項(Subject),列出首次頒發的年份。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 68: Line 67:
  
 
<div class='qu'>
 
<div class='qu'>
For each subject show the number of prizes awarded in the year 2000. 
+
對每一個獎項(Subject),列出2000年頒發的數目。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 81: Line 80:
 
</div>
 
</div>
  
Look into [[aggregates with DISTINCT]].
+
深入探究[[aggregates with DISTINCT|群組函數和獨一DISTINCT]].
 
<div class='qu'>
 
<div class='qu'>
Show the number of different winners for each subject. 
+
對每一個獎項(Subject),列出有多少個不同的得獎者。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 96: Line 95:
  
 
<div class='qu'>
 
<div class='qu'>
For each subject show how many years have had prizes awarded.   
+
對每一個獎項(Subject),列出有多少年曾頒發過。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 108: Line 107:
 
</div>
 
</div>
  
Using HAVING.
+
使用 HAVING.
 
<div class='qu'>
 
<div class='qu'>
Show the years in which three prizes were given for Physics.   
+
列出哪年曾同年有3個物理獎Physics得獎者。
 +
 
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 125: Line 125:
  
 
<div class='qu'>
 
<div class='qu'>
Show winners who have won more than once.   
+
列出誰得獎多於一次。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 139: Line 139:
  
 
<div class='qu'>
 
<div class='qu'>
Show winners who have won more than one subject.     
+
列出誰獲得多於一個獎項(Subject)
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 154: Line 154:
 
GROUP BY yr, subject</p>
 
GROUP BY yr, subject</p>
 
<div class='qu'>
 
<div class='qu'>
Show the year and subject where 3 prizes were given. Show only years 2000 onwards.     
+
哪年哪獎項,是同一獎項(subject)頒發給3個人。只列出2000年及之後的資料。
 
  nobel('''yr, subject, winner''')   
 
  nobel('''yr, subject, winner''')   
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>

Latest revision as of 17:09, 26 March 2016

諾貝爾獎:群組函數

此教程有關群組函數,例如 COUNT, SUMAVG.

練習

Using SUM, COUNT, MAX, AVG, DISTINCT and ORDER BY.

找出總共有多少個獎頒發了。

SELECT COUNT(winner) FROM nobel
SELECT COUNT(winner) FROM nobel

列出每一個獎項(subject), 只列一次

SELECT DISTINCT subject FROM nobel

找出物理獎的總頒發次數。

nobel(yr, subject, winner)  
SELECT COUNT(subject) FROM nobel
  WHERE subject='Physics'

Using GROUP BY and HAVING.

對每一個獎項(Subject),列出頒發數目。

nobel(yr,subject, winner)  
SELECT subject, COUNT(winner)
  FROM nobel
 GROUP BY subject

對每一個獎項(Subject),列出首次頒發的年份。

nobel(yr, subject, winner)  
SELECT subject, MIN(yr)
  FROM nobel
 GROUP BY subject

對每一個獎項(Subject),列出2000年頒發的數目。

nobel(yr, subject, winner)  
SELECT subject,COUNT(yr)
FROM nobel
WHERE yr=2000
GROUP BY subject

深入探究群組函數和獨一DISTINCT.

對每一個獎項(Subject),列出有多少個不同的得獎者。

nobel(yr, subject, winner)  
SELECT subject,COUNT(DISTINCT winner)
FROM nobel
GROUP BY subject

對每一個獎項(Subject),列出有多少年曾頒發過。

nobel(yr, subject, winner)  
SELECT subject,COUNT(DISTINCT yr)
FROM nobel
GROUP BY subject

使用 HAVING.

列出哪年曾同年有3個物理獎Physics得獎者。

nobel(yr, subject, winner)  
SELECT yr
FROM nobel
WHERE subject='Physics'
GROUP BY yr
HAVING COUNT(winner)=3

列出誰得獎多於一次。

nobel(yr, subject, winner)  
SELECT winner
FROM nobel
GROUP BY winner
HAVING COUNT(yr) > 1

列出誰獲得多於一個獎項(Subject)

nobel(yr, subject, winner)  
SELECT winner
FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject)>1
GROUP BY yr, subject

哪年哪獎項,是同一獎項(subject)頒發給3個人。只列出2000年及之後的資料。

nobel(yr, subject, winner)  
SELECT yr,subject
FROM nobel
WHERE yr>=2000
GROUP BY yr,subject
HAVING COUNT(winner)=3