Compare a "temp' table with values in CTE, then update two different tables - sql-server

I have a "temp' table populated from an enrollment transportable in java. What I am doing is comparing the "temp" table with values I am populating in a CTE with a select query. What I need to do next is update two different tables. Here is my query for the comparison of the "temp" table and CTE:
WITH CTE AS
(
SELECT S.SYS_USER_NAME, PG.PAX_ID
FROM component.SYS_USER S
INNER JOIN component.PAX_GROUP PG
ON S.PAX_ID = PG.PAX_ID
WHERE (ROLE_CODE = 'AC' and THRU_DATE is null) or
(ROLE_CODE = 'DLRP' and THRU_DATE is null)
)
SELECT * FROM CTE
INNER JOIN component.TEMP_CONTROL_NUM
ON TEMP_CONTROL_NUM.CONTROL_NUM = CTE.SYS_USER_NAME
What I want to do next is update two different tables. One I need to set a status column as inactive and the other I need to set a thru date.
The issue I am having is writing an UPDATE with a SELECT. I have something like:
UPDATE component.SYS_USER SET STATUS = 'I'
WHERE SYS_USER_NAME =
(SELECT * FROM CTE INNER JOIN component.TEMP_CONTROL_NUM ON
TEMP_CONTROL_NUM = CTE.SYS_USER_NAME)
Would this be correct? I realize I am only attempting to update one table but I figure if I have one table updating, I can figure out the other. It doesn't seem to be.
Thank you in advance.

;WITH CTE as
(
...
)
UPDATE u SET
STATUS = 'I'
FROM component.SYS_USER u
INNER JOIN CTE c on c.SYS_USER_NAME = u.SYS_USER_NAME
INNER JOIN component.TEMP_CONTROL_NUM t ON t.TEMP_CONTROL_NUM = c.SYS_USER_NAME

Related

How to update multiple rows in a temp table with multiple values from another table using only one ID common between them?

I am trying to reconcile the IDs in a temp table (top) from another DB table (bottom). Since I only have one ID that's common between the two, I am only getting the top result for all the rows (ReconGlobalRemunerationGrantID) in my temp table. I am aiming to get each of the unique ID and update my temp table as such.
Right now, my update query is simple and I update using the ID common between the 2 tables. Is there a function or another command statement I can use to get the result intended?
update tgrg set ReconGlobalRemunerationGrantID = grg.GlobalRemunerationGrantID from #GlobalRemunerationGrant tgrg
join #GlobalRemuneration tgr on tgr.GlobalRemunerationID = tgrg.GlobalRemunerationID
join DataCore..GlobalRemuneration gr on gr.CompanyID = #CompanyID and gr.FiscalYearID = tgr.FiscalYearID and gr.DirectorDetailID = tgr.DirectorDetailID and tgr.GlobalRoleIDCODE = gr.GlobalRoleID
join DataCore..GlobalRemunerationGrant grg on gr.GlobalRemunerationID = grg.GlobalRemunerationID
Thank you.
Based on the comment - you have 2 values to match on, not just one? e.g., both GlobalRemunerationID and GlobalRemunerationGrantID?
Here's an example using tables 'temptable' and 't1'
UPDATE temptable
SET ReconGlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
FROM temptable
INNER JOIN t1 ON temptable.GlobalRemunerationID = t1.GlobalRemunerationID
AND temptable.GlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
Update below
The below version takes the two data sets
Partitions them by GlobalRemunerationID and orders them by ReconGlobalRemunerationGrantID to get the 'row numbers' (rn)
Joins them on GlobalRemunerationID and rn to get them in order
Key code is below (with slightly different tables than your full set sorry - matches the data set you gave though).
; WITH tgrg AS
(SELECT GlobalRemunerationID, ReconGlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM #GlobalRemunerationGrant
)
UPDATE tgrg
SET ReconGlobalRemunerationGrantID = tgr.GlobalRemunerationGrantID
FROM tgrg
INNER JOIN
(SELECT GlobalRemunerationID, GlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM GlobalRemuneration
) AS tgr ON tgrg.GlobalRemunerationID = tgr.GlobalRemunerationID AND tgrg.rn = tgr.rn
A db<>fiddle with the full set is there - note that I changed some of the IDs to demonstrate that it wasn;t using them to match.

Updating DISTINCTROW in SQL Server [duplicate]

