Jenny Shu
Jenny Shu

Reputation: 57

Use a newly-created column

I'm new to SQL. I do not have write access so I created a temp table #temp and did the following:

select 
    *, round(var1/100,0) as year 
from #temp

select 
    id_bucket, year, sum(b_flag) as num_b
from 
    #temp
group by 
    id_bucket, year 
order by 
    id_bucket, year

Then an error occurs that says

invalid column name 'year'.

Why is that and what should I do?

Upvotes: 1

Views: 875

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270051

Presumably the query you want is:

select id_bucket, round(var1 / 100,0), sum(b_flag) as num_b
from  #temp
group by id_bucket, round(var1 / 100, 0) 
order by  id_bucket, round(var1 / 100, 0);

Upvotes: 1

Related Questions