Update table with a join [duplicate] - sql

This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 7 months ago.
I am trying to update data from 1 table into another, however I don't know the structure required.
UPDATE ITEM_WMS iw
JOIN ITEM_CBO ic ON iw.ITEM_ID = ic.ITEM_ID
SET iw.critcl_dim_1 = ic.unit_length,
iw.critcl_dim_2 = ic.unit_height,
iw.critcl_dim_3 = ic.unit_width
WHERE ic.COLOUR_DESC = 'B4F';

That's wrong syntax for Oracle. MERGE might be a simpler (better?) option:
MERGE INTO item_wms iw
USING item_cbo ic
ON (ic.item_id = iw.item_id)
WHEN MATCHED
THEN
UPDATE SET
iw.critcl_dim_1 = ic.unit_length,
iw.critcl_dim_2 = ic.unit_height,
iw.critcl_dim_3 = ic.unit_width
WHERE ic.colour_desc = 'B4F';
If it has to be UPDATE, then:
UPDATE items_wms iw
SET (iw.critcl_dim_1, critcl_dim_2, critcl_dim_3) =
(SELECT ic.unit_length, ic.unit_height, ic.unit_width
FROM item_cbo ic
WHERE ic.item_id = iw.item_id
AND ic.colour_desc = 'B4F')
WHERE EXISTS
(SELECT NULL
FROM item_cbo ic1
WHERE ic1.item_id = iw.item_id
AND ic1.colour_desc = 'B4F');
(exists part is here to skip rows that shouldn't be updated; otherwise, you'd set those columns to null)

Related

SQL Query taking a long time to run for result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
WITH
cust AS
(SELECT DISTINCT
C.swCustomerId AS EnduserId,
A.AssetID AS asset,
suite.ctName AS Product
FROM ReplicaCADS.dbo.TransactionHeader TH WITH (NOLOCK)
INNER JOIN ReplicaCRMDB.dbo.SW_CUSTOMER C WITH (NOLOCK) ON C.swCustomerId = TH.EndUserCustomerId
INNER JOIN ReplicaCADS.dbo.Asset A WITH (NOLOCK) ON TH.TransactionId = A.TransactionId
AND A.Status = 'Active'
INNER JOIN ReplicaCADS.dbo.AssetComponent AC WITH (NOLOCK) ON AC.AssetId = A.AssetId
AND AC.PrimaryFlag = 1
AND AC.Status = 'Active'
INNER JOIN ReplicaCADS.dbo.MaintenanceProgram MP WITH (NOLOCK) ON MP.AssetId = A.AssetId
AND IsLatest = 1
AND MP.Status = 'Active'
AND MP.EndDate <> '2099-12-31 00:00:00.000'
AND MP.MaintenanceType = 'Core'
INNER JOIN ReplicaCRMDB.dbo.[ct_Product_Suite] suite WITH (NOLOCK) ON suite.ctSuiteID = A.ProductSuiteID
AND suite.cTName LIKE 'DaaS'
INNER JOIN Salesforce.[dbo].[Apttus__APTS_Agreement__c] agr ON agr.Vantive_Org_ID__c = C.ctOrgId
AND Apttus__Status__c = 'Activated'
AND Agreement_Type__c = 'Licensing'
AND agr.Account_Geo__c LIKE 'APAC'
WHERE NOT EXISTS (SELECT 1
FROM salesforce..Priority_Customer__c pr
WHERE pr.Account_Org_Id__c = C.CtOrgId)
AND NOT EXISTS (SELECT 1
FROM ReplicaTransactionData..[Transaction] TN
WHERE TN.CustomerId = C.swCustomerId
AND Status = 'Pending'
AND QuoteType IS NULL)
AND NOT EXISTS (SELECT 1
FROM [Salesforce]..Large_Customer__c LDC
WHERE LDC.Org_ID__c = C.ctOrgId)
AND NOT EXISTS (SELECT 1
FROM [Salesforce].dbo.Account sac
WHERE sac.Org_ID__C = C.ctOrgId
AND High_Touch_Account__C = 'true')
AND NOT EXISTS (SELECT 1
FROM salesforce..Priority_Customer__c pr
WHERE pr.Account_Org_Id__c = C.ctOrgId)
AND NOT EXISTS (SELECT 1
FROM Salesforce..Asset_Maintenance_Program__c
WHERE frmAccount_Org_ID__c = C.ctOrgId
AND Maintenance_Type__c IN ('Advanced'))
AND EXISTS (SELECT 1
FROM ReplicaCADS..AssetpricingData AP
WHERE AP.AssetId = A.AssetId))
SELECT TOP 1
P.swLogin AS LoginId
FROM cust
INNER JOIN ReplicaCRMDB.dbo.SW_PERSON P WITH (NOLOCK) ON P.swCustomerId = EnduserId
AND P.swStatus = 'Current'
AND SWLogin IS NOT NULL
AND P.ctLocale = 'en-US'
INNER JOIN ReplicaCRMDB.dbo.CT_CONTACT_TYPE Contact WITH (NOLOCK) ON Contact.swContactId = P.swPersonId
INNER JOIN ReplicaCRMDB.dbo.CT_MC_USERS MCUsers ON MCUsers.swPersonID = P.swPersonId
AND (MCUsers.ctPassword = '32CA9FC1A0F5B6330E3F4C8C1BBECDE9BEDB9573'
OR MCUsers.ctPassword = '')
ORDER BY NEWID();
When trying to use both the below condition together its taking a long time
AND NOT EXISTS (SELECT 1
FROM Salesforce..Asset_Maintenance_Program__c
WHERE frmAccount_Org_ID__c = C.ctOrgId
AND Maintenance_Type__c IN ('Advanced'))
AND EXISTS (SELECT 1
FROM ReplicaCADS..AssetpricingData AP
WHERE AP.AssetId = A.AssetId)
Can you please help me in someway tweaking the above query to get the result in quick time
This can be massively helped by understanding how SQL works. But I would recommend that you remove most of the text fields in the joins, and using their int values instead that I suppose exist.
For instance - High_Touch_Account__C = 'true', this should probably be stored as a BIT inside of the DB, and as such, 1 or 0 would be the way to go, not 'true'. Similarily the Status = 'Active' should probably be replaced with using the int value for 'Active'.
Regarding the and not exists, I would probably create a temporary table at the start that gathers all of the things that you do not want in there, then simply do a left join and then "where join is null" basically. This could replace 25% of your code.
NOLOCK might also be something that you should look into.
If you upload the files with the data, It would be easier however to give you a reply on the most optimal way to do this, but as it sits, we've got no idea of what data exists.

Error while using update with inner join and select and where condition [duplicate]

This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 1 year ago.
I have some error when i tried to update on row in my database
update tbl_status
set document_status=11
from tbl_status
inner join tbl_log on tbl_status.id=tbl_log.id
where tbl_status.document_status=7 and tbl_log.IDS=662;
error :
SQL command not properly ended
any solution
In Oracle, that would be
update tbl_status a set
a.document_status = 11
where a.id = (select b.id --> possibly where a.id IN (select ...
from tbl_log b
where b.ids = 662
)
and a.document_status = 7;
Or, with merge:
merge into tbl_status a
using (select b.id
from tbl_log b
where b.ids = 662
) x
on (a.id = x.id)
when matched then update set
a.document_status = 11
where a.document_status = 7;

How to apply multiple combine condition in T-SQL and not let these condition individually effect result

I have two tables Question and Answers. Question table have column 'QuestionType' and its value is either 1 if question is of type text and 2 if question has pre-define answer options for example radio, drop-down
I am trying T-SQL where I need only answers against question if answer has value. now for QuestionType if value is null or empty then that is mean no answer which will be fillter out but for QuestionType 2, it will always null even if there is answer because there is another table holding that information.
I need Answer where value exist but also also questionType of 2 even with Null value in Answer
i did in following but condition rule out of questionType 2 which I don't want
FROM [dbo].[MyTable] AS sur
INNER JOIN [dbo].[SurveyQuestions] AS surQus ON sur.Id = surQus.SurveyId
INNER JOIN [dbo].[Questions] AS qus ON surQus.QuestionId = qus.Id
LEFT JOIN [dbo].[Responses] AS res ON res.SurveyId = sur.Id
LEFT JOIN [dbo].[Answers] AS ans ON res.Id = ans.ResponseId AND qus.Id = ans.QuestionId
LEFT JOIN [dbo].[AnswerOptions] AS ansOpt ON ans.Id = ansOpt.AnswerId
LEFT JOIN [dbo].[QuestionOptions] AS qusOpt ON ansOpt.QuestionOptionId = qusOpt.Id
WHERE con.Id = '00000011-0013-4D34-8888-7E7189CA348U'
AND (qus.QuestionType ='1' AND ans.Value IS NOT NULL ) //???????? NEED HELP HERE
If I understand your question you need an OR statement, with each set of conditions in their own set of parenthesis, surrounded by one main set of parenthesis.
FROM [dbo].[MyTable] AS sur
INNER JOIN [dbo].[SurveyQuestions] AS surQus ON sur.Id = surQus.SurveyId
INNER JOIN [dbo].[Questions] AS qus ON surQus.QuestionId = qus.Id
LEFT JOIN [dbo].[Responses] AS res ON res.SurveyId = sur.Id
LEFT JOIN [dbo].[Answers] AS ans ON res.Id = ans.ResponseId AND qus.Id = ans.QuestionId
LEFT JOIN [dbo].[AnswerOptions] AS ansOpt ON ans.Id = ansOpt.AnswerId
LEFT JOIN [dbo].[QuestionOptions] AS qusOpt ON ansOpt.QuestionOptionId = qusOpt.Id
WHERE con.Id = '00000011-0013-4D34-8888-7E7189CA348U'
AND (
(qus.QuestionType ='1' AND ans.Value IS NOT NULL )
OR
(qus.QuestionType ='2')
)

Update on Oracle with join [duplicate]

This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 5 years ago.
I have the Following Statement and need to create an update.
Select p.POS_NR, p1.VON_POS_NR
FROM table1 p. inner join table2 p1
ON p.Bestell_NR = p1.Vorgangs_NR
and p.Bestell_pos = p1.Vorgangs_pos
where NOT (p.POS_NR = p1.Von_Pos_NR)
Now I want to equal p.POS_NR and p1.Von_POS_NR, but I don't know how to create the update
I hope someone can help me
You can do this using a MERGE statement. I'll assume that TABLE1 has a primary key field named KEY_FIELD - you can substitute as necessary:
MERGE INTO TABLE1 t1
USING (SELECT p.KEY_FIELD, p.POS_NR, p1.VON_POS_NR
FROM TABLE1 p
INNER JOIN TABLE2 p1
ON p.BESTELL_NR = p1.VORGANGS_NR and
p.BESTELL_POS = p1.VORGANGS_POS
WHERE NOT (p.POS_NR = p1.VON_POS_NR) d
ON (d.KEY_FIELD = t1.KEY_FIELD)
WHEN MATCHED THEN
UPDATE
SET t1.POS_NR = d.VON_POS_NR;
Best of luck.

SQL Update values from one table column to another [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 6 years ago.
I have a simple select statement that identifies the rows I want to update. Basically i want to copy the vad_description to the vb_description and can't quite figure this out. Any Help would be appreciated.
SELECT
variant_bom.vb_id
,variant_bom.vb_description
,variant_detail.vad_description
FROM dbo.variant_bom
INNER JOIN dbo.variant_detail
ON variant_bom.vb_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
AND variant_setting.vas_discontinued_product = 0
Try something like this
UPDATE vb
SET vb.vb_description = vd.vad_description
FROM dbo.variant_bom vb
INNER JOIN dbo.variant_detail vd
ON vb.vb_vad_id = vd.vad_id
INNER JOIN dbo.variant_setting vs
ON vs.vas_vad_id = vd.vad_id
WHERE vs.vas_manufactured_variant = 1
AND vs.vas_discontinued_product = 0
Giving Alias name to tables will make your query more readable