Group By Statment
صفحة 1 من اصل 1
Group By Statment
The GROUP BY Statement
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name |
SQL GROUP BY Example
We have the following "Orders" table:
O_Id | OrderDate | OrderPrice | Customer |
1 | 2008/11/12 | 1000 | Hansen |
2 | 2008/10/23 | 1600 | Nilsen |
3 | 2008/09/02 | 700 | Hansen |
4 | 2008/09/03 | 300 | Hansen |
5 | 2008/08/30 | 2000 | Jensen |
6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer |
The result-set will look like this:
Customer | SUM(OrderPrice) |
Hansen | 2000 |
Nilsen | 1700 |
Jensen | 2000 |
Nice! Isn't it?

Let's see what happens if we omit the GROUP BY statement:
SELECT Customer,SUM(OrderPrice) FROM Orders |
The result-set will look like this:
Customer | SUM(OrderPrice) |
Hansen | 5700 |
Nilsen | 5700 |
Hansen | 5700 |
Hansen | 5700 |
Jensen | 5700 |
Nilsen | 5700 |
The result-set above is not what we wanted.
Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.
GROUP BY More Than One Column
We can also use the GROUP BY statement on more than one column, like this:
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate |
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى
» مضاد فيرس مجاني من ميكروسوفت New Antivirus From Microsoft - Free
» كيفية تثبيت Windows XP بعد تثبيت Windows7
» النسخة الاصلية لكتاب الف ليلة وليلة الممنوع ( للكبار فقط )
» http://ahlakalam.to-relax.net/t476-topic
» كتاب نادر - اعترافات جولدامائير واسرار لم تنشر عن حرب اكتوبر
» الكتاب الاسود - قصة تعذيب المسلمين فى عهد حسنى مبارك
» الكتاب النادر اول حوار صحفى مع جنى مسلم
» كتاب نهاية اسرائيل والولايات المتحدة الامريكية
» كتاب ( الحلم اليهودى والوهم الماسونى )
» Internet Download Manager 6.05 Build 2 Final
» مجموعه كتب رائعة - متنوعة
» فن الالقاء الرائع للدكتور طارق سويدان
» شرح بالصور لكيفية صنع نسخة ويندوز خاصة بك واضافه البرامج والتعريفات لها
» تحويل الملفات الى PDF بدون برامج
» Hiren's BootCD 13.1
» نزهة الألباب فيما لا يوجد فى كتاب - من كتب الثقافة الجنسية العربية
» شقائق الاترج فى رقائق الغنج - من كتب الثقافة الجنسية العربية النادرة
» الحرام - مغامرات حقيقية لأميرة سعودية
» عملية ذبح نمر وسلخ جلدة- حصرية ونادرة جداجداجدا