How can I add additional information to joined rows?
SELECT column1, column2
FROM table1
LEFT JOIN table2
ON column1 = col1_table2 //The row which matches with this join should have additional information e.g. "'joined' AS info
Thanks a lot for your help!
If I understand your question, this should work:
SELECT
t1.column1,
t1.column2,
CASE WHEN t2.col1_table2 IS NOT NULL THEN 'joined' END AS info
FROM table1 t1
LEFT JOIN table2 t2 ON t1.column1 = t2.col1_table2
try this:
SELECT column1,
column2,
CASE WHEN col1_table2 IS NOT NULL THEN 'joined' END AS additional_field
FROM table1
LEFT JOIN table2 ON column1 = col1_table2
Related
I have two tables in the database (myCustomTable1 and myCustomTable2) and from them I'm creating another two tables (Table1 and Table2) which are created during the run time. Now I need to take the rows which are inside of Table1, but are not in the Table2.
I found this question, which seems that contains the answer that I need, but I'm not able to implement this with my solution, because as said, tables which I need to "antijoin" are generated during run-time.
Both of my (run-time generated) tables have the format:
-----------------------
| Column1 | Column2 |
-----------------------
| | |
-----------------------
| | |
-----------------------
Here is the code that I have.
SELECT Table1.* FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT Table2.* FROM (
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
) as Table2
)
ON Table1.Column1 = Table2.Column1
AND Table1.Column2 = Table2.Column2
Now I'm aware that this solution is not working, because when trying to join, I'm trying to use Table2, which is not available in the global scope, but I can not find any appropriate solution.
Maybe using NOT EXISTS, LEFT OUTER JOIN or NOT IN is also an option, but in every try, I faced with the same issue where the scope of the defined tables is a problem.
I find it a lot easier to separate your sets in CTEs:
;WITH Table1 AS
(
SELECT
myCustomTable1.Column1,
myCustomTable1.Column2
FROM
myCustomTable1
),
Table2 AS
(
SELECT
myCustomTable2.Column1,
myCustomTable2.Column2
FROM
myCustomTable2
)
SELECT *
FROM Table1 as t1
WHERE
NOT EXISTS (SELECT 1
FROM Table2 as t2
WHERE t1.Column1 = t2.Column1
AND t1.Column2 = t2.Column2);
You missed a from table in the inner suquery and a table alias in the outer left join try use
SELECT Table1.* FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT Table2.* FROM (
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
FROM myCustomTable2
) as Table2
) as table 3
ON Table1.Column1 = Table3.Column1
AND Table1.Column2 = Table3.Column2
You can select the records from Table1 excluding the matches using not exists in the where:
select *
from Table1
where
not exists(select 1
from Table2
where Table1.Column1 = Table2.Column1 and Table1.Column2 = Table2.Column2);
If you meant the aliases Table1 and Table2 as runtime created wouldn't this work?:
SELECT Table1.*
FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
) as Table2
ON Table1.Column1 = Table2.Column1
AND Table1.Column2 = Table2.Column2
where Table2.Column1 is null;
Or (better IMHO):
SELECT Column1,
Column2
from myCustomTable1 t1
where not exists
(
SELECT * from myCustomTable2 t2 where
t1.Column1 = t2.Column1 and t1.Column2 = t2.Column2
);
I'm trying to return all columns in a database where certain rows within certain columns have been eliminate. Is there any possible way to do this? I tried using code like this but I'm unsure what I am missing to make this work
Select * from table1
where (select column1 from table1
minus select column1 from table2);
You can do this with a WHERE NOT EXISTS:
Select T1.*
From Table1 T1
Where Not Exists
(
Select *
From Table2 T2
Where T2.Column1 = T1.Column1
)
Alternatively, you could use a LEFT OUTER JOIN:
Select T1.*
From Table1 T1
Left Join Table2 T2 On T2.Column1 = T1.Column1
Where T2.Column1 Is Null
Or even a WHERE NOT IN:
Select *
From Table1
Where Column1 Not In
(
Select Column1
From Table2
)
I would recommend the WHERE NOT EXISTS approach, but to fix the query you have in the question, you just need to add a WHERE IN:
Select *
From Table1
Where Column1 In
(
Select Column1
From Table1
Minus
Select Column1
From Table2
)
Try this:
select * from table1 where column1 in
(select column1 from table1
minus
select column1 from table2);
I have a data in column 1 of table 1 with multiple rows, i have to search all the data one by one to all the column of table 2 and wherever match comes, i want all the records from table 2 using SQL.
Really would be great help if i get the solution.
Something like:
select t2.*
from table2 t2
join (select distinct column1 from table1) t1
on t1.column1 in (t2.col1, t2.col2, t2.col3 ...)
The sub-select, i.e. (select distinct column1 from table1), is used to find the values to search for in table2's columns.
JOIN with these values, where all table2's columns are searched for each value.
Think you are talking about INNER JOIN like
select t2.* from table2 t2 inner join table1 t1
on t1.column1 = t2.column1
or t1.column1 = t2.column2
or t1.column1 = t2.column3
....
I feel like there should be a simpler way to write this, since both SELECT statements are pulling from the same table:
SELECT column1
FROM table1
WHERE column2 = 'VIRTUAL'
AND column3 IN
(SELECT column3
FROM table1
WHERE column1 = 'ELEC-035A');
Here is the same query but i replaced the subquery by an INNER JOIN clause:
SELECT T1.column1
FROM table1 T1
INNER JOIN table1 T2 ON T2.column3 = T1.column3
AND T2.column1 = 'ELEC-035A'
WHERE T1.column2 = 'VIRTUAL'
GROUP BY T1.column1
Hope this will help.
Table 1
Column1 Column2 Column 3
A Null 12
B Null 15
C 0 15
Table 2
Column2 Column3
0 15
0 12
I have table 1 and Table 2 , Here I'm passing table 2 parameters to table 1 which should return the column1 but it should match with Null values like the below scenario
If I pass (0, 15) to table 1 then it should return 'C' not 'B'.
If I Pass (0,12) to table 1 then it should return 'A'
Anytime it should return one value not multiple vales.
Could you please help me with this logic ?
You could do this as a union:
select Column1 from
(
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2=Table2.Column2 and Table1.Column3=Table2.Column3
UNION
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2 is NULL and Table1.Column3=Table2.Column3
) as Unioned
ORDER BY Column2 NULLS LAST
LIMIT 1;
The UNION discards duplicates, but you could always wrap the entire statement in another SELECT if you explicitly need to tell it to return at most one value.
select *
from table2 t
left outer join table1 t1 on t.column2=t1.column2 and t.column3=t1.column3
left outer join table1 t2 on t2.column2 is null and t.column3=t2.column3
where t1.column2 is not null or t2.column3 is not null
SQL Server
WITH T1
as
(SELECT COLUMN1,COLUMN2,
case when COLUMN2 is null then 0 else COLUMN2 end AS TRANSFORMED_COLUMN2,
row_number() OVER (partition by isnull(column3,2) ORDER BY isnull(COLUMN2,2) asc) AS rnk,
COLUMN3
FROM TABLE1)
SELECT
T1.COLUMN1,
T2.COLUMN2,
T2.COLUMN3
FROM
T1
INNER JOIN
(SELECT COLUMN2,COLUMN3 FROM TABLE2) T2
ON T1.TRANSFORMED_COLUMN2 = T2.COLUMN2
AND T1.COLUMN3=T2.COLUMN3
where T1.rnk=1
By using CTE, you will running through the tables just once.
SELECT t1.* FROM table1 t1
INNER JOIN table2 t2
ON NVL(t1.column2,0) = t2.column2 and t1.column3 = t2.column3