SQL update statement based unique value in another table - sql

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)

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

Query to update 1 column with no condition and another column only if certain condition is met

I have the below SQL Server query to update column type in tblTicket:
UPDATE A set type=B.type
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =#tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id
I also want to update column bid in table tblTicket, but only if a.name = "ML":
UPDATE A set bid = B.id
FROM tblTicket A
JOIN (select b.tblTicket_id,f.id
FROM tblP a
JOIN tblA b ON a.allocation_id = b.id
JOIN FIRM f ON f.firm = a.firm
WHERE b.tblTicket_id =#tblTicket_id and a.name = 'ML' Group By b.tblTicket_id,f.id) B
ON A.id=b.tblTicket_id
Can I combine the 1st query with the 2nd query above ?
Thank you.
Using CASE you can update a column if a certain condition is satisfied, otherwise you can "set" that column with the same value.
A "best practice" tip: avoid using same aliases (even switching from uppercase to lowercase and vice-versa) in your nested queries.
UPDATE A set type=B.type,
bid = case when a.name = 'ML' then B.id else bid end
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =#tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id

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

I'm trying to update the data in table column "Supplier_Base.CreditBalance" with the sum of the colum "Purch" in table "Suppliers_Account"

This was what i'm trying but it does not work.
UPDATE
dbo.Supplier_Base SET dbo.Supplier_Base.CreditBalance (
SELECT
SUM(dbo.Suppliers_Account.Purch ) AS BAL,
FROM
dbo.Suppliers_Account
INNER JOIN
dbo.Supplier_Base ON dbo.Suppliers_Account.Code = dbo.Supplier_Base.Code
GROUP BY dbo.Suppliers_Account.Code, dbo.Supplier_Base.CreditBalance
HAVING (dbo.Suppliers_Account.Code = N'C003'))
Sub-queries aren't allowed in a SET statement. You can work around this by aliasing the tables and doing a JOIN instead.
Do you have a test environment where you can run queries with damaging production data? I think this is what you're looking for, but DO test it first:
UPDATE
b
SET
b.CreditBalance = SUM(a.Purch)
FROM
dbo.Suppliers_Account a
INNER JOIN
dbo.Supplier_Base b
ON
a.Code = b.Code
WHERE
a.Code = N'C003'
GROUP BY
a.Code
This is the correct syntax:
UPDATE SB
SET CreditBalance = SA.Purch
FROM dbo.Supplier_Base SB
INNER JOIN (SELECT Code, SUM(Purch) Purch
FROM dbo.Suppliers_Account)
GROUP BY Code) SA
ON SB.Code = SA.Code
WHERE SB.Code = N'C003'