In a SQL 2008 DB there are 2 tables of data that I need to use to build a result in a stored procedure. Table 1 looks something like this.
date name hour amount price
------------------------------------------
2009-10-12 tom 12 20 15.43
2009-10-13 fred 16 -10 6.98
Table 2 looks like this. Note that each hour is in a separate column versus all hours in a single column in table 1.
date name H12 H16
--------------------------------
2009-10-12 tom 15.75 0
2009-10-13 fred 0 12.54
I need to write a stored procedure that ends up with a result that looks like this.
date name hour amount price result actualsold
---------------------------------------------------------------------
2009-10-12 tom 12 20 15.43 positive 15.75
2009-10-13 fred 16 -10 6.98 negative 12.54
I can get most of the result I need doing this . . . yes, the easy part.
SELECT date,
name,
hour,
amount,
price,
case
when amount > 0 then 'positive'
when amount < 0 then 'negative'
end as result
FROM Table1
How do I get the value from Table 2 and add this to the actualsold column (preferable without using a cursor)?
I do not have control over the table schemas. The DB is running on SQL 2008 ENT.