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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare columns

Author  Topic 

miked1978
Starting Member

25 Posts

Posted - 2009-01-27 : 09:28:10
I have a table (named History_Act) that has Date1, Division, History, and Actuals as columns. I want to create another column that takes the greater value of History or Actuals.

Example:

Date1 Division History Actual NewColumn
1/1/08 pipe 251 185 251
1/3/08 pipe 252 259 259

What would my script look like to do this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-27 : 09:33:41
[code]
UPDATE t
SET t.NewColumn=tmp.Val
FROM Table t
INNER JOIN (SELECT Date.MAX(Val) AS Val
FROM
(
SELECT Date1 AS Date,History AS Val
FROM Table
UNION ALL
SELECT Date1,Actual
FROM Table1
)r
GROUP BY Date
)tmp
ON tmp.Date=t.Date1
[/code]
Go to Top of Page
   

- Advertisement -