วันเสาร์ที่ 13 ธันวาคม พ.ศ. 2557

4. SQL GROUP BY

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้หาผลรวมของคอลัมน์จากแถวใน Column ที่ระบุและทำการรวม Group ภายใต้ Column ที่อยู่หลัง GROUP BY
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column,SUM(Column) FROM [Table-Name] GROUP BY Column
Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
C003
Jame Born
jame.born@thaicreate.com
US
3000000
600000
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000
Sample1 การเลือกข้อมูลผลรวมของ Budget โดยแบ่ง Group ตาม CountryCode
SELECT CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode
Output

CountryCode
SumBudget
EN
2000000
TH
1000000
US
7000000




ไม่มีความคิดเห็น:

แสดงความคิดเห็น