Revised Query.. the [Id] column is unique to all records.. the query should return the correct value for CorEURUSD to both Symbol = EURUSD and Symbol = GBPUSD where the [Time] = [Time] values.
CREATE TABLE [dbo].[Tck2](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Symbol] [varchar](35) NULL,
[Time] [datetime] NULL,
[CorEURUSD] [decimal](14, 10) NULL,
[CorEURUSD2] [decimal](14, 10) NULL
) ON [PRIMARY]
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('EUR/USD', '2011-07-01 12:04:28.000', 0.8229, 0.6488)
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('EUR/USD', '2011-07-01 12:26:17.000', 0.9427, 0.6558)
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('EUR/USD', '2011-07-01 12:58:34.000', 0.7713, 0.5267)
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('GBP/USD', '2011-07-01 12:04:28.000', 0, 0)
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('GBP/USD', '2011-07-01 12:26:17.000', 0, 0)
INSERT [VT7STAB1].[dbo].[Tck2] ([Symbol],[Time],[CorEURUSD],[CorEURUSD2]) VALUES('GBP/USD', '2011-07-01 12:58:34.000', 0, 0)
Running the following query in an attempt to copy the CorEURUSD column from Symbol – ‘EUR/USD’ into the resulting CorEURUSD column for Symbol = ‘GBP/USD
update Tck2
set CorEURUSD = (
select CorEURUSD
from Tck2 T
where Symbol = 'EUR/USD')
where Symbol = 'GBP/USD'
gave this error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
and when I used this revision..
update Tck2
set CorEURUSD = (
select CorEURUSD
from Tck2 T
where Symbol = 'EUR/USD')
where Symbol = 'GBP/USD'
and T.[Time] = [Time]
It throws this error.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier “T.Time” could not be bound.
I hope that is better, sorry for the ‘mass-confusion’ please revise answers to match the above query and table which should be correct.
Just a guess based on loose specs and no sample data / desired results.
EDIT 2011-07-03
Based on revised specs. Is [Time] really going to be your key for this type of update? Sounds risky. Anyway, since [Time] was the only way I could determine to join two rows based on your narrative and sample data, here is what I assume you mean (and I can also assume you only want to update CorEURUSD and not CorEURUSD2):
Really wasn’t that much of a modification really, I merely changed the join condition.