SQL Server insert left Join - sql

I have two tables:
TABLE1 TABLE2
COLUMN1 COLUMN2 COLUMN1 COLUMN2
--------------- ---------------
John 56 45 A
Bob 45 45 B
Eva 68 68 C
Alex 56 47 D
Android 48 45 L
Mum 68 68 C
… … 56 Q
… ...
And I need add column to one table with fact I will insert into new column value from other table using function join
COLUMN1 COLUMN2 COLUMN3
-----------------------
John 56 Q
Bob 45 B
Bob 45 A
Bob 45 L
Alex 56 Q
Eva 68 C
Android 48 NULL
Mum 68 C
… … …
ALTER TABLE [dbo].[Table1]
ADD Column3 NVARCHAR(255);
INSERT INTO [dbo].[Table1] (column3)
SELECT table2.column2
FROM [dbo].[Table2]
LEFT JOIN [dbo].[table1] ON table1.column2 = table2.column1
But I am getting
COLUMN1 COLUMN2 COLUMN3
------------------------
John 56
Bob 45
Eva 68
NULL NULL A
NULL NULL D
NULL NULL C
… … …
Can you help me to fix my insert?

I don't see what left join has to do with this:
UPDATE t1
SET column3 = t2.column2
FROM [dbo].[Table1] t1 JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
The values that are not found in Table2 will be set to NULL.
EDIT:
Hold on. You are inserting rows as well as values into columns. You seem to want to accomplish this query:
SELECT t1.column1, t1.column2, t2.column3
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
My recommendation would be to put this into a new table, not Table1:
SELECT t1.column1, t1.column2, t2.column3
INTO [dbo].[Table3]
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
If you really want to replace the data in Table1, then use an intermediate table:
SELECT t1.column1, t1.column2, t2.column3
INTO #temp
FROM [dbo].[Table1] t1 LEFT JOIN
[dbo].[Table2] t2
ON t1.column2 = t2.column1;
TRUNCATE TABLE dbo.Table1;
INSERT INTO dbo.Table1 (column1, column2, column3)
SELECT column1, column2, column3
FROM #temp;

Seems to me like you need an update, not an insert... Try this instead:
UPDATE t1
SET column3 = t2.column2
FROM dbo.Table2 t2
LEFT JOIN dbo.Table1 t1 ON t1.column2 = t2.column1

Related

if conditions inside where sql

I'm trying this logic not sure what am missing in this case
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where t1.column1 between 1 and 10 if t2.column2='value1'
and t1.column1 between 11 and 20 if t2.column2='value2'
You could use a case when aproach
CREATE tABLE table2(column1 int, column2 varchar(10))
INSERT INTO table2 VALUEs(1,'value1'),(2,'value1'),(13,'value2'),(44,'value2')
CREATE tABLE table1(column1 int)
INSERT INTO table1 VALUES (1),(2),(13),(44)
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where
CASE WHEN t1.column1 between 1 and 10 AND t2.column2 like 'value1' THEN TRUE
WHEN t1.column1 between 11 and 20 AND t2.column2 like 'value2' THEN TRUE
ELSE FALSE END
column1 | column1 | column2
------: | ------: | :------
1 | 1 | value1
2 | 2 | value1
13 | 13 | value2
db<>fiddle here
I assume you've left out key details, like the ON for the JOIN and the wildcard for the LIKE, in the interest of simplicity.
select *
from table1 t1
join table2 t2
where (t1.column1 between 1 and 10 and t2.column2 like 'value1')
or (t1.column1 between 11 and 20 and t2.column2 like 'value2')

Matching ID if more than one in column then nothing

