Issue
I would like to write an SQL query for intersection between A and B unless B is empty set. For example:
Table A:
| col |
-------
| 1 |
| 2 |
-------
Table B:
| col |
-------
| 2 |
| 3 |
-------
Table B':
| col |
-------
-------
- For A and B, I expect
2(takeBwhich are also contained inA)- e.g.
SELECT * FROM A INTERSECT SELECT * FROM B;orSELECT A.* from A JOIN B ON a.col = b.col;
- e.g.
- For A and B', I expect
1and2(takeAas default ifBis empty)
Do you have any idea?
Solution
Edit : Modified for Postgres.
You can run the standard INETRSECT command using both sets to get the standard intersect results. In order to return the non-empty set when on set is empty, UNION the INTERSECT results, which will be null if a set is missing, against the non-empty set. There needs to be an empty check on the second step or all distinct values will be returned when both sets have values with no intersects {a,b,c}{x,y,z}
Schema (PostgreSQL v14)
CREATE TABLE SetA (SetValue INT);
CREATE TABLE SetB(SetValue INT);
INSERT INTO SetA VALUES(7),(3);
INSERT INTO SetB VALUES(2),(3),(1);
Query #1
SELECT
SetValue
FROM
(
SELECT SetValue FROM
(
SELECT SetValue FROM SetA
UNION
SELECT SetValue FROM SetB
)x
WHERE
NOT EXISTS(SELECT * FROM SetA) OR NOT EXISTS(SELECT * FROM SetB)
UNION
SELECT SetValue FROM SetA
INTERSECT
SELECT SetValue FROM SetB
)x;
| setvalue |
|---|
| 3 |
Answered By - Ross Bush Answer Checked By - Mary Flores (sql.easybugfix Volunteer)
Post a Comment
Post a Comment