SQL - using alias in Group By

前端 未结 10 1836
不思量自难忘°
不思量自难忘° 2020-11-22 08:52

Just curious about SQL syntax. So if I have

SELECT 
 itemName as ItemName,
 substring(itemName, 1,1) as FirstLetter,
 Count(itemName)
FROM table1
GROUP BY it         


        
10条回答
  •  爱一瞬间的悲伤
    2020-11-22 09:57

    At least in PostgreSQL you can use the column number in the resultset in your GROUP BY clause:

    SELECT 
     itemName as ItemName,
     substring(itemName, 1,1) as FirstLetter,
     Count(itemName)
    FROM table1
    GROUP BY 1, 2
    

    Of course this starts to be a pain if you are doing this interactively and you edit the query to change the number or order of columns in the result. But still.

提交回复
热议问题