What would the syntax be to convert this MS Access query to run in SQL Server as it doesn't have a DistinctRow keyword
UPDATE DISTINCTROW [MyTable]
INNER JOIN [AnotherTable] ON ([MyTable].J5BINB = [AnotherTable].GKBINB)
AND ([MyTable].J5BHNB = [AnotherTable].GKBHNB)
AND ([MyTable].J5BDCD = [AnotherTable].GKBDCD)
SET [AnotherTable].TessereCorso = [MyTable].[J5F7NR];
DISTINCTROW [MyTable] removes duplicate MyTable entries from the results. Example:
select distinctrow items
items.item_number, items.name
from items
join orders on orders.item_id = items.id;
In spite of the join getting you the same item_number and name multiple times when there is more than one order for it, DISTINCTROW reduces this to one row per item. So the whole join is merely for assuring that you only select items for which exist at least one order. You don't find DISTINCTROW in any other DBMS as far as I know. Probably because it is not needed. When checking for existence, we use EXISTS of course (or IN for that matter).
You are joining MyTable and AnotherTable and expect for some reason to get the same MyTable record multifold for one AnotherTable record, so you use DISTINCTROW to only get it once. Your query would (hopefully) fail if you got two different MyTable records for one AnotherTable record.
What the update does is:
update anothertable
set tesserecorso = (select top 1 j5f7nr from mytable where mytable.j5binb = anothertable.gkbinb and ...)
where exists (select * from mytable where mytable.j5binb = anothertable.gkbinb and ...)
But this uses about the same subquery twice. So we'd want to update from a query instead.
The easiest way to get one result record per <some columns> in a standard SQL query is to aggregate data:
select *
from anothertable a
join
(
select j5binb, j5bhnb, j5bdcd, max(j5f7nr) as j5f7nr
from mytable
group by j5binb, j5bhnb, j5bdcd
) m on m.j5binb = a.gkbinb and m.j5bhnb = a.gkbhnb and m.j5bdcd = a.gkbdcd;
How to write an updateble query is different from one DBMS to another. Here is the final update statement for SQL-Server:
update a
set a.tesserecorso = m.j5f7nr
from anothertable a
join
(
select j5binb, j5bhnb, j5bdcd, max(j5f7nr) as j5f7nr
from mytable
group by j5binb, j5bhnb, j5bdcd
) m on m.j5binb = a.gkbinb and m.j5bhnb = a.gkbhnb and m.j5bdcd = a.gkbdcd;
The DISTINCTROW predicate in MS Access SQL removes duplicates across all fields of a table in join statements and not just the selected fields of query (which DISTINCT in practically all SQL dialects do). So consider selecting all fields in a derived table with DISTINCT predicate:
UPDATE [AnotherTable]
SET [AnotherTable].TessereCorso = main.[J5F7NR]
FROM
(SELECT DISTINCT m.* FROM [MyTable] m) As main
INNER JOIN [AnotherTable]
ON (main.J5BINB = [AnotherTable].GKBINB)
AND (main.J5BHNB = [AnotherTable].GKBHNB)
AND (main.J5BDCD = [AnotherTable].GKBDCD)
Another variant of the query.. (Too lazy to get the original tables).
But like the query above updates 35 rows =, so does this one
UPDATE [Albi-Anagrafe-Associati]
SET
[Albi-Anagrafe-Associati].CRegDitte = [055- Registri ditte].[CRegDitte],
[Albi-Anagrafe-Associati].NIscrTribunale = [055- Registri ditte].[NIscrTribunale],
[Albi-Anagrafe-Associati].NRegImprese = [055- Registri ditte].[NRegImprese]
FROM [055- Registri ditte]
WHERE EXISTS(
SELECT *
FROM [055- Registri ditte]-- [Albi-Anagrafe-Associati]
WHERE ([055- Registri ditte].GIBINB = [Albi-Anagrafe-Associati].GKBINB)
AND ([055- Registri ditte].GIBHNB = [Albi-Anagrafe-Associati].GKBHNB)
AND ([055- Registri ditte].GIBDCD = [Albi-Anagrafe-Associati].GKBDCD))
Update [AnotherTable]
Set [AnotherTable].TessereCorso = MyTable.[J5F7NR]
From [AnotherTable]
Inner Join
(
Select Distinct [J5BINB],[5BHNB],[J5BDCD]
,(Select Top 1 [J5F7NR] From MyTable) as [J5F7NR]
,[J5BHNB]
From MyTable
)as MyTable
On (MyTable.J5BINB = [AnotherTable].GKBINB)
AND (MyTable.J5BHNB = [AnotherTable].GKBHNB)
AND (MyTable.J5BDCD = [AnotherTable].GKBDCD)

Issue in Update query

