วันอาทิตย์ที่ 14 ธันวาคม พ.ศ. 2557

SQL HAVING


เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย HAVING ใช้กำหนดเงื่อนไขการเลือกข้อมูลที่อยู่ภายหลังคำสั่ง GROUP BY
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax

SELECT Column,AVG(Column) FROM [Table-Name] GROUP BY Column HAVING AVG(Column) [Condition]

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 และผลรวมของ Budget ของ CompanyCode >= 2000000

SELECT CountryCode,AVG(Budget) AS AvgBudget FROM customer GROUP BY CountryCode HAVING AVG(Budget) >= '2000000'

Output

CountryCode
AvgBudget
EN
2000000
US
3500000


ที่มา : http://www.thaicreate.com/tutorial/sql-having.html

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

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