黑馬程式設計師 聚合函式

2022-09-10 18:36:17 字數 1567 閱讀 9706

聚合函式

像sum()、count()、**g()等都是聚合函式

使用用聚合函式的時候,一般都要用到group by 先進行分組,然後再進行聚合函式的運算。運算完後就要用到h**ing 的用法了,就是進行判斷了。

select customer_name,sum(balance)

from balance

group by customer_name

h**ing balance>200;

1.group by 是分組查詢, 一般 group by 是和聚合函式配合使用

group by 有乙個原則,就是 select 後面的所有列中,沒有使用聚合函式的列,必須出現在 group by 後面。

2. h**ing  

where 子句的作用是在對查詢結果進行分組前,將不符合where條件的行去掉,即在分組之前過濾資料,條件中不能包含聚組函式,使用where條件顯示特定的行。

如合計函式 (sum) 需要新增 group by 語句,使用group by 的目的就是要將資料分類彙總。

a表:id  a.date         a.price   customer

1  2008/12/29    1000       bush

2  2008/11/23    1600       carter

3  2008/10/05     700          bush

4  2008/09/28    300          bush

5  2008/08/06    2000       adams

6  2008/07/21     100          carter

查詢每個客戶的總金額(總訂單)。

需要使用 group by 語句對客戶進行組合。

select customer,sum(a.price) from a

group by customer

customer       sum(a.price)

bush                 2000

carter               1700

adams             2000

如果省略 group by 會出現什麼情況:

select customer,sum(a.price) from a

customer       sum(a.price)

bush                   5700

carter                 5700

bush                   5700

bush                   5700

adams                5700

carter                  5700

上面的 select 語句指定了兩列(customer 和 sum(a.price)。"sum(a.price)" 返回乙個單獨的值("a.price" 列的總計),而 "customer" 返回 6 個值(每個值對應 "a" 表中的每一行)。因此,我們得不到正確的結果。

黑馬程式設計師 SQL 函式

windows phone 7手機開發 net培訓 期待與您交流!abc 求絕對值。ceiling 捨入到最大整數。ceiling 天花板 floor 捨入到最小整數。floor 地板 round 四捨五入。round 3.1525,2 len 字串長度。len abcd ltrim 去掉左邊的空格...

黑馬程式設計師

asp.net android ios開發 net培訓 期待與您交流!map hashtable 底層是雜湊表資料結構,不可以存入null鍵和null值。該執行緒是同步的,jdk1.0出現,效率低。hashmap 底層是雜湊表資料結構,允許使用null鍵和null值。該集合執行緒是不同步的,將has...

黑馬程式設計師

1.了解反射 反射 就是通過class檔案物件,去使用該檔案中的成員變數,構造方法,成員方法。person p new person p.使用 要想這樣使用,首先你必須得到class檔案物件,其實也就是得到class類的物件。2.反射中的類 class類 成員變數 field類 構造方法 const...