How to copy column from one table to another using JOIN - sql

What is wrong with the following PostgreSQL query?
UPDATE project_project SET project_project.create_date = assignments.start_date
FROM project_project
LEFT JOIN account_analytic_account ON account_analytic_account.id = project_project.analytic_account_id
LEFT JOIN assignments ON assignments.accounts_ref = account_analytic_account.id
gives
[SQL] UPDATE project_project SET project_project.create_date = assignments.start_date
FROM project_project
LEFT JOIN account_analytic_account ON account_analytic_account.id = project_project.analytic_account_id
LEFT JOIN assignments ON assignments.accounts_ref = account_analytic_account.id
[Err] ERROR: table name "project_project" specified more than once
I tried,
UPDATE pp SET pp.create_date = am.start_date
FROM project_project as pp
LEFT JOIN account_analytic_account as aa ON aa.id = pp.analytic_account_id
LEFT JOIN assignments as am ON am.accounts_ref = aa.id
gives
[SQL] UPDATE pp SET pp.create_date = am.start_date
FROM project_project pp
LEFT JOIN account_analytic_account aa ON aa.id = pp.analytic_account_id
LEFT JOIN am ON am.accounts_ref = aa.account_analytic_account.id
[Err] ERROR: relation "pp" does not exist
LINE 1: UPDATE pp SET pp.create_date = am.start_date
What is the correct syntax?

There are two problems to avoid:
1- in SET column=value, do not prepend a table name or alias to column. It yields an error and is useless anyway, since UPDATE tablename is only able to update columns from tablename. An alias of tablename can be used in other places, but not in the SET clause.
2- don't repeat the name of the table to update in the rest of the query unless you want to specifically induce a secondary and uncorrelated scan of this table.
Here is my proposal of a modified query that I hope does what you intend:
UPDATE project_project pp SET create_date =
(select assignments.start_date FROM account_analytic_account LEFT JOIN assignments
ON assignments.accounts_ref = account_analytic_account.id
WHERE
account_analytic_account.id = pp.analytic_account_id);

Related

SQL update statement based unique value in another table

I am trying to do an SQL UPDATE query to set a value for b.[Disposition] WHERE the i.uid field is unique
The following select statement returns the correct rows.
Select distinct i.*
FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b
on b.[CLI] = i.[CLI]
and b.[Quantity] = i.[Quantity]
and b.[UnitCost] = i.[UnitCost]
and b.[TotalCost] = i.[TotalCost]
and b.[Description] = i.[Description]
However I am unsure how to incorporate that into an SQL UPDATE statement.
Any help greatly appreciated.
Try this
UPDATE upb
SET b.Disposition = "YOUR VALUE"
FROM [DaisyCompare].[dbo].[Baseline] ubp
INNER JOIN (Select distinct i.* FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description] )tmp ON Tmp.UID = ubp.UID
Your question is not very clear but you can do something like this
update b
set -- your fields here
FROM [DaisyCompare].[dbo].[Baseline] b
inner join [dbo].[Imported] i on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
I think this works..Mention the column name equal to
update b
set b.disposition = i.xxxx from [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
update [b].[Disposition] set x(Desired value)
from [dbo].[Imported] i inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] and
b.[UnitCost]=i.[UnitCost] and
b.[TotalCost]=i.[TotalCost] and
b.[Description]=i.[Description] where i.uid = y(Desired value)

How to use few joins with one right join in table SQL Server