I have table1 and table2
table1
nameID column1 column2 column3
Joe 10 10 99
table2
nameID column1 cost
Joe 99 100
Joe 10 100
Joe 30 200
My goal to have column3 from table1 match with column1 from table2.
IF table2 have 2 or more records even if table1 column3 match with table2 column1 (99) and expect nothing output.
IF table 2 have only one record on column1 (99) match with table1 column3 (99) then expect result
nameID column3 cost
Joe 99 100
I have tried
select t1.name, t1.column3
from table1 t1
join table2 t1 on t1.nameID = t2.nameID
where
t1.column3 = t2.column1
and t1.column1 <> t2.column1
and t1.column2 <> t2.column1
Not sure how to make it works. Thank you.
WITH CTE_X
AS (
SELECT count(*) AS countX
FROM table2 t2
)
SELECT table1.nameId
,table1.column3
FROM table1
INNER JOIN table2 ON table1.column3 = table2.column1
JOIN CTE_X ON 1 = 1
WHERE CTE_X.countX = 1
Please see this

How to join three tables with to avoid invalid identifier error

Having three tables as follows
Table 1
c_id User_id code
-------------------------
001 UI1 AB01
002 UI2 XD01
003 UI3 AD01
004 UI4 OP01
005 UI5 QW01
Table 2
id c_id brn_code sts
-----------------------------------
1 004 90E1 Y
2 005 91E3 Y
3 001 91S4 Y
4 003 93S6 Y
5 002 99S7 Y
Table 3
Key brn_code
--------------------
1 91S4
2 90D1
3 90Z5
4 93S6
5 96W4
Need Output like below
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '90D1', 'Y');
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '90Z5', 'Y');
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '96W4', 'Y');
Wrote query as
SELECT 'INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('||t1.c_id||', '||t3.brn_code||', '||''''||'Y'||''''||');'
FROM Table 1 t1 JOIN Table 2 t2
ON t1.c_id = t2.c_id JOIN
(SELECT brn_code FROM Table 3 t3
MINUS
(SELECT brn_code FROM Table 2 t2
WHERE t2.c_id IN (SELECT t1.c_id FROM Table 1 t1 WHERE t1.User_id = 'UI4' AND t1.code = 'OP01')
AND t2.sts = 'Y')) ON t2.brn_code = t3.brn_code;
For above query i returns error as invalid identifier. I don't know why this error came.
How to get output as above.
If you really wrote Table 1 t1 etc., that's certainly wrong. Should probably be something like Table1 if your tables are called as suggested in your first line.
You have problem with names of the table as already mentioned in the other answer.
Also, you can simplify your query by using EXISTS as follows:
SELECT 'INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('||t1.c_id||', '||t3.brn_code||', '||''''||'Y'||''''||');'
FROM Table1 t1 JOIN Table2 t2 ON t1.c_id = t2.c_id
JOIN table3 t3 on t2.brn_code = t3.brn_code
Where not exists (
Select 1 from table2 t22 join table1 t11 on t11.c_id = t22.c_id
Where t11.User_id = 'UI4' AND t11.code = 'OP01' AND t22.sts = 'Y' And t3.brn_code = t22.brn_code);

Insert SQL data into table by copying values from the same table

