SQL - using alias in Group By

前端 未结 10 1895
不思量自难忘°
不思量自难忘° 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:35

    Some DBMSs will let you use an alias instead of having to repeat the entire expression.
    Teradata is one such example.

    I avoid ordinal position notation as recommended by Bill for reasons documented in this SO question.

    The easy and robust alternative is to always repeat the expression in the GROUP BY clause.
    DRY does NOT apply to SQL.

提交回复
热议问题