Hi i have one select with few joins and i want to show all rows from one table(RIGHT JOIN).
here is my query
MERGE #Players AS Target
USING (SELECT DT.[TimeId] AS [Selector],
[CurrencyId],
PT.Name [ProductType],
COUNT(*) FirstTimeDepositors,
PT.Id FirstDepositProductTypeId
FROM [WarehouseMgmt].[DimPlayer] DP
JOIN [WarehouseMgmt].[DimTimeZone] DT ON DP.[FirstDepositTimeId] = DT.TimeUTCId
RIGHT JOIN [WarehouseMgmt].[DimProductType] PT ON PT.Id = DP.FirstDepositProductTypeId
AND [FirstDepositTimeId] BETWEEN #DimStartDateUTC AND #DimEndDateUTC
AND DP.[IsInternalAccount] = 0
GROUP BY DT.[TimeId],[CurrencyId],PT.Name,PT.Id) AS Source
ON (Target.[Time] = Source.[Selector] AND Target.[CurrencyId] = Source.[CurrencyId] AND Target.[ProductType] = Source.[ProductType] )
WHEN MATCHED THEN
UPDATE SET Target.[FirstTimeDepositorsCounts] = Source.[FirstTimeDepositors]
WHEN NOT MATCHED BY TARGET THEN
INSERT ([Time],[CurrencyId],[ProductType],[FirstTimeDepositorsCounts],[FirstDepositProductTypeId])
VALUES (Source.[Selector],Source.[CurrencyId],[ProductType],Source.[FirstTimeDepositors],Source.[FirstDepositProductTypeId]);
But this doesn't work fine for me , plus i want to have WHERE clause if it's possible with right join
If you want to filter the joined table you can add it to the join clause or use a subquery.
RIGHT JOIN [WarehouseMgmt].[DimProductType] PT
ON PT.Id = DP.FirstDepositProductTypeId And PT.Field = "What you want"
or
RIGHT JOIN
(Select Fields From [WarehouseMgmt].[DimProductType] Where Field = "What you want") PT
ON PT.Id = DP.FirstDepositProductTypeId

Update table inner join with 3rd table

I have a Fact_Actuals yable with a new column "Segment_Id".
I need to insert the segment id from DimsegmentMaster into Fact_Actuals.
Link is with SegmentMaster and Source Table column.
Link is with FactTable is Measures of the Source Table column below.
Kindly provide me the update query, as my below query is not fine.
UPDATE Application_DB.[cdw].[Fact_Actuals]
set segment_sid =
(SELECT c.SID
FROM Application_DB.[cdw].[Fact_Actuals] b
inner join Source_DB.STA.SourceTable a
ON convert(decimal(20,10),LTRIM(RTRIM(a.[K308]))) = b.NetExternalSales
and convert(decimal(20,10),LTRIM(RTRIM(a.[K203]))) = b.Quantity_CON
and convert(decimal(20,10),LTRIM(RTRIM(a.[K202]))) = b.Quantity_KG
inner join Application_DB.cdw.DimSegmentMaster c
ON RTRIM(a.[C005])=c.SegmentOriginal
)
Try this:
UPDATE b
set segment_sid = c.sid
FROM Application_DB.[cdw].[Fact_Actuals] b
inner join Source_DB.STA.SourceTable a
ON
convert(decimal(20,10),LTRIM(RTRIM(a.[K308])))=b.NetExternalSales
and convert(decimal(20,10),LTRIM(RTRIM(a.[K203])))=b.Quantity_CON
and convert(decimal(20,10),LTRIM(RTRIM(a.[K202])))=b.Quantity_KG
inner join Application_DB.cdw.DimSegmentMaster c
ON RTRIM(a.[C005])=c.SegmentOriginal

SQL Server update statement error

I am writing a script to update the duplicate contact and all its tables where it is referenced.
One of the update statements that I have is the following:
/* update the contactid, and the compcontactid on the compcontact table */
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
However, when I run the script (script is way tooo long to post here), I get the following error:
Msg 2601, Level 14, State 1, Line 255
Cannot insert duplicate key row in object 'dbo.compcontact' with unique index 'XPKcompcontact'. The duplicate key value is (A000UZCU, A00JTCAP, X00GM2NF).
Can anyone explain why this is happening, and what the fix would be?
Is this because It is attempting to update the value that has the
You are attempting to update a unique key value to a value that already exists in the column. This is not allowed, since each value in the unique key must be unique.
As #sion_corn said, you're trying to update a column having unique key constraint.
Try adding a WHERE clause in your update statement to exclude the value which already exists.
For example:
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cc.compcontactid <> cmpc.compcontactid)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cmpc.contactid <> tt.contactid

SQL Server update with joins

I am trying to update a date in a table, based off of a MAX(date) in another table. To get the correct data to link up, I have to do 2 inner joins and 2 left outer joins.
I can select the correct data, it returns a Guid (PersonId) and the Date.
I have to use this information to update my original table. I am having trouble getting this to work, I still getting syntax errors.
update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken from tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
where types.intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid
Assuming you are using SQL Server for this, then this should work:
UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken
from tblqualityassignments as assign
inner join tblrequirementteams as team
on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg
on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs
on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as [types]
on [types].intcerttypeid = certs.intcerttypeid
where [types].intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid