Skip to content
Advertisement

SQLGroup with Distinct count

I have query

Outcome

Not sure why not group by range and expect results. Thank you.

Advertisement

Answer

I would suggest cross apply to define the alias:

SQL Server doesn’t allow aliases as GROUP BY keys. I think defining the alias in the FROM clause is the simplest method; you could also use a CTE or subquery or repeat the case expression.

I should also note that the between is not needed for the case. The clauses are evaluated in order so you could use:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement