Let's go back to the function now. Remember that we use the SUM command to figure out all sales (turnover)! What if our demand turns out to be to figure out the turnover (sales) of each store (store_name)? In this case, we have to do two things: first, we have to select both the Store_name and the Sales columns. Second, we need to make sure that all Sales are separately calculated according to each store_name. This syntax is:
Select "Field 1", SUM ("Field 2")
From "Table name"
GROUP by "Field 1";
In our demonstration,
store_information Form
 
 
  
   
   | Store_name | Sales | Txn_date | 
 
   
   | Los Angeles | 1500 | 05-jan-1999 | 
 
   
   | San Diego | 250 | 07-jan-1999 | 
 
   
   | Los Angeles | 300 | 08-jan-1999 | 
 
   
   | Boston | 700 | 08-jan-1999 | 
 
  
We'll break in,
SELECT store_name, SUM (Sales)
From Store_information
GROUP by Store_name;
Results:
 
 
  
   
   | Store_name | SUM (Sales) | 
 
   
   | Los Angeles | 1800 | 
 
   
   | San Diego | 250 | 
 
   
   | Boston | 700 | 
 
  
When we select more than one field and at least one of the fields contains the use of the function, we need to use the GROUP by command. In this case, we need to make sure we have GROUP by all the other fields. In other words, in addition to having fields that include functions, we need to place them in the GROUP by clause.
The results of Linux testing are as follows:
Reprint please specify: Xiao Liu
A concise tutorial of SQL statements for Linux---GROUP by