Update a single column based on multiple conditions in an expression - sql

I am trying to update all the values in a single column. The column currently contains only NULL values.
There are two columns in the table to be updated and the table the new data should be taken from that need to match for the update to take place.
After looking up the problem, it does not seem to have been mentioned before in the way it needs to be solved here.
The code I have come up with so far is (and does not work a bit):
UPDATE ANSWER_PATTERN AS outer
SET outer.ANSWER_DURATION =
(
SELECT inner.ANSWER_DURATION
FROM PREP_ANSWER_DURATION AS inner
WHERE (inner.TEST_ITEM_EXT_ID,
inner.STUDENT_EXT_ID) =
(outer.TEST_ITEM_EXT_ID,
outer.STUDENT_EXT_ID)
);
So, how can I get the values from table2 in the column ANSWER_DURATION into table1, column ANSWER_DURATION, provided the TEST_ITEM_EXT_ID and STUDENT_EXT_ID columns both match?
I would be glad for any help provided. :-)

Here is how you would do that:
UPDATE outer, inner
SET outer.ANSWER_DURATION = inner.ANSWER_DURATION
WHERE inner.TEST_ITEM_EXT_ID=outer.TEST_ITEM_EXT_ID AND
inner.STUDENT_EXT_ID=outer.STUDENT_EXT_ID;

I think this achieves what you're looking for. My experience with your database flavor is nonexistent, but this is how I would approach the problem using PostgreSQL.
UPDATE ANSWER_PATTERN t1
SET ANSWER_DURATION = t2.ANSWER_DURATION
FROM PREP_ANSWER_DURATION t2
WHERE t2.TEST_ITEM_EXT_ID = t1.TEST_ITEM_EXT_ID
AND t2.STUDENT_EXT_ID = t1.STUDENT_EXT_ID;

Related

Updating a set of values using criteria from multiple tables

Given a specific set of rows that I've queried I want to update the "ifSpeed" value on each of them to a static value of 1000000000.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table. That query is the first block of code. The second is one of my attempts at updating the values. I've also looked into subqueries, but my SQL is very rusty as it has been about 8 years since I've worked with it.
SELECT ifSpeed
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
UPDATE master_dev.device_interfaces
SET ifSpeed = '1000000000'
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
My expectation is that all rows that exist in my SELECT query have their ifSpeed updated to 1000000000.
What actually happens is simply an error. I'm using a restricted interface to query the DB and it only provides rows affected, the data from the rows or simply "ERROR". Very helpful... I know.
in SSMS try Below
---- Updated ---
UPDATE MDDI
SET ifSpeed = '1000000000'
FROM master_dev.dbo.device_interfaces AS MDDI
JOIN master_dev.dbo.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed = 10000000
AND MDDITM.tag_id = 13
The syntax of UPDATE that has a FROM clause is rather cumbersome. Luckily in SQL Server there is a better way.
I usually use CTE (common-table expression) in this case, which makes the query very easy to write and read and verify that it works as intended.
So, you have your complex SELECT statement that returns all the rows that you want to update. Great.
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
;
Now, wrap it into CTE and update the CTE:
WITH
CTE
AS
(
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
)
UPDATE CTE
SET ifSpeed = '1000000000'
;
You can write SELECT * FROM CTE instead of the line UPDATE CTE SET ... before performing an actual update to verify that you are selecting correct rows.
Thanks for all the input on this! Unfortunately I had two different teams give me two different answers. While I supposedly was able to do this after escalating further up the chain the engineering team on the backend put up a brick wall saying don't risk the DB tables. That decision baffles me based on what's affected, but not my call to make I suppose.
That being said they did let me know that the original code was fine and likewise the variation that maSTAShuFu provided.
#Vladimir - Sorry didn't get to try your's out, but I'm definitely keeping it in my back pocket. Thank you!

Why am I asked for "Enter parameter value" when running update query in MS Access?

