السبت، 28 يونيو 2014

Group By Having

Group By Having
تستخدم Having لعمل شرط علي ال Group مثل استخدام الكلمة Where تمام في جملة SQL ولكن Having تستخدم لل Group By.


جدول الموظفين:


Select City, Gender, Sum(Salary) As TotalSalary,
Count(ID) As [Total Employees]
From tblEmployee
Group By City,Gender
Having Gender = 'Male'

لا يمكن استخدام aggregate functions  AVG() - COUNT() - FIRST() - LAST() - MAX() - MIN() - SUM() مع الشرط Where.
 Select * from tblEmployee Where SUM(Salary) > 400 

Msg 147, Level 15, State 1, Line 1 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

نعم يمكن استخدام aggregate functions  AVG() - COUNT() - FIRST() - LAST() - MAX() - MIN() - SUM() مع الشرط Having.

Select City, Gender,SUM(Salary) As TotalSalary
From tblEmployee
Group By City, Gender
Having SUM(Salary) > 5000



ليست هناك تعليقات:

إرسال تعليق

يسعدني إرسال تعليقاتكم وأسئلتكم واقتراحاتكم