I have 3 tables
Table1:
emp_id, code_1, code_2
Table2:
code_1, code_2
Table3:
emp_id, emp_ind
Table2 is a lookup table. I need to update Table3 checking code_1 and code_2 of Table1 present in Table2.
If code_2 in Table2 is null, then check if code_1 in Table1 and Table2 match, if yes update emp_ind in Table3 as Y.
If code_1 in Table2 is null, then check if code_2 in Table1 and Table2 match, if yes update emp_ind in Table3 as Y.
If code_1 and code_2 both are valued in Table2, check if both code1 and code2 in Table1 match with that of Table2, if yes update emp_ind in Table3.
Is there a way to do it simply instead of checking like below:
Update table_3 t3
Set t3.emp_ind = 'Y'
Where exists (
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_1 = t2.code_1
Where t2.code_2 is null
And t2.emp_id= t3.emp_id
Union
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_2 = t2.code_2
Where t2.code_1 is null
And t2.emp_id= t3.emp_id
Union
Select 1 from table1 t1
Inner Join table2 t2
on t1.code_1 = t2.code_1
And t1.code_2 = t2.code_2
Where t2.emp_id= t3.emp_id
)
You can combine the conditional joins using OR operator like this:
Update table_3 t3
Set t3.emp_ind = 'Y'
Where exists (
Select 1 from table1 t1
Inner Join table2 t2
on
(t1.code_1 = t2.code_1 AND t2.code_2 is null)
OR (t1.code_2 = t2.code_2 AND t2.code_1 is null)
OR (t1.code_1 = t2.code_1 AND t1.code_2 = t2.code_2)
Where t2.emp_id= t3.emp_id
)
Related
[Below are two tables Table1 and Table2][1]
Table1:Column names id,name
Table2:Column names id,name
After the swap ,the name column data of Table1 will reflect in Table2 and name of Table2 will reflect in Table1.
I tried to resolve the issue using below query:
update table1 t set t.name=replace(t.name,(select name from T1 where T1.id=t.id),(select name from T2 where T2.id = t.id));
update table2 t set t.name=replace(t.name,(select name from T2 where T2.id=t.id),(select name from T1 where T1.id = t.id));
But,it is not giving correct result.
You can't do this in a single pass, as you are overwriting the values you want to move in the next step. Here's a solution using a temporary table:
SELECT * INTO #temp FROM table1;
UPDATE t1 SET name = t2.name FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.id;
UPDATE t2 SET name = t1.name FROM #temp t1 INNER JOIN table2 t2 ON t2.id = t1.id;
DROP TABLE #temp;
A little modification in your query and it worked.
create table temp1 as select * from table1;
update table1 set name = (select name from table2 where table2.id = table1.id);
update table2 set name = (select name from temp1 where temp1.id = table2.id);
Thanks,
Vinita Singh
I searched in SO but couldn't find anything for my purpose. I need to insert unique rows ONLY from one table into another. I have:
table1
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1331 elm 04/01/2016 04/30/2016 05/01/2016 5656
table2
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1441 elm 04/01/2016 04/30/2016 05/01/2016 5656
I need to insert into table1 the row with id = 2 in table2 AND also delete the row with id = 2 from table1, because bookid is different even though the rest of the columns match.
I tried following:
insert into table1
select * from table2
where not exists (select * from table2 where table1.id = table2.id
and table1.name = table2.name and table1.bookid = table2.bookid and
table1.bookname = table2.bookname and table1.start_date = table2.start_date
and table1.end_date = table2.end_date and table1.rel_date = table2.rel_date
and table1.rel_id = table2.rel_id)
Any way I can do all of this in one sql block?
In theory the following merge statement should achieve what you are looking for.
MERGE table1 [Target]
USING table2 [Source]
ON ([Target].[name] = [Source].[name]
AND
[Target].[bookname] = [Source].[bookname]
AND
[Target].[start_date] = [Source].[start_date]
AND
[Target].[end_date] = [Source].[end_date]
AND
[Target].[rel_date] = [Source].[rel_date]
AND
[Target].[rel_id] = [Source].[rel_id]
)
WHEN MATCHED AND ([Target].[bookid] <> [Source].[bookid]) THEN
UPDATE
SET [Target].[name] = [Source].[name]
,[Target].[bookid] = [Source].[bookid]
,[Target].[bookname] = [Source].[bookname]
,[Target].[start_date] = [Source].[start_date]
,[Target].[end_date] = [Source].[end_date]
,[Target].[rel_date] = [Source].[rel_date]
,[Target].[rel_id] = [Source].[rel_id]
WHEN NOT MATCHED THEN
INSERT(
[name]
,[bookid]
,[bookname]
,[start_date]
,[end_date]
,[rel_date]
,[rel_id]
)
VALUES
(
[Source].[name]
,[Source].[bookid]
,[Source].[bookname]
,[Source].[start_date]
,[Source].[end_date]
,[Source].[rel_date]
,[Source].[rel_id]
);
Note that there are some risks and limitations to this approach. If your [id] column has a uniqueness constraint, then it should be set as an identity column otherwise you will run into uniqueness violation errors. Also if [id] column value in table1 is different to [id] column in table2 then merge statement will keep the original [id] value from table1.
Basically this query simply updates your existing record in table1 with the matching record in table2 and insert new records from table2 into table1 if they don’t already exists.
All you should need to achieve your objective is this:
UPDATE T1
SET T1.bookid = T2.bookid
FROM Table1 T1
JOIN Table2 T2
ON T1.ID = T2.ID
However, to answer the question exactly as it was asked:
DELETE T1
FROM Table1 T1
JOIN Table2 T2
ON T1.ID = T2.ID
AND T1.bookid <> T2.bookid
INSERT INTO Table1
SELECT id, name, bookid, bookname, start_date, end_date, rel_date, rel_id
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.ID = T2.ID
AND T1.bookid = T2.bookid
WHERE T1.id IS NULL
Note that if your ID fields aren't unique, you'll need to add other conditions to the ON clauses.
If you are just concerned about updating the bookid value from table2, you can change the value of bookid with the below query
UPDATE t1 SET t1.bookid = t2.bookid
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
If you think your id column is not unique in two tables, you might need to consider adding other matching columns in the JOIN.
Here is the thing:
I have three tables:
Table1:
COL1 COL2
Table2:
COL2 COL3
Table3:
COL3 COL4
And I want to select the COL4 from Table3 when Table.COL3 = TABLE2.COL3, which the Table2.COL3 from when Table2.COL2 = Table1.COL2
It likes two join table, but when I use the following query it doesn't work.
SELECT * FROM
table3
INNER JOIN table2 ON table3.col3
=
(
SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2
)
You are selecting from table1 joining to the others. So table1 goes in the from.
SELECT TABLE3.col4
FROM TABLE1
JOIN TABLE2 ON TABLE2.COL2 = TABLE1.COL2
JOIN TABLE3 ON TABLE3.COL3= TABLE2.COL3
In the first table in the join statement. You have used a column name i.e. table3.col4 in stead of a table name i.e. table3. So, it's giving error.
Your correct query will be as follows
SELECT table3.col4 FROM table3
INNER JOIN table2
ON table3.col3 =
( SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2)
I am using SQL Server 2008. I have two tables, Table1 and Table2 as below.
Table1
ID Col1 Col2 Col3
-- ---- ---- ----
1 X Y Z
Table2
ID Col1 Col2 Col3
-- ---- ---- ----
1 1 2 3
I want to write a stored procedure, to return result something like below. And have to achieve this result without using cursor.
Result
Key Value
--- -----
X 1
Y 2
Z 3
Edited:
I required one result set.
Both IDs are parameter of my Store Procedure.
Since you are using SQL Server 2008, you can use CROSS APPLY with VALUES to unpivot data. You can first JOIN the two table on the id column and then unpivot it into the key/value columns:
select [key], value
from
(
select t1.col1, t2.col1 t2_col1,
t1.col2, t2.col2 t2_col2,
t1.col3, t2.col3 t2_col3
from table1 t1
inner join table2 t2
on t1.id = t2.id
) src
cross apply
(
values
(col1, t2_col1),
(col2, t2_col2),
(col3, t2_col3)
) c ([key], value);
See SQL Fiddle with Demo
select t1.col1 as [Key]
, t2.col1 as Value
from dbo.Table1 t1
join dbo.Table2 t2
on t1.id = t2.id
union all
select t1.col2
, t2.col2
from dbo.Table1 t1
join dbo.Table2 t2
on t1.id = t2.id
union all
select t1.col3
, t2.col3
from dbo.Table1 t1
join dbo.Table2 t2
on t1.id = t2.id
Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);