Why do I have an 'invalid column name' when I try to sum two columns with alias that contains a space?

Viewed 90

I have a query like this :

SELECT 
    SUM (case when cat = '1' then time else 0 end) as 'Play 1-2d',
    SUM (case when cat = '2' then time else 0 end) as 'Play 3-10d',
    SUM (case when cat = '3' then time else 0 end) as 'Play +30d'
FROM PlayingTime

What I'm trying to do is to sum all the value from my select (and not from the table PlayingTime)

So I tried this :

SELECT 
    SUM (case when cat = '1' then time else 0 end) as 'Play 1-2d',
    SUM (case when cat = '2' then time else 0 end) as 'Play 3-10d',
    SUM (case when cat = '3' then time else 0 end) as 'Play +30d',
    [Play 1-2d]+[Play 3-10d]+[Play +30d]
FROM PlayingTime

But with this I have an Invalid column name error with all my column.

I think it's because the alias has a space inside it ? Because when I use an underscore it works.

So, my question is : how can I sum my columns without changing my alias names?

0 Answers
Related