I have two tables called PassengerPaymentDetails and RoomInfo. Following is the query that I used to extract some values from existing PassengerPaymentDetails table.
SELECT
COUNT(*) AS Count, RequestReference, RoomTypeID, RoomCategory
FROM
[UL_SLHEV].[dbo].[PassengerPaymentDetails]
WHERE
Status != 0
GROUP BY
RoomTypeID, RoomCategory, RequestReference
As you can see I have RoomTypeID, RoomCategory and Count in the above mentioned table.
Following screenshot has the RoomInfo table:
I want to update the RoomInfo table data from the extracted Passengerpaymentdetails table. I can map these two tables with the RequestReference.
Need to update the count value in RoomInfo table according to the Passengerpaymentdetails table count value. Can anybody please help?
UPDATE:
Following is the code that I have tried so far. It is correctly return join table. I don't know how to set the value to the RoomInfo table with the getting table. And also here I am using left join for some purpose. I want to insert the value as well if the left table contains new row with the new roomtypeId. otherwise if the right table contains the same roomtypeID update the roomInfo with the updated value from passangerpaymentdetails table.
SELECT
t1.RequestReference as RoomInfoReq,
t1.Count as RoomInfoCount,
t1.RoomTypeID as RoomInfoID,
t1.RoomCategory as RoomInfoRoomCat,
l.RequestReference as PassangerReq,
l.Count as PassangerCount,
l.RoomTypeID as PassangerRoomTypeID,
l.RoomCategory as PassangerRoomCategory
FROM (
select
count(*) as Count,
RequestReference,
RoomTypeID,
RoomCategory
FROM
[UL_SLHEV].[dbo].[PassengerPaymentDetails]
where
Status!=0 group by RoomTypeID, RoomCategory, RequestReference)
as t1
Left JOIN RoomInfo as l on
t1.RequestReference = l.RequestReference and
t1.RoomTypeID = l.RoomTypeID and
t1.RoomCategory = l.RoomCategory and
l.Status!=0)
Something like that. It would be good if you provide DDL and DML instructions to test that, however you can see the logic how to do that:
UPDATE ri
SET [COUNT] = rd.[COUNT]
FROM RoomInfo ri
JOIN (SELECT
COUNT(*) AS [Count], RequestReference, RoomTypeID, RoomCategory
FROM
[UL_SLHEV].[dbo].[PassengerPaymentDetails]
WHERE
Status != 0
GROUP BY
RoomTypeID, RoomCategory, RequestReference) rd ON rd.RoomTypeID = ri.RoomTypeID
AND rd.RoomCategory = ri.RoomCategory
AND rd.RequestReference = ri.RequestReference

Update using three tables in netezza

I am trying to avoid a co-related sub query which in turn made me to update from three different tables and I am not quite sure how to do updates from three tables using netezza.
update stemp
set maxi = a.marks
from stemp
left join
sd696 sd
where st.id = sd.id
left join
(select id,MAX(marks) marks from sm696 group by ID) a
where a.id = sd.id;
Please help me
When you require joins in UPDATEs, the joins are specified implicitly in a comma separated FROM clause (without specifying the UPDATEd table again), with the JOIN criteria being specified in the WHERE clause.
Your UPDATE would look something like this:
UPDATE stemp
SET maxi = a.marks
FROM sd696 sd,
(
SELECT id,
MAX(marks) marks
FROM sm696
GROUP BY ID
)
a
WHERE stemp.id = sd.id
AND a.id = sd.id;

Update a table using JOIN in SQL Server?

