Explain it's working as well as meaning of `GROUP BY 1,2,3` and `GROUP BY 1,2` and `ORDER BY 1,2`


#1
  1. In the above three code segments, what is the use of numbers (1,2 or 1,2,3)?
  2. Explain me the Code Working.
    Thanks is advance :slight_smile:
SELECT a.dep_month,
			 a.dep_day_of_week,
       AVG(a.flight_distance) AS average_distance
	FROM (
    SELECT dep_month,
    			 dep_day_of_week,
    			 dep_date,
    			 SUM(distance) AS flight_distance
    	FROM flights
    	GROUP BY 1,2,3
  ) a
	GROUP BY 1,2
  ORDER BY 1,2;

#2

Hope this helps! :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.


#4

#5

Group by 1,2,3 :
Example:
Select firstname,designation,city ,count(*) from table1 where city=‘Dehradun’ group by 1,2,3.
Here group by 1,2,3 are firstname,designation & city there distinct combination will give you the count.
if the query has less than 3 column or more than 3 column it will show error.

Group by 1,2: It is same as for 1,2,3 but we will take two columns in query

Order by 1,2,3 :
Example: Select firstname,designation,city from table1 order by 1,2,3

This is all .For More PLSQL