![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm trying to recalculate rates for all the entries marked by flag=1 using the rates from THE SAME table: |
#3
| |||
| |||
|
|
Could somebody please help me out with UPDATE-FROM ? My table 'Rates' has a structure: currency_id_1 int currency_id_2 int rate float flag boolean I'm trying to recalculate rates for all the entries marked by flag=1 using the rates from THE SAME table: update Rates set rate = t1.rate * t2.rate from Rates t1, Rates t2 where currency_id_1 = t1.currency_id_1 and t1.currency_id_2 = t2.currency_id_1 and t2.currency_id_2 = currency_id_2 and flag = 1 Sybase ASE 12.5 complains with: Ambiguous column name currency_id_1 What a problem here? How to bypass that (without temporary tables)? Thanks, Aleksey |
#4
| ||||
| ||||
|
|
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, datatypes, etc. in your schema are. Sample data is also a good idea, along with clear specifications. |
|
Do you know that ISO has currency code standards? That you never use FLOAT for commercial calculations (look up the rules for Euro conversion)? That BOOLEAN is not Standard SQL? |
|
The Standard UPDATE statement does not use a FROM clause. It makes no sense. |
|
Your code should use a scalar subquery, something like this -- I cannot figure out what you are trying to do by reading your code and your spec is not clear. |
#5
| |||
| |||
|
|
Alex wrote: Could somebody please help me out with UPDATE-FROM ? My table 'Rates' has a structure: currency_id_1 int currency_id_2 int rate float flag boolean I'm trying to recalculate rates for all the entries marked by flag=1 using the rates from THE SAME table: update Rates set rate = t1.rate * t2.rate from Rates t1, Rates t2 where currency_id_1 = t1.currency_id_1 and t1.currency_id_2 = t2.currency_id_1 and t2.currency_id_2 = currency_id_2 and flag = 1 Sybase ASE 12.5 complains with: Ambiguous column name currency_id_1 What a problem here? How to bypass that (without temporary tables)? Thanks, Aleksey Is it not just that when you reference currency_id_1 in the where clause you don't always specify the table? So it should be : where t2.currency_id_1 = t1.currency_id_1 <-- t2 added here and t1.currency_id_2 = t2.currency_id_1 and t2.currency_id_2 = t1.currency_id_2 <--t1 added here Adam |
#6
| |||
| |||
|
|
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, datatypes, etc. in your schema are. Sample data is also a good idea, along with clear specifications. Do you know that ISO has currency code standards? That you never use FLOAT for commercial calculations (look up the rules for Euro conversion)? That BOOLEAN is not Standard SQL? CREATE TABLE Rates (currency_code_1 CHAR(3) NOT NULL, currency_code_2 CHAR(3) NOT NULL, rate DEIMAL(12,4) NOT NULL); flag INTEGER NOT NULL, PRIMARY KEY (currency_code_1, currency_code_2)); I'm trying to recalculate rates for all the entries marked by flag=1 using the rates from THE SAME table: The Standard UPDATE statement does not use a FROM clause. It makes no sense. Your code should use a scalar subquery, something like this -- I cannot figure out what you are trying to do by reading your code and your spec is not clear. UPDATE Rates SET rate = rate * (SELECT T1.rate FROM Rates AS T1 WHERE Rates.currency_id_1 = T1.currency_id_2) WHERE flag = 1; |
#7
| |||
| |||
|
|
update Rates t0 set t0.rate = t1.rate * t2.rate from Rates t1, Rates t2 where t0.currency_id_1 = t1.currency_id_1 and t1.currency_id_2 = t2.currency_id_1 and t2.currency_id_2 = t0.currency_id_2 and t0.flag = 1 BUT - Sybase doesn't allow me to use alias t0 after 'Update Rates'. |
![]() |
| Thread Tools | |
| Display Modes | |
| |