I have the following tables and trying to insert data into table T2 by copying from the same table if the name is Similar.
For Example, If T2 Has Name Red Then Copy ID and Name from T1 along with T2 Field.
TABLE T1
ID| NAME
5 RED T
6 BLUE T
TABLE T2
ID| NAME| FIELD 1| FIELD 2|FIELD 3
1 RED 17 20 23
2 RED 10 15 9
3 BLUE 7 8 3
EXPECTED OUTCOME:
TABLE T3
ID| NAME| FIELD 1| FIELD 2|FIELD 3
1 RED 17 20 23
1 RED 10 15 9
5 RED T 17 20 23
5 RED T 10 15 9
3 BLUE T 7 8 3
6 BLUE T 7 8 3
Here is what I have tried:
Insert into T1
Select ID, Name, Field1, Field2, Field3 From T2 a
Left Join T1 b
On a.Name like b.name
INSERT INTO T2 ([NAME], FIELD1, FIELD2, FIELD3)
SELECT T1.NAME, FIELD1, FIELD2, FIELD3
FROM T2 LEFT JOIN T1 ON T1.NAME LIKE CONCAT("%", T2.NAME ,"%")
You mentioned inserting into T2 but results displayed T3 so I went with T3 for now. If you really only want the records that weren't already in T2 then you can omit the UNION to the second part. Since you said that 'containing' is the same as 'similar' for your purposes:
CREATE TABLE T1
(
ID INT,
NAME VARCHAR(20)
);
INSERT INTO T1 VALUES (5, 'RED T');
INSERT INTO T1 VALUES (6, 'BLUE T');
CREATE TABLE T2
(
ID INT,
NAME VARCHAR(20),
FIELD1 INT,
FIELD2 INT,
FIELD3 INT
);
INSERT INTO T2 VALUES (1,'RED',17,20,23);
INSERT INTO T2 VALUES (2,'RED',10,15,9);
INSERT INTO T2 VALUES (3,'BLUE',7,8,3);
INSERT INTO T3(ID, NAME, FIELD1, FIELD2, FIELD3)
SELECT T1.ID, T1.NAME, T2.FIELD1, T2.FIELD2, T2.FIELD3
FROM T1 INNER JOIN T2 ON T1.NAME LIKE '%'||T2.NAME||'%'
UNION
SELECT T2.ID, T2.NAME, T2.FIELD1, T2.FIELD2, T2.FIELD3
FROM T2
ORDER BY ID
I think your problem is due the the fact that 'RED T' is not like 'RED'
You could try
Insert into T1
Select ID, Name, Field1, Field2, Field3 From T2 a
Left Join T1 b
On a.Name like ('%' + b.name + '%')
...But this is not generic or at least it could not match all the cases you would expect.
As others said you should specify more clearly what you mean by "similar".
Below would be the possible query
Insert into T1 (ID, Name, Field1, Field2, Field3)
Select t.ID, t.Name, t2.Field1, t2.Field2, t2.Field3
From T1 t Left outer join T2 t2 on
substring(t.Name,1,CHARINDEX(' ', t.Name))=t2.Name
Apologies for possible syntax mismatch as I didn't execute it and entered the solution via phone
The solution I have proposed is based on the assumption that in T1 the matching colour should be the first part then a space and finally last part

display max on one column but display multiple from multiple tables

how would i write a query to output the max column3 records for each corresponding record in column 1:
SELECT t1.column1, t1.column2, MAX(t2.column3) as MAXcolumn3
FROM table1 t1
LEFT JOIN table1 t2
ON t1.column2 = t2.column2
Group by t1.column1, t1.column2
RAW OUTPUT
column1 column2 column3
a aa 33
a ab 02
a ac NULL
b ba 11
b bb 00
c ca NULL
c cb 00
d da NULL
DESIRED OUTPUT
column1 column2 column3
a aa 33
a ab 33
a ac 33
b ba 11
b bb 11
c ca 00
c cb 00
d da NULL
Join table1 to itself on column1 to get all sibling rows, then left join to table2:
SELECT t1.column1, t1.column2, MAX(t3.column3) as MAXcolumn3
FROM table1 t1
JOIN table1 t2 ON t2.column1 = t1.column1
LEFT JOIN table2 t3 ON t3.column2 = t2.column2
GROUP BY t1.column1, t1.column2
See SQLFiddle
You might try using the aggregate function MAX() as a window function:
SELECT column1, column2, MAX(column3) OVER ( PARTITION BY column1 ) AS MAXcolumn3
FROM table1;
I don't know the precise schema of your table so I don't know if the above would return duplicates.
If you have two tables, then you might take your original query and do something similar with a subquery or CTE:
WITH cte AS (
SELECT t1.column1, t1.column2, MAX(t2.column3) as MAXcolumn3
FROM table1 t1 LEFT JOIN table2 t2 -- supposed to be table1?
ON t1.column2 = t2.column2
GROUP BY t1.column1, t1.column2
)
SELECT column1, column2, MAX(MAXcolumn3) OVER ( PARTITION BY column1 ) AS MAXcolumn3
FROM cte;
select tt.*
from (select t1.*,
row_number() over (partition by t1.column1 , t2.column2
order by t1.column1 desc) as column3
from table t1
LEFT JOIN table1 t2
ON t1.column2 = t2.column2
Group by t1.column1, t1.column2
) tt
where tt.column3 = 1;