Insert into Table using JOIN T-SQL - tsql

I want to insert into a specific column in my table A which belongs to DB 1
from my DB 2 table B
In table A I have a unique ID field called F6 same goes for table B field name F68; both fields are the same they are simply a copy of each other which gives me the opportunity to do a join on them.
So far so good, what I want now is to insert into my table A in the field F110 the values from table B F64 since I did a join on the "ID's" they should be in the right manner.
All fields are of type VARCHAR.
INSERT INTO [D061_15018659].[dbo].[A](F110)
SELECT v.F64,v.F68
FROM [VFM6010061V960P].[dbo].[B] v LEFT JOIN
ON v.F68 = F6
I have the problem that I have an error on "ON" why so ever I can't figure it out.

Your select query provide 2 columns ==> you need concatenate the columns
You need repeat the tabel A in join clause.
Try this :
INSERT INTO [D061_15018659].[dbo].[A] (F110)
SELECT
v.F64 || v.F68 as theNewF110
FROM
[VFM6010061V960P].[dbo].[B] v
LEFT JOIN
[D061_15018659].[dbo].[A] w ON v.F68 = w.F6

Related

DB2 SPUFI query

Using DB2 query, I need to fetch the address from Table A using multiple where condition and when the address is unknown in table A, I need to get the temporary address from Table B using multiple where condition from Both Table A and Table B.
The common field for both the table is Employee ID
Where condition should be,
A.Emp-dept = xxx
A.Emp-state = yyy
B.Emp-code = zzz
B.Emp-proj = AAA
I Tried the below query
SELECT A.EMP_ID
A.EMP_ADDR,
A.EMP_DEPT,
B.EMP_CODE,
B.EMP_TEMP_ADDR
FROM TAB A
LEFT OUTER JOIN TAB B
ON A.EMP_ID = B.EMP_ID
WHERE A.Emp_dept = xxx
A.Emp_state = yyy
B.Emp_code = zzz
B.Emp_proj = AAA
ORDER BY EMP_ID
But this query is not working, I am getting 0 rows as result
Expected Result
https://dbfiddle.uk/s7zr35wU
If there may or may not be a row in table B, then you need to put the filter in the ON clause instead of the WHERE. With it in the WHERE, you are basically treating table B as an INNER JOIN instead of a LEFT JOIN because it is joining the two tables together with the ON criteria, and then filtering it out with the WHERE criteria.
You need something like this:
SELECT A.EMP_ID
A.EMP_ADDR,
A.EMP_DEPT,
B.EMP_CODE,
B.EMP_TEMP_ADDR
FROM TAB A
LEFT OUTER JOIN TAB B
ON A.EMP_ID = B.EMP_ID
AND B.Emp_code = zzz
AND B.Emp_proj = AAA
WHERE A.Emp_dept = xxx
AND A.Emp_state = yyy
ORDER BY EMP_ID
This assumes that you are at least going to get a row from table A (with a NULL address or something). If that is not the case, you may need to use FULL OUTER JOIN instead of LEFT OUTER JOIN.

select opposite operation group by month SQL Redshift

