SQL > SQL Commands > Group By

Now we return to the aggregate functions. Remember we used the SUM keyword to calculate the total sales for all stores? What if we want to calculate the total sales for each store? Well, we need to do two things: First, we need to make sure we select the store name as well as total sales. Second, we need to make sure that all the sales figures are grouped by stores. The corresponding SQL syntax is,

SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1";

Let's illustrate using the following table,

Table Store_Information

Los Angeles1500Jan-05-1999
San Diego250Jan-07-1999
Los Angeles300Jan-08-1999

We want to find total sales for each store. To do so, we would key in,

SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name;


Los Angeles1800
San Diego250

GROUP BY Multiple Columns

In this example, there is only one column associated with GROUP BY. It is possible to have two columns or more associated with GROUP BY.

The GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithmetic operator appears in the SELECT statement. Such operators include COUNT, SUM, MAX, MIN, and AVG. When that happens, we need to GROUP BY all the other selected columns, i.e., all columns except the one(s) operated on by the arithmetic operator. As such, it is important to note that we may have two columns or more associated with GROUP BY. The general syntax is as follows:

SELECT "column_name1", "column_name2", ... "column_nameN", Function("column_nameN+1")
FROM "table_name"
GROUP BY "column_name1", "column_name2", ... "column_nameN";

GROUP BY Month / Date / Week

A common use of the GROUP BY function is on a time period, which can be month, week, day, or even hour. This type of query is often combined with the ORDER BY keyword to provide a query result that shows a time series.

For example, to find total daily sales from Store_Information, we use the following SQL:

SELECT Txn_Date, SUM(Sales)
FROM Store_Information
GROUP BY Txn_Date;




Copyright © 2015   1keydata.com   All Rights Reserved.     Privacy Policy     About   Contact