I'm trying to update some rows in a table joined to another table. The left table is indexed by ID; the right table is joined on the same ID but has multiple rows per ID. My query looks like:
UPDATE t1
SET t1.modified = t2.created
FROM table1 t1
INNER JOIN table2 t2
ON t1.ID = t2.ID
Note that t1.modified and t2.created are both datetime.
As stated, t2 has several rows per ID, each with a different created value (so the primary key is t2, created). What I want to do is set the max value of t2.created=t1.modified. However, when joining, the t1.modified value gets updated in no particular order, so whichever row is updated last, that's the value that t1.modified gets. I tried using t1.modified=max(t2.created), but apparently I can't use aggregate functions in an update query, nor can I use an order clause (i.e. order the rows so that the last row updated will effectively be the latest value of t2.created) in the update query.
Any help you can provide me is much appreciated! Thanks!!
how about this? will this fit your need?
UPDATE t1
SET modified = isnull((SELECT max(t2.created)
FROM table2 t2
WHERE t2.ID = t1.ID), modified)
FROM table1 t1;
Use the isnull function to set modified to be itself if the value returned is null. that should take care of the null issue.
You could do it this way, although it may not be standard SQL. However it should work for SQL Server.
update t1
set t1.modified = (select max([created]) from t2 where t1.ID = t2.id)
Related
I am trying to update column from one table to another table of different database.
I searched on stack but found answers for PostgreSQL and SQLite. Those code ain't worked for me properly in SQL Server.
Say,
D1,D2 are the two different databases
T1 is the table
C1,C2,C3 are the columns in T1
I want to update like
UPDATE T1
SET D2.T1.C1 = D1.T1.C1
WHERE D2.T1.C2 = D1.T1.C2
Everything except the where clause works fine.
Here is some code I tried:
use DB2
GO
UPDATE TABLE1
SET COL1 = (SELECT COL1 FROM DB1.dbo.TABLE1)
WHERE COL2 = DB1.dbo.TABLE1.COL2
How shall I write this query?
Note: D1 and D2 are identical. Both have the exact same schema. Just the name and data are different. Both databases are on the same server.
SQL Server supports an update join syntax:
UPDATE t1
SET COL1 = t2.COL1
FROM TABLE1 D1.t1
INNER JOIN TABLE2 D2.t2
ON t1.COL2 = t2.COL2;
Actually, your current approach might work, but you should try changing it to this:
UPDATE TABLE1 D1.t1
SET COL1 = (SELECT t2.COL1 FROM TABLE2 D2.t2 WHERE T1.COL2 = T2.COL2);
You can simply use an INNER JOIN cause sql-server support Cross-Database Queries, so you can do as:
UPDATE T1
SET T1.Col1 = T2.Col1
FROM DataBase1.Schema.TableName T1 INNER JOIN DataBase2.Schema.TableName T2
ON T1.ID = T2.ID;
Please, visit and read Cross-Database Queries.
Or update tableName set column Name = (select columnName from second table where 1=1) i.e condition is either true or false optional though
Update details
set details.a=a, details.b=b
where Id=10 select a,b from #userdefinedtabletype
Here id is unique for all the records
Since you have not provided much detail as to what either table has in it I am going to assume that your user defined table only has one row in it. Otherwise you will get the last values that are returned from the query.
So I would use a CROSS APPLY to do the update such as:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
CROSS APPLY (select a,b from #userdefinedtabletype) t2
where t1.Id=10
If in fact these tables are related by an ID of some sort then you would probably want to do an INNER JOIN:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
INNER JOIN #userdefinedtabletype t2 on t1.id = t2.id
where t1.Id=10
I would appreciate if someone could help out.
There are two tables that I need to combine. So far I have been doing this every time.
SELECT * INTO TABLE_3
FROM TABLE_1 LEFT JOIN TABLE_2
ON TABLE_1.[DATE] = TABLE_2.[DATE1]
However, I would like to skip the part of creating a new table and insert the columns I need directly into the existing table.
I tried doing this,
INSERT INTO [TABLE_1] (USD,EUR,RUR)
SELECT USD,EUR,RUR
FROM TABLE_1 AS T1 LEFT JOIN TABLE_2 AS T2
ON T1.[DATE] = T2.[DATE1]
but got an error saying that my column names are ambiguous
I use SQL server 2014.
Instead of giving the column name directly, please specify the alias name to say from which table the column should take. May be here both tables having same column the you are trying to select. You should specify the exact table
INSERT INTO [TABLE_1] (USD,EUR,RUR)
SELECT [T1/T2].USD,[T1/T2].EUR,[T1/T2].RUR
FROM TABLE_1 AS T1 LEFT JOIN TABLE_2 AS T2
ON T1.[DATE] = T2.[DATE1]
Either you can specify T1 or T2 as per your business logic. Please rewrite the query as mentioned here. This will solve the problem. Please try this.
I am querying a Microsoft SQL Server 2012.
The primary table (T1) structure contains account details:
AccountID, Name, Address
This table is dropped and recreated using external data nightly. We need to display this information but also need to exclude some of the records. Since we have no access to the external data we can't just add a column.
So we created a table (T2) to mark all the accounts we would like to exclude. It just has 2 fields:
AccountNo, Type
So we populated T2 and for every account we wanted to exclude from the display we gave the Type field a value of 'ex' (for exclude). We have no entries for the account we want to display.
When I execute the following query:
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type != 'ex'
The above query returns and empty set.
If I run a query to look for the value 'ex' (remove the !):
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type = 'ex'
The query returns the rows with that field populated with 'ex', as you expect.
I can search for NULL or NOT null with success but we need to use this extra table to do some other data manipulation in the future. In other words, we will not just be populating this field with "ex".
I'm wondering why I can't query the field in the joined table by looking for a Boolean false for a string. Is is because since the column doesn't exist in the table that is joined (T2) that it doesn't actually exist in the data set?
If that's the case how would I execute a query to return the records that do not equal a value in the joined table, whether that record exists in the joined table or not.
You can use the ISNULL solution like mentioned in the comments.
Another way you could write the query is this:
SELECT #t1.AccountID AS acct, #t1.Name AS [name], #t1.Address AS [add]
FROM #t1
LEFT JOIN #t2 ON #t1.AccountID = #t2.AccountNo
AND #t2.type = 'ex' --In case you add additional types to #t2
WHERE #t2.AccountNo IS NULL;
I need my code to set values of a column based on two criteria: the value of a different column in the same table and a value of a column in a different table. Is there a way to use the where statement to look at criteria in different tables?
Below is pseudo code that doesn't work but captures what I am trying to do.
UPDATE Table1
SET CustomerStatus_2003 = 'New'
FROM Table1, Table2
WHERE Table1.Column1 = 'New'
AND Table2.Column1 = '2003'
AND Table1.Column2= Table2.Column2
Thanks for your help!
You should always alias your tables and use a JOIN when you are using more than one table in a statement. Here is some code that does what you are looking for
UPDATE t
SET myColumn = 'New'
FROM MyTable t
JOIN OtherTable t2 ON t.Column = t2.Column
WHERE t.SomeCondition
AND t2.SomeOtherCondition