November 25, 2013 at 7:44 am
Friends, I am new to sql and I have a doubt, friends could assist me.
I would like to add a column to an existing table, this column would have two conditions.
Example: In the current table I have 3 columns (city, value, month), I would create a fourth column in that table with the name 'RJ_MES_JANEIRO' with the following condition (if the column field equals city Rio de Janeiro and the field of month column is equal to January so that fourth column will return the column field value, otherwise it returns zero.
Link the current table and I would like her to stay: http://imageshack.com/a/img96/7825/nsfg.png
Desde já agradecido a todos.
November 25, 2013 at 8:09 am
You can easily use a computed column for this.
create table #Something
(
CIDADE varchar(50),
VALOR int,
RJ_MES_JANEIRO as case when CIDADE = 'RIO DE JANEIRO' then Valor else 0 end
)
insert #Something
select 'RIO DE JANEIRO', 100 union all
select 'Sao Paulo', 200
select * from #Something
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply