Update table inner join with 3rd table - sql

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

Related

Passing different column values to where clause

SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid = pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890'
which gives me the following output
Now I want to use the above output values to select the rows from the table "YesNoAnswerWithObservation"
I imagine it should look something like this Select * from YesNoAnswerWithObservation Where Id in (22,27,26,...23)
Only instead of typing the values inside IN clause I want to use the values in each column resulting from above-mentioned query.
I tried the below code but it returns all the rows in the table rather than rows mentioned inside the In
SELECT pims.yesnoanswerwithobservation.observation,
graphitegtccore.yesnoquestion.description,
pims.yesnoanswerwithobservation.id ObservationId
FROM pims.yesnoanswerwithobservation
INNER JOIN graphitegtccore.yesnoquestion
ON pims.yesnoanswerwithobservation.yesnoanswerid =
graphitegtccore.yesnoquestion.id
WHERE EXISTS (SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.pelvicorgandiseaseid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.gynocologicalscanid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid =
pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890')
Any help or a nudge in the right direction would be greatly appreciated
Presumably you want the ids from the first query:
SELECT awo.observation, ynq.description, ynq.id as ObservationId
FROM pims.yesnoanswerwithobservation awo JOIN
graphitegtccore.yesnoquestion ynq
ON awo.yesnoanswerid = ynq.id
WHERE ynq.id = (SELECT mer.id
FROM pims.pimscase c JOIN
pims.digitization d
ON c.digitizationid = d.id JOIN
pims.medicalexaminerreport mer
ON d.medicalexaminerreportid = mer.id JOIN
pims.icicimedicalexaminerreport imer
ON mer.id = imer.id JOIN
pims.icicimerfemaleapplicant ifa
ON imer.id = ifa.id
WHERE c.tiannumber = 'ICICI1234567890'
) ;
Notice that table aliases make the query much easier to write and to read.

Multiple Join not working on two string attributes

I have the following issue:
I have several tables in my Database, in order to check for a specific criteria I have to join several tables, where I use the following statement:
SELECT *
FROM (SELECT * FROM (((((((((SELECT * FROM table1 WHERE tab1_id = 1) AS A
INNER JOIN (SELECT * FROM table2 WHERE tab2_variable IS NOT NULL) AS B
ON A.variable = B.variable)
INNER JOIN (SELECT table3.*, IIF(year='XXXX', 0, 1) AS flag FROM table3) AS C
ON A.h_name = C.h_name)
INNER JOIN (SELECT * FROM table4 WHERE s_flag = 0) AS D
ON C.v_type = D.v_type)
INNER JOIN table5
ON C.ng_type = table5.ng_type)
INNER JOIN table6
ON C.part = table6.part)
INNER JOIN table7
ON C.ifg = table7.ifg)
INNER JOIN table8
ON C.v_type = table8.v_type AND C.ng_typ = table8.ng_typ AND C.ntr_flag = table8.ntr_flag)
INNER JOIN table9
ON table8.ifg_sii_id = table9.ifg_sii_id)) AS F
LEFT JOIN table10
ON F.risk = table10.risk AND C.v_type = table10.v_type
AND F.series = table10.series
This statement fails. But when I remove one the following two join-conditions in the last Left JOIN, it works as intended:
F.risk = table10.risk and/or C.v_type = table10.v_type
They are both of type CHAR, whereas series is type TINYINT, I guess it has something to do with joining on multiple conditions with strings, but I'm not able to find a workaround, any ideas?
according to your current SQL, Table C is not visible as it's a sub query within F. instead of C.v_type you need to use F.c_v_type and the c_v_type field must come from C table like. select v_type as c_v_type from table3... as C
In the last part of your Query You could try to actually select the table and include the Where Clause Like so:
LEFT JOIN (Select * from table10 Where C.v_type = v_type AND F.series = series) as xx
ON F.risk = xx.risk
Hope this helps

Code Explanation - Please

Sorry - I hope you don't mind me asking this question but I am new to SQL - Could someone comments on what these lines mean in this code:
select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
drop table #rectemp;
I understand that the general idea of this code. It is about how to update (or change) the value for two different columns: Attribute & Color. These two items are located in two different tables before I joined them with the appropriate UUID.
The update should take a place only when the Attribute = ‘SetupType’ and the Color = ‘Green’ or ‘Yellow’
I would like to change these two values to:
Attribute = ‘Normal’ and the
Color = ‘1’
select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
***#rectemp is a temp table. Data imported into the table***
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
***Column Value is updated to 1 based on Value is Green or yellow***
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
*** Name column is updated to Normal ***
drop table #rectemp;
*** Temp Table is dropped***

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)

SQL update statement inserting PeopleID into child tables to link data

I need to place the PeopleID in several tables for my new database to link all of the peole information. I have tried several times to write a simple update statement please help. Every time I get close I get AMBIGUOUS COLUMN ERROR I don't know what else to do.
Update CONTRACT
Set PeopleID = B.PeopleID
from People A
Inner join
(
Select PeopleId, F.ContractID
From People A
Inner Join Person PRSN on PRSN.PersonID = A.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id
) B on A.PeopleID = B.PeopleID
Go
try this
Update CONTRACT Set CONTRACT.PeopleID = B.PeopleID
from People A
Inner join (
Select A.PeopleId, F.ContractID
From People AA
Inner Join Person PRSN on PRSN.PersonID = AA.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id ) B
on A.PeopleID = B.PeopleID
you were asigning the ´A´ alias twice so I recomend using different aliases always