Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
s1lentb0b
Starting Member
3 Posts |
Posted - 2012-11-12 : 07:59:55
|
HiFirst time poster.I have a set of data that is a list of names and a list of values next to them. There is a 3rd column that is for an amended value. Basically I want to add the values together but if there is a value in the 3rd column I want to use that one instead of the real value.hope I have explained this well. |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-12 : 08:16:11
|
One of these two:SELECT [name], COALESCE(thirdColumn,SecondColumn) AS [Values]FROM YourTable; SELECT SUM(COALESCE(thirdColumn,SecondColumn)) AS SumOfValuesFROM YourTable; |
|
|
s1lentb0b
Starting Member
3 Posts |
Posted - 2012-11-12 : 09:11:12
|
will that ignore the first value and just use the amended value when there is one? |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-12 : 10:40:47
|
Yes - if the third column is NULL. COALESCE function goes down the argument list, starting at the first one. If the first one is not null, it picks that value. If that is null, it picks the second value, and so on. http://msdn.microsoft.com/en-us/library/ms190349.aspx |
|
|
s1lentb0b
Starting Member
3 Posts |
Posted - 2012-11-14 : 05:39:01
|
excellent thanks for the help. |
|
|
|
|
|