I've been struggling with my query and don't know what is wrong with it. I am not an advanced SQL user, but I have some understanding of SQL.
I have two tables and I am trying to update one field with the field from the other table. They do not have a unique identifier in each, so they need to be matched with three fields (HoleID, From, To), especially because table LEAPFROG_Lithology has more records that do not have the same From/To than in the Lithology table.
I have looked for questions asked on similar subject, and tried different variant of my query, but none work and it always asks me to "Enter parameter value".
I tried with an inner joint like in this one and one, but it did not work.
Here is what I tried:
Version 1
Update LEAPFROG_Lithology
SET LEAPFROG_Lithology.Primary_litho = (
SELECT Lithology.Primary_litho
FROM Lithology
RIGHT JOIN LEAPFROG_Lithology ON (
Lithology.[HoleID] = LEAPFROG_Lithology.[HoleID] and
Lithology.[From_m] = LEAPFROG_Lithology.[From_m] and
Lithology.[To_m] = LEAPFROG_Lithology.[To_m]))
Where LEAPFROG_Lithology.Primary_litho is null ;
Version 2 (tried with an INNER JOIN as well)
Update LEAPFROG_Lithology
LEFT JOIN Lithology ON (
LEAPFROG_Lithology.[HoleID] = Lithology.[HoleID] and
LEAPFROG_Lithology.[From_m] = Lithology.[From_m] and
LEAPFROG_Lithology.[To_m] = Lithology.[To_m])
SET LEAPFROG_Lithology.Primary_litho = Lithology.Primary_litho
Where LEAPFROG_Lithology.Primary_litho is null ;
I tried with [ ] everywhere, with none. It doesn't make a difference.
Can someone tell me what am I doing wrong?
Thanks!
Make sure both your tables can form a relationship before using JOIN, you need to make sure your PRIMARY KEY in one table corresponds to the FOREIGN KEY in the other table to join them.
Access lets you use a design view to create queries which can be easy if you're not fluent with SQL. Once you design it visually you can switch to SQL view to find the code.
Please verify that the names of the tables and fields are correct. If so, the second query should work.
But I suggest to use a slightly different bracketing, so you can display the graphical design view of the query. Also, there's no need to use a LEFT JOIN, because for records that have no Primary_litho and don't have a matching Lithology, the NULL value would be updated with a NULL value. So this should work without any problem:
UPDATE LEAPFROG_Lithology
INNER JOIN Lithology ON
(LEAPFROG_Lithology.[HoleID] = Lithology.[HoleID]) AND
(LEAPFROG_Lithology.[From_m] = Lithology.[From_m]) AND
(LEAPFROG_Lithology.[To_m] = Lithology.[To_m])
SET LEAPFROG_Lithology.Primary_litho = Lithology.Primary_litho
WHERE LEAPFROG_Lithology.Primary_litho Is Null;

UPDATE FUNCTION based on matching other column using SQL query in Access 2010

I have a table '1042sRegistration' I'm trying to update. It has a column 'IssueNumber' that I need to populate with values. Another table '1042sTransactions' has an 'IssueNumber' column as well that I want to pull the values from. These two tables also both have a column 'AccountNumber' that I'm trying to use as the condition to fill in 'IssueNumber' values. When I run the following query:
UPDATE 1042sRegistration
SET [1042sRegistration].IssueNumber = [1042sTransactions].IssueNumber
WHERE [1042sRegistration].AccountNumber = [1042sTransactions].AccountNumber;
0 rows are updated - even though I know they have thousands of matching 'AccountNumbers'
Try joining the two tables:
UPDATE 1042sRegistration INNER JOIN 1042sTransactions ON
1042sRegistration.AccountNumber = 1042sTransactions.AccountNumber
SET 1042sRegistration.IssueNumber = 1042sTransactions.IssueNumber
Perhaps your intention is:
UPDATE 1042sRegistration
SET [1042sRegistration].IssueNumber = (SELECT [1042sTransactions].IssueNumber
FROM [1042sTransactions]
WHERE [1042sRegistration].AccountNumber = [1042sTransactions].AccountNumber
);

