I want to perform update operation by joining two different tables.
My tables are :
dim_sess
name role
20111012133513aaa123
20110908072611aaa121
20111002210235bbb853
20120113113353bbbl971
The role column is updated later.
and the other table is
employeerole
username role thedate
aaa technician 2011-10-12 13:35:13
aaa technician 2011-09-08 07:26:11
bbb day guard 2011-10-02 21:02:35
bbb day guard 2012-01-13 11:33:53
bbb night guard 2012-01-13 21:30:00
I want to update role in the dim_sess table depending upon username and startdate. The same user can have different role in different date. There is no common column in two table and the only column to perform join is “name” in dim_sess.
I write the following query to update the dim_ses table.
UPDATE dim_sess
SET role = (SELECT employeerole.role FROM employeerole
WHERE SUBSTRING(dim_sess.name,15,7) = employeerole.username
AND SUBSTRING(dim_sess.name,1,14) = (TEXTCAT( TEXTCAT(TEXTCAT(SUBSTRING(thedate,1,4), SUBSTRING(thedate,6,2)) , TEXTCAT(SUBSTRING(thedate,9,2), SUBSTRING(thedate,12,2)) ), TEXTCAT( SUBSTRING(thedate,15,2), SUBSTRING(thedate,18,2)))))
WHERE SUBSTRING(dim_sess.name,1,21) = (SELECT TEXTCAT((TEXTCAT(TEXTCAT(TEXTCAT(SUBSTRING(thedate,1,4), SUBSTRING(thedate,6,2)) , TEXTCAT(SUBSTRING(thedate,9,2), SUBSTRING(thedate,12,2))), TEXTCAT(SUBSTRING(thedate,15,2), SUBSTRING(thedate,18,2)))) , employeerole.username) AS session FROM employeerole);
The error message I get is:
ERROR: more than one row returned by a subquery used as an expression
Since there is no common column between two tables, I try to join them by using the substring function to match the results. But this seems to be a bad solution. I was wondering if there is other way to join these two tables.
Try this:
I’ve tested this in Oracle but I think you might be using PostgreSQL so the syntax might be a little different.