Iam trying to extract a list of operation that was inactive in each month :
table 1 "all_opreration" is containing the whole list of operation id
table all_operation
the second table "active_operation" is containing the operations that was active on the specified month
table active operation
So I want to get "inactive operation" by month (for each month the operation that was not in active_operation table
==> Wished table :
wished table inactive operation
I have tried several ways but without success
Thank you in advance
You just need to left join and check the the right side is NULL. You will need to expand your all_operations to have all dates but this can be done with a cross join. Like this:
Set up:
create table all_ops (op_id varchar(32));
insert into all_ops values ('A'), ('B'), ('C');
create table active_ops (month date, op_id varchar(32));
insert into active_ops values ('2021-10-01', 'A'),
('2021-10-01', 'B'),
('2021-07-01', 'C');
Find the missing data / id pairs:
select l.month, l.op_id
from (
select month, op_id
from all_ops
cross join (select distinct month from active_ops) m
) l
left join active_ops r
on l.op_id = r.op_id and l.month = r.month
where r.op_id is null;

It takes a long time to compare two table values (large volume)

Table a has 100 row with name columns.
Table b is the same structure as Table A, but has 10 million rows.
Create a query to verify that the value of table a is not in table b.
However, comparing the value of table a with the value of table b takes too long.
I want to complete the work in 5 seconds, but I don't know how.
Below is the method I tried. Both table name columns have b-tree indexes.
1.
select
name
from
a
where
and not exists (select
name
from
b
where
a.name = b.name
);
select
a.name
from
a left outer join b
on a.name = b.name
where
b.name is null;
You want the following index on the B table:
CREATE INDEX name_idx ON b (name);
This should allow Postgres to rapidly lookup any of the 100 names in the a table against the above index. This should avoid the need to do a full table scan of the b table, which, as you are seeing, can be costly.

dynamically choose fields from different table based on existense

I have two tables A and B.
Both the tables have same number of columns.
Table A always contains all ids of Table B.
Need to fetch row from Table B first if it does not exist then have
to fetch from Table A.
I was trying to dynamically do this
select
CASE
WHEN b.id is null THEN
a.*
ELSE
b.*
END
from A a
left join B b on b.id = a.id
I think this syntax is not correct.
Can some one suggest how to proceed.
It looks like you want to select all columns from table A except when a matching ID exists in table B. In that case you want to select all columns from table B.
That can be done with this query as long as the number and types of columns in both tables are compatible:
select * from a where not exists (select 1 from b where b.id = a.id)
union all
select * from b
If the number, types, or order of columns differs you will need to explicitly specify the columns to return in each sub query.

T-SQL Need help to optimize table value function

I need help to optmize the SQL logic in one of my functions. Please, note that I am not able to use store procedure.
Here is my table. It will be initialized using #MainTable that contains a lot of records.
DECLARE TABLE #ResultTable
(
ResultValue INT
)
These are tables that stores some parameters - they can be emty too.
DECLARE TABLE #ParameterOne (ParameterOne INT)
DECLARE TABLE #ParameterTwo (ParameterOne NVARCHAR(100))
...
DECLARE TABLE #ParameterN(ParameterN TINYINT)
Now, I need to join a lot of tables to my #MainTable in order to select from it only some of its records.
The selected records depend on the information stored in the parameters table.
So, my current solution is:
INSERT INTO ResultTable(ResultValue)
SELECT ResultValue
FROM MainTable M
INNER JOIN #MainOne MO
ON M.ID=MO.ID
....
INNER JOIN #MainN MN
ON M.IDN=MN.ID
WHERE (EXISTS (SELECT 1 FROM #ParameterOne WHERE ParameterOne=MO.ID) OR NOT EXISTS (SELECT 1 FROM #ParameterOne))
AND
...
AND
(EXISTS (SELECT 1 FROM #ParameterN WHERE ParameterN=MN.Name) OR NOT EXISTS (SELECT 1 FROM #ParameterN ))
So, the idea is to add the records only if they match the current criteria from the parameters tables.
Because I am not able to use procedure to build dynamic query I am using the WHERE clause with combinations of EXISTS and NOT EXISTS for each parameter table.
The problem is that it works slower when I am adding more and more parameters table. Is there an other way to do this without using a lot of IF/ELSE statements checking what parameter table has records - it will make the function a lot bigger and difficult for read.
And ideas and advices are welcomed.
Good question.
Try the following one:
INSERT INTO ResultTable(ResultValue)
SELECT ResultValue
FROM MainTable M
INNER JOIN (SELECT * FROM #MainOne WHERE (EXISTS (SELECT 1 FROM #ParameterOne WHERE ParameterOne=#MainOne.ID) OR NOT EXISTS (SELECT 1 FROM #ParameterOne))) MO
ON M.ID=MO.ID
....
INNER JOIN (SELECT * FROM #MainN WHERE (EXISTS (SELECT 1 FROM #ParameterN WHERE ParameterOne=#MainN.Name OR NOT EXISTS (SELECT 1 FROM #ParameterN))) MO
ON M.IDN=MN.ID
Advantages:
Result of the JOIN is more quickly, because it does not process all data (it is already filtered)
It looks more simple for adjusting