table: A
-----------
value
1
2
3
sub-query: B
-----------
value
2
I need (A - B).
The below query works when B is not empty. Output = (1,3) as expected.
SELECT * FROM A
JOIN B
ON (A.value != B.value)
However, when the sub-query B is empty, the JOIN does an intersection of A with an empty B, and the output is an empty result-set.
And if I use LEFT JOIN, it does not subtract the row containing value 2 from table A.
Is it possible to write a single query for (A - B), irrespective of whether B is empty or not.
SELECT A.*
FROM A
LEFT JOIN B
ON A.value = B.value
WHERE b.value IS NULL
Related
I have three tables.
Table A ###(Code, Value are combined primary key)
Code Value
1 | b
1 | c
3 | c
Table B
Value
b
Table C
Value
c
I would like to write a query of 'Code' from Table A.
The condition is that 'Code' should contain Value 'c'.
If 'Code' contains Value'b', this Code shouldn't be queried. (that's, Code 1 has value b and value c, so Code 1 needs to be excluded)
But I'm not available to do that query.
The expected outcome might be '3'
I want to use intersect but MySql doesn't contain this function yet.
So I tried some codes.
I'm sure that my codes have problems but I have no idea how to fix it.
SELECT DISTINCT A.*
FROM A B C
WHERE A.Value IN
(SELECT Value FROM B)
AND A.Value NOT IN
(SELECT Value FROM C);
Could you give me some tips on my questions?
the problem is that you are joining a, b and c. you almost got it.
select distinct a.value
from a
where a.value in (select value from b)
and a.value not in (select value from c)
or you can use join
select *
from a
inner join b where b.value = a.value
and not in (select value from c)
I have two datasets:
1)
Table A
id name
1 raju
2 ramu
2 ramu
3 rakesh
Table A
2)
Table B
id Status
1 Y
1 Y
2 N
2 N
2 Y
3 N
Table B
I want to perform a left outer join (Table A Left outer join Table B) in hive in such a way that while joining the two datasets on column 'id', if the Status column in table B has 'Y' at least once, then the resultant dataset will have the Status as 'Yes' like below:
Final Result:
id name Status
1 raju Yes
2 ramu Yes
2 ramu Yes
3 rakesh No
I do not want to increase the number of records in the result while performing the join. The result table should have only 4 records, not 5(increased
records based on the matching join condition).
How do I achieve this?
To meet this requirement, you need to reduce the row count of your table B to either one row or zero rows per row of table A.
Do that like this, getting one row for every id value with a status of Y.
SELECT DISTINCT id, Status
FROM B
WHERE Status = 'Y'
Then you can use the old LEFT JOIN ... IS NOT NULL trick to figure out which rows of A have matching rows of B.
This does it. (http://sqlfiddle.com/#!9/71d84b/1/0)
SELECT A.id, A.name,
CASE WHEN B.Status IS NOT NULL THEN 'Yes' ELSE 'No' END Status
FROM A
LEFT JOIN (
SELECT DISTINCT id, Status
FROM B
WHERE Status = 'Y'
) B ON A.id = B.id
I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
Table A
Name Value
John 1
Mary 2
Gary 3
Table B
Name Value
Jim 10
Jason 20
Mary 30
I want Name and Value from Table A, but overriding Value if exists in Table B. So my expected output would be:
John,1
Mary,30
Gary,3
I was trying something like:
SELECT A.Name, IF(EXISTS(B.Value),B.Value,A.Value) FROM Table A LEFT JOIN Table B on B.Name=A.Name
You can use the Mysql COALESCE function for this. It will return the first non-null argument in a list of parameters:
So your code would look something like:
SELECT A.Name,
coalesce(B.Value,A.Value)
FROM Table A LEFT JOIN Table B on B.Name=A.Name
So what would happens is that if there is a value in table B, that will be used, if not, it will revert to A.
See the docs: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
Try this instead:
SELECT
A.Name,
CASE WHEN b.Name IS NULL THEN a.Value ELSE b.value END AS value
FROM TableA AS a
LEFT JOIN TableB AS b on B.Name=A.Name;
SQL Fiddle Demo
Given the following 2 tables
table_a
id name num_one num_two
------------------------------------
1 Foo 5 10
2 Bar 4 -1
table_b
name table_a_id
--------------------
Fooa 1
Foob 1
Suppose I want to use ether num_one or num_two in a where clause depending on if another table has joined rows or not.
The best thing I can come up with is this:
SELECT a.* FROM table_a a
JOIN table_b b on b.table_a_id = a.id
WHERE if(count(b.*) > 0, a.num_one, a.num_two) > 0
group by a.id
Ideally it would check if 5 > 0 on the first row and -1 > 0 on the 2nd because the 2nd row as no joined rows from table B.
But it errors with invalid use of group by. I know about "having" but not sure how I could use it in this situation.
Any ideas? Thanks!!
This can be done with an IF statement, OR statement or a subquery. None of which will be very efficient on a large table.
The only real modification to your original statement was the use of NULL instead of count(*).
SELECT DISTINCT a.*
FROM table_a a
LEFT JOIN table_b b on b.table_a_id = a.id
WHERE (b.table_a_id is null AND a.num_one > 0)
OR (b.table_a_id is NOT NULL AND a.num_two > 0)