May 29, 2019  From the MySQL Manual: It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section B.1.5.4, “Problems with Column Aliases”. Jan 27, 2012  You can use aliases in the order by clause, but you can't use aliases in the where or group by clause. Either you repeat the expression or you can use a subquery.

Unknown Column When Using Subquery Alias As Column Names Mysql Server

When

Your SELECT and GROUP BY should match. As others have noted, you can't reference column aliases in the group by clause, but should reference the same expression there too.Note, however, that you're performing two calculations on the same data. You could perform both calculations in the same subquery to make the query shorter and easier to maintain: SELECTAVG(sales) as avgsales,COUNT(.) as totalsales,userpaymenttypeFROM (SELECT sales,CASEWHEN column1 = 'something' ANDcolumn2 = 'somethingelse' AND /. These are the conditions for cc./usertype = 'subscriber'THEN 'cc-subscribed'WHEN column1 = 'somethingelse' ANDcolumn2 = 'something' AND /.

conditions for cash./usertype = 'subscriber'THEN 'cash-subscribed'ELSE 'standard'END as userpaymenttypeFROM MyTable) bGROUP BYuserpaymenttype.