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.
Related
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;
I'm trying to update a temporary table with multiple values from another table without using a join.
However, the query doesn't give any error but rather returns an asterisk as the value of the column. I have googled and asked some folks around the office but no one seems to have encountered this before or can offer explanation of why this could be happening.
update ##tempCLUnique set Total =
(
select COUNT(distinct u.unique_subs)
from tbl_Cluster_Cumm_Unique_Subs u
where u.cluster = ##tempCLUnique.cluster
)
Seems simple enough
Result Screen Grabhttp://i.stack.imgur.com/qE0ER.png
Use this
update ##tempCLUnique set Total = U.unique_subs
FROM ##tempCLUnique
INNER JOIN
(
select COUNT(distinct unique_subs)unique_subs
from tbl_Cluster_Cumm_Unique_Subs
)U
ON
u.cluster = ##tempCLUnique.cluster
Change the join according to your use.
Ashutosh
I am new to SQL and despite hours of searching, cannot figure out the SQL query to update records in my members table, based on conditions in my payments table. I'm very confused whether I use a JOIN (and if so what kind) or a Subquery?
Here's what I have so far:
UPDATE wp_mcra_members
SET wp_mcra_members.dues_paid = 1
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
WHERE wp_mcra_payments.year_paid = '2013' and wp_mcra_payments.reason = 'Dues';
I want the databased to search for any records in the Payments table that meet my conditions of being year 2013 and labeled Dues. Then I want the Members table to update the field dues_paid based on any found records matching those conditions, where the Member ID = Payments Member
The syntax for an update with a join varies by database. Here is generic syntax using a subquery in the where clause:
UPDATE wp_mcra_members
SET dues_paid = 1
where wp_mcra_members.id in (select wp_mcra_payments.member
from wp_mcra_payments
WHERE wp_mcra_payments.year_paid = '2013' and
wp_mcra_payments.reason = 'Dues'
) ;
The syntax you supplied looks like it would work if you are using SQL Server. Oracle does not support JOINs with UPDATE, instead look into using MERGE or Gordon's answer would also work there.
Assuming however you are using MySQL, a JOIN in the UPDATE statement would probably have a better performance than using IN:
UPDATE wp_mcra_members
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
SET wp_mcra_members.dues_paid = 1
WHERE wp_mcra_payments.year_paid = '2013'
and wp_mcra_payments.reason = 'Dues';
I was looking through the AskTom site and found what should be a very powerful tool for update statments but my statement refuses to accept the alias after the subquery.
Can someone please explain this for me and possibly show a solution?
Update (SELECT T.Date_,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET P.ID = T.Name_
There may be other issues as to how to run this type of update and That would be a bonus. I am more interested at this point in trying to understand the alias problem. I have tried to sub in
TableB.ID = TableA.Name_
but no luck
Specifically the error is ORA-00904 invalid identifier.
As always thanks in advance.
The above issue has been answered for me but I do have some others as I try to understand it.
I now get ORA-01779 cannot modify a column which maps to a non-key preserved table.
I presume this is referring to the table I'm trying to update correct? As I could update from a view or any other proper source that may not have a pk or fk.
Can this type of update statement work with Oracle temp tables or can I expect problems?
Can I use this type of statement with a case statement to update multiple columns in TableA or will there be problems?
Thanks again.
When you perform an UPDATE (query) operation, the only columns you can use outside the parentheses are the names of columns returned by the query - in this case Date_ and Name_. The table aliases you used in the query are not valid outside the parentheses either.
What you therefore need is:
Update (SELECT P.ID ,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET ID = Name_
I have a spreadsheet that I am converting to an Access DB. I have a column of typed out customer names that I want to replace with the appropriate customer number from our accounting system.
I have created a table with the customer info, and a query that shows what ID needs to be inserted into the source data. What I'm looking for is:
UPDATE tblStarting_Data
SET CustomerID=x
WHERE TEMPCustomer=y
Where X and Y come from qryIDPerCustomer.
Can I use a loop? How do I reference another query?
Another possibility in MS Access (object names borrowed from Tomalak answer):
UPDATE tblStarting_Data, qryIDPerCustomer
SET tblStarting_Data.CustomerID=qryIDPerCustomer.CustomerID
WHERE tblStarting_Data.TEMPCustomer=qryIDPerCustomer.CustomerName
I think a JOIN will help you:
UPDATE
tblStarting_Data AS sd
INNER JOIN qryIDPerCustomer AS qc ON sd.TEMPCustomer = qc.CustomerName
SET
sd.CustomerID = qc.CustomerID;
This can be expressed as a correlated sub-query as well (though the join syntax is preferable):
UPDATE
tblStarting_Data
SET
CustomerID = (
SELECT CustomerID
FROM qryIDPerCustomer
WHERE CustomerName = tblStarting_Data.TEMPCustomer
)
No need for a loop, both statements will update all records in tblStarting_Data in one step.