Can anyone help to get this result in 1 sql statement?
I have 2 tables with the 2nd table having dominance over the first -- as follows:
TableA
Code | Quantity
1 5
3 5
4 5
TableB
Code | Quantity
1 5
2 1
4 6
Query Result
Code | Quantity
1 5 - code exists in both - return record from TableB
2 1 - distinct only in TableB
3 5 - distinct only in TableA
4 6 - code exists in both - return record from TableB
Appreciate your help. Thanks!
A FULL JOIN will do:
SELECT COALESCE(t1.Code, t2.Code) AS Code,
COALESCE(t2.Quantity, t1.Quantity) AS Quantity
FROM TableA AS t1
FULL OUTER JOIN TableB AS t2 ON t1.code = t2.code
COALESCE(t2.Quantity, t1.Quantity) essentially implements your requirement for 'dominance' of TableB over TableA: Quantity is picked up from TableA only in case there is no matching record in TableB.
For an explanation on how a FULL JOIN works you can have a look here:
FULL OUTER JOIN, includes all rows from both tables, regardless
of whether or not the other table has a matching value.
Related
Could anyone help with how to take a single column:
ID
1
2
3
4
and show all possible distinct pairings in 2 columns:
ID ID
1 2
1 3
1 4
2 3
2 4
3 4
You can join the column on itself and arbitrarily decide that the left hand side will always be smaller than the right hand side (as the example shows):
SELECT t1.col, t2.col
FROM mytable t1
JOIN mytable t2 ON t1.col < t2.col
do self join
select t.id,t2.id from t t1 join t t2 on t.id<t1.id
A Simple Cartesian product should do
select distinct a.id as id_a, b.id as id_b
from test a, test b
where a.id<b.id;
I have the following tables:
T1 T2 Desired result
CA CB CA CC CA CB
1 2 1 3 1 4
1 4 1 2 2 1
1 3 1 5 2 3
2 1 2 4
2 3
3 6
3 1
4 ...
I need to make a join between T1 and T2 (using column CA) and return only those rows which the values in CB do not exists in T2.CC
A simple way to achieve that is using the following query:
SELECT T1.* FROM T1 INNER JOIN T2 ON t1.CA = t2.CA AND
t1.CB NOT IN (SELECT CC FROM T2 WHERE T2.CA = T1.CA)
I think the previous query is not very efficient. For that reason I am looking for something better
Any help will be appreciated
Generally, a more efficient means of achieving this sort of result is finding records which fail a simpler join condition. Those can be found by doing an outer join and checking for null, as follows:
select t1.ca, t1.cb
from t1 left outer join t2 on t1.ca=t2.ca and t1.cb=t2.cc
where t2.ca is null;
I think you just want not exists:
select t1.*
from t1
where not exists (select 1
from t2
where t2.ca = t1.ca and t2.cb = t1.cb
);
For performance, you want an index on t2(ca, cb).
I have two tables
Table 1:
color_id | label
---------|------
2 | 0
3 | 0
2 | 0
1 | 0
4 | 1
4 | 1
5 | 0
Table 2:
color_id
--------
2
1
4
I want a query that just gives me results for color_ids that are present in Table 2
So, I wrote:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.color_id = table2.color_id
WHERE table2.color_id IS NOT NULL
however, the above gives duplicates as well. Meaning I get
2 | 0
2 | 0
1 | 0
4 | 1
4 | 1
I don't want the duplicates in the results. I just want unique items.
I want a query that just gives me results for color_ids that are present in Table 2
So, you shouldn't use LEFT JOIN in this case:
SELECT DISTINCT a.color_id, a.label
FROM table_1 a JOIN table_2 b
ON a.color_id = b.color_id
When you add the keyword Left (or Right or full) to a join specifier, you make the join an outer join. This means that you get all the rows from one side of the join, and only those rows from the other side that match. If you only want the rows from table_1 where the color_id is in table_2, then you want an inner join, specified by writing inner join or just writing join, without a left, right or full.
to eliminate duplicates, add the keyword distinct to the select clause...
Select distinct color_id, label
From table1 t1
join table2 t2
on t2.color_id = t1.color_id
Try the below query
SELECT DISTINCT color_id
FROM table_1 T1
WHERE EXISTS (SELECT 1 FROM table_2 T2 where T1.color_id = T2.color_id)
Use an inner join and a distinct clause:
SELECT DISTINCT table1.color_id, table1.label
FROM table1
INNER JOIN table2
ON table1.color_id = table2.color_id
What you are looking for is an INNER JOIN combined with a
SELECT distinct table1.color_id, tabl1.label
FROM table1
INNER JOIN table2 ON table1.color_id = table2.color_id
This eliminates any item in table1 not present in table 2 and duplicated rows.
the reason of that is you used Left Join, which will keep all obs in table1.
Try this:
SELECT table1.* FROM table1 Inner JOIN table2 ON table1.color_id = table2.color_id
this should works as actually all table2 obs are in table1. To be more serious, if table2 has obs that are not in table1 and you do want to keep them, replace inner join with right join.
I am struggeling with a left join on a Raima database. I'd like to add an additional join condition, but in this case the join behaves like an inner join, thus I am losing some of the expected results.
Example:
TABLE_A
ID
-------
1
2
3
4
.
TABLE_B
A_ID | B
--------
1 | 1
2 | 1
2 | 2
3 | 2
Query
select * from TABLE_A left join TABLE_B
on TABLE_A.ID = TABLE_B.A_ID
and TABLE_B.B = 1
I am expecting the following result:
1 1 1
2 2 1
3 null null
4 null null
E.g. on an Oracle 11g I get the expected result, but on the Raima it shows me only the first two results. What is the problem here and how to fix it?
You need this
select * from A left join
(select * from B where B=1) bd
on A.ID = bd.A_ID
The query you gave will not give you expected result in oracle also. This will.
PS: Please use different names for table and column
I have a table that contains the following columns:
ID
Master ID
The Master ID can be shared between different rows with different IDs.
E.g.:
ID | Master ID
1 | 1
2 | 1
3 | 1
4 | 2
Knowing the ID I want to retrieve all the rows that share the same master ID
I managed to do it using this query:
Select *
FROM table t
LEFT JOIN table t2
ON t.MASTER_ID = t2.MASTER_ID
Where t.ID = '1'
Then I also tried using:
Select *
FROM table t
LEFT JOIN table t2
ON t.MASTER_ID = t2.MASTER_ID and t.ID = '1'
In that case, it was much slower. Can anyone explain why?
The queries are doing different things, the first you are saying:
1. give me all rows from `table` where `id = 1`
2. Also give me rows from t2 with a matching master ID
In the second you are saying
1. Give me all rows from `table`
2. Return rows from `t2` with a matching master ID and where `t1.ID = 1`
In a simple example you might have
ID Master_ID
------------------------
1 1
2 1
3 1
4 2
So your first query will return:
t1.ID t1.Master_ID t2.ID t2.Master_ID
--------------------------------------------
1 1 1 1
1 1 2 1
1 1 3 1
Your second query will return
t1.ID t1.Master_ID t2.ID t2.Master_ID
--------------------------------------------
1 1 1 1
1 1 2 1
1 1 3 1
2 1 NULL NULL
3 1 NULL NULL
4 2 NULL NULL
So basically in the first query you are returning a limited number of rows from your table, whereas in the second you return all rows, but only join to some of them.
If the t.ID = '1' condition is in the WHERE clause the t.ID='1' condition only has to be evaluated for the number of rows in t. If the t.ID='1' condition is put into the ON clause for the join it must be evaluated for all rows in t2. If there are a lot of rows in t2 this can significantly increase the run time of the query.
You shouldn't include t.ID = '1' in JOIN ON condition since it's not the joined table. condition on the table in FROM part should stay in WHERE clause; whereas condition belongs to joined table should be moved to join on clause so to get a proper outer join effect rather a inner join effect.
You don't need an OUTER JOIN. The reason is simple, your are joining the same table on the same column - there can't be a non-match!
The query to use is therefore
Select *
FROM T
INNER JOIN T t2
ON T.MasterID = t2.MasterID
Where t.ID = 1