I want to update a column in a table making a join on other table e.g.:
UPDATE table1 a
INNER JOIN table2 b ON a.commonfield = b.[common field]
SET a.CalculatedColumn= b.[Calculated Column]
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
But it is complaining :
Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'a'.
What is wrong here?
You don't quite have SQL Server's proprietary UPDATE FROM syntax down. Also not sure why you needed to join on the CommonField and also filter on it afterward. Try this:
UPDATE t1
SET t1.CalculatedColumn = t2.[Calculated Column]
FROM dbo.Table1 AS t1
INNER JOIN dbo.Table2 AS t2
ON t1.CommonField = t2.[Common Field]
WHERE t1.BatchNo = '110';
If you're doing something silly - like constantly trying to set the value of one column to the aggregate of another column (which violates the principle of avoiding storing redundant data), you can use a CTE (common table expression) - see here and here for more details:
;WITH t2 AS
(
SELECT [key], CalculatedColumn = SUM(some_column)
FROM dbo.table2
GROUP BY [key]
)
UPDATE t1
SET t1.CalculatedColumn = t2.CalculatedColumn
FROM dbo.table1 AS t1
INNER JOIN t2
ON t1.[key] = t2.[key];
The reason this is silly, is that you're going to have to re-run this entire update every single time any row in table2 changes. A SUM is something you can always calculate at runtime and, in doing so, never have to worry that the result is stale.
Try it like this:
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a INNER JOIN table2 b ON a.commonfield = b.[common field]
WHERE a.BatchNO = '110'
Answer given above by Aaron is perfect:
UPDATE a
SET a.CalculatedColumn = b.[Calculated Column]
FROM Table1 AS a
INNER JOIN Table2 AS b
ON a.CommonField = b.[Common Field]
WHERE a.BatchNo = '110';
Just want to add why this problem occurs in SQL Server when we try to use alias of a table while updating that table, below mention syntax will always give error:
update tableName t
set t.name = 'books new'
where t.id = 1
case can be any if you are updating a single table or updating while using join.
Although above query will work fine in PL/SQL but not in SQL Server.
Correct way to update a table while using table alias in SQL Server is:
update t
set t.name = 'books new'
from tableName t
where t.id = 1
Hope it will help everybody why error came here.
MERGE table1 T
USING table2 S
ON T.CommonField = S."Common Field"
AND T.BatchNo = '110'
WHEN MATCHED THEN
UPDATE
SET CalculatedColumn = S."Calculated Column";
UPDATE mytable
SET myfield = CASE other_field
WHEN 1 THEN 'value'
WHEN 2 THEN 'value'
WHEN 3 THEN 'value'
END
From mytable
Join otherTable on otherTable.id = mytable.id
Where othertable.somecolumn = '1234'
More alternatives here.
Seems like SQL Server 2012 can handle the old update syntax of Teradata too:
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a, table2 b
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
If I remember correctly, 2008R2 was giving error when I tried similar query.
I find it useful to turn an UPDATE into a SELECT to get the rows I want to update as a test before updating. If I can select the exact rows I want, I can update just those rows I want to update.
DECLARE #expense_report_id AS INT
SET #expense_report_id = 1027
--UPDATE expense_report_detail_distribution
--SET service_bill_id = 9
SELECT *
FROM expense_report_detail_distribution erdd
INNER JOIN expense_report_detail erd
INNER JOIN expense_report er
ON er.expense_report_id = erd.expense_report_id
ON erdd.expense_report_detail_id = erd.expense_report_detail_id
WHERE er.expense_report_id = #expense_report_id
Another approach would be to use MERGE
;WITH cteTable1(CalculatedColumn, CommonField)
AS
(
select CalculatedColumn, CommonField from Table1 Where BatchNo = '110'
)
MERGE cteTable1 AS target
USING (select "Calculated Column", "Common Field" FROM dbo.Table2) AS source ("Calculated Column", "Common Field")
ON (target.CommonField = source."Common Field")
WHEN MATCHED THEN
UPDATE SET target.CalculatedColumn = source."Calculated Column";
-Merge is part of the SQL Standard
-Also I'm pretty sure inner join updates are non deterministic..
Similar question here where the answer talks about that
http://ask.sqlservercentral.com/questions/19089/updating-two-tables-using-single-query.html
I think, this is what you are looking for.
UPDATE
Table1
SET
Table1.columeName =T1.columeName * T2.columeName
FROM
Table1 T1
INNER JOIN Table2 T2
ON T1.columeName = T2.columeName;
I had the same issue.. and you don't need to add a physical column.. cuz now you will have to maintain it..
what you can do is add a generic column in the select query:
EX:
select tb1.col1, tb1.col2, tb1.col3 ,
(
select 'Match' from table2 as tbl2
where tbl1.col1 = tbl2.col1 and tab1.col2 = tbl2.col2
)
from myTable as tbl1
Aaron's approach above worked perfectly for me. My update statement was slightly different because I needed to join based on two fields concatenated in one table to match a field in another table.
--update clients table cell field from custom table containing mobile numbers
update clients
set cell = m.Phone
from clients as c
inner join [dbo].[COSStaffMobileNumbers] as m
on c.Last_Name + c.First_Name = m.Name
Those who are using MYSQL
UPDATE table1 INNER JOIN table2 ON table2.id = table1.id SET table1.status = 0 WHERE table1.column = 20
Try:
UPDATE table1
SET CalculatedColumn = ( SELECT [Calculated Column]
FROM table2
WHERE table1.commonfield = [common field])
WHERE BatchNO = '110'

Resources