Update query based on two conditions

I have the following table based on this query:
SELECT
repName.repID, repName.Rep_Name, repName.Job_Code, GenItems.Item_Name,
repName.Entered
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
ORDER BY
repName.Rep_Name
I want to add an update routine to it. I want to update the entered field if the user entry matches the rep.ID and the Item Name. and finally return the Max value for the Entered field. Can I add this to this query or is it better to write another.
I just started working with sql, so if my questions seems basic, please forgive me. I am self taught and stumbling greatly.
Thank you
I don't understand fully your question.
You are showing us a SELECT statement. It can only be used to return a table-like result. If you want to upate a table you must use an UPDATE query. For the SQL-Server (and SQL CE) the query looks like this:
UPDATE repName
SET repName.Entered = x
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
WHERE
repName.repID = x AND GenItems.Item_Name = 'y'
The difficulty is that tables have to be joined in the UPDATE statement. This not supported in Oracle for instance, where you have to do it with sub-selects.

Adding new fields to an existing table, inserting data into proper position, then joining

Scenario One
I have two new fields that I want to add to a table called existingTable. After I add these fields, I can update SOME but NOT ALL records with data for those fields. There will be blank entries, and I am fine with this.
Problem One
I want to make sure that the CORRECT records are updated. The primary key for the existing table and the incoming data table is Email.
Proposed Solution One
An UPDATE query looking like this is the solution.
UPDATE existingTable
SET existingTable.newField1 = incomingDataTable.newField1, existingTable.newField2 = incomingDataTable.newField2
WHERE existingTable.Email = incomingDataTable.Email
What do you think?
Scenario Two
After the table is updated with the new fields & data in the proper records, I want to join this table with two other ones. I want ALL entries, even if some fields are blank, to be in this join. I don't want ANY records excluded.
By the way, each record in these tables has a 1-to-1 relationship with its partner in the other tables. There SHOULD NOT BE ANY duplicate records. In the past, I've seen Access use an INNER JOIN, which excludes records that do not have values for newField1 and newField2. This is not what I want.
Problem
I'm inexperienced at joining tables. The different joins are a bit confusing to me.
Proposed Solution
Does the join I use necessarily matter since the three to-be-joined tables should have a one-to-one relationship?
SELECT * FROM existingTable
FULL JOIN tableToJoinWith1, tableToJoinWith2
On existingTable.Email = tableToJoinWith1.Email, tableToJoinWith1.Email = tableToJoiNWith2.Email
Clarifying your Scenario 2. I'm assuming you mean you want all the rows from existingTable even if there is no match on the Email field with either of the other tables. In this case, a LEFT JOIN is what you want:
SELECT * FROM existingTable
LEFT JOIN tableToJoinWith1 ON existingTable.email = tableToJoinWith1.email
LEFT JOIN tableToJoinWith2 ON existingTable.email = tableToJoinWith2.email
For scenario 1, the problem is that you haven't given it any sort of SELECT for incomingDataTable. In standard SQL, to my knowledge, there's no nice way to do this that supports multiple columns. So it depends what database you're using. Some will let you do this:
UPDATE existingTable
SET newField1 = incomingDataTable.newField1, newField2 = incomingDataTable.newField2
FROM incomingDataTable
WHERE existingTable.Email = incomingDataTable.Email
But some won't. Others will allow this:
UPDATE (Select * FROM existingTable JOIN incomingDataTable
ON existingTable.Email = incomingDataTable.Email)
SET existingTable.newField1 = incomingDataTable.newField1,
existingTable.newField2 = incomingDataTable.newField2
If it were only a single column, you could do this which is totally standard:
UPDATE existingTable SET newField1 = (SELECT newField1 FROM incomingDataTable
WHERE existingTable.Email = incomingDataTable.Email)