Postgresql tables intersect - sql

I have a two tables in my postgresql database.
Both tables have same columns.
How can i prefer values from tableA which are not null?
TableA
id | name
1 | val_a_1
2 | val_a_2
3 | (null)
TableB
id | name
1 | (null)
2 | val_b_2
3 | val_b_3
Result which i want to get:
id | name
1 | val_a_1
2 | val_a_2
3 | val_b_3
For now I've got it like this, but its more complicated, because there are lots of columns.
SELECT *
CASE
WHEN TableA.name is NULL or TableA.name = ''
THEN (SELECT TableB.name FROM TableB where TableB.id = 1)
ELSE TableA.name
END
AS name,
CASE
.
. another columns
.
END
Thanks

why not use COALESCE? Assuming all recordID in tableA is present on Table2
SELECT a.ID,
COALESCE(a.name, b.name) AS "Name"
FROM TableA a
INNER JOIN TableB b
ON a.ID = b.ID
SQLFIddle Demo
Conditional Expressions
The COALESCE function returns the first of its arguments (there can be more arguments) that is not null.
It's all about the COALESCE, not the join itself.

you can use a full outer join for the values which exist in tableA but not in tableB and vice versa:
select coalesce(tableA.ID, tableB.ID) as ID,
coalesce(tableA.Name, tableB.Name) as Name
from tableA full outer join tableB on tableA.ID = tableB.ID

Related

SQL query - select row basing on the joined table's criteria

I'm stuck within this simple scenario:
tableA
| ID | TableB_ID | Name |
tableA 1 ---> * tableB
tableB
| ID | Status_ID |
and I need to retrieve the Name column values from the tableA, whose contains rows in the tableB with Status_ID = 1 and Status_ID = 2 (two separate rows, it can be more with other values but it doesn't matter here)
Try this:
SELECT A.NAME
FROM TABLE_A AS A INNER JOIN TABLE_B AS B ON A.TABLEB_ID = B.ID
WHERE B.STATUS_ID IN (1, 2) -- OR OTHER VALUES

How do show a result table from two 'select'? SQL

I have 2 queries:
First query:
select TableB.name, count(TableB.name) from TableB group by TableB.name
result:
name | count
Jack | 2
Marry| 1
and
Second query:
select DISTINCT TableA.kName, TableA.Value from TableA inner join TableB
ON
TableA.kName=TableB.name
result:
kName | Value
Jack | 1
Marry | 3
But I need result table:
kName | Value | newColumn
Jack | 1 | 2
Marry | 3 | 1
where the newColumn is result of first query, How can to do it? help, please.
maybe:
select DISTINCT TableA.kName, TableA.value,
(select TableB.name, count(TableB.name) from TableB group by TableB.name)
AS
newColumn from TableA inner join TableB ON TableA.kName=TableB.name
but this is not work :(
Try this query
SELECT tableB.name, tableA.value, count(tableB.name) as newColum FROM tableB
JOIN tableA ON tableB.name = tableA.kname
GROUP BY tableB.name,tableA.value
select kname, value, "newColumn"
from
(
select name, count(name) as "newColumn"
from tb
group by name
) tb
inner join
(
select distinct kname, value
from ta
) ta ta.kname = tb.name

IF condition in INNER JOIN SQL Query

I have a problem in writing sql query. Here is the illustration about my problem.
I have 2 tables in the database as follows :
| Table A | | Table B |
| idTableA | | idTableB |
| idPriority | | idReference |
Those tables above are related. The idReference in Table B is referring to idPriority in Table A. BUT if the value of the idPriority is NULL, then the idReference should be referring to idTableA. Hope you guys get the illustration and can help me working with this problem. Thx in advance.
Double-join method, easy to read
select
B.idTableB,
isnull(A1.idPriority, A2.idTableA) as RefValue
from
TableB as B
left join TableA as A1 on A1.idPriority = B.idReference
left join TableA as A2 on A1.idTableA = B.idReference
Single-join method, two IsNulls()'s:
select
B.idTableB,
isnull(A1.idPriority, A1.idTableA) as RefValue
from
TableB as B
left join TableA as A1 on isnull(A1.idPriority, A1.idTableA) = B.idReference
would the following construct work for you?
select *
from tableA join tableB on (join condition)
where (idpriority is not null)
union
select *
from tableA join tableB on (join condition)
where (idpriority is null)
select *
from tableB b
join tableA a
on
a.idPriority = b.idReference
or
(a.idPriority IS NULL AND b.idReference = a.idTableA)

sql select all but nulls

I want to get values from two tables like in the following example:
Suppose we have this two tables:
TableA (with link to tableB):
Id | Id_TableB | Name
---------------------
1 | 1 | Ralf
2 | NULL | Marta
TableB:
Id | Color
---------------------
1 | Blue
2 | Red
I would like to get values for Color if there is a link, NULL otherwise:
Result:
Id | Name | Color
-----------------------------
1 | Ralf| Blue
2 | Marta| NULL
Any ideas on how to solve this?
You need a left outer join .
SELECT t1.Id, t1.Name, t2.Color
from TableA t1 left outer join TableB t2
on t1.Id_TableB = t2.Id;
Please check the link that I have provided above. Its a simplified tutorial
select
a.Id,
a.Name,
b.Color
from
a
left outer join
b
on
a.Id_TableB = b.Id
group by
b.Id
The group by b.Id is necessary if you have a one-to-many relationship a->b.
You need to use a LEFT OUTER JOIN, which includes rows even when there are NULLs. Something like this:
SELECT *
FROM TableA a
LEFT OUTER JOIN TableB b on a.ID_TableB = b.ID
More info here:
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Hope this helps,
John
just you have to use outer join for this....
select a.id,a.Name, b.Color from tableB as b
left join tableA as a on b.Id = a.id_tableB
and for this you can use in condition as well
you can try below query--
select a.id,a.Name, b.Color from tableB as b,tableA as a on b.Id(+) = a.id_tableB

SQL join problem

I want to retrieve all records from one table when there are no matches in the second table.
So it is kind of the opposite of an inner join.
You need a LEFT JOIN WHERE IS NULL query (aka outer join):
SELECT table1.*
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table2.id IS NULL
Or a NOT IN:
SELECT *
FROM table1
WHERE id NOT IN (SELECT id FROM table2)
You have three options:
Correlated sub-query.
SELECT * FROM TableA
WHERE NOT EXISTS
(SELECT * FROM TableB WHERE TableB.ID = TableA.ID)
Non-correlated sub-query.
SELECT * FROM TableA
WHERE ID NOT IN (SELECT ID FROM TableB)
OUTER JOIN with NULL elimination.
SELECT * FROM TableA
LEFT [OUTER] JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableB.ItsPrimaryKey IS NOT NULL
In the last example some DBMSes require the word OUTER, some permit it, and some do not allow it at all.
Depending on the DBMS, the various options might produce different execution plans and different performance. Select the one with good performance and that best expresses your intent.
It's an outer join:
SELECT *
FROM tableA AS a
LEFT JOIN tableB AS b USING(x)
Say you have:
tableA:
a | x
-----
1 | 1
3 | 3
table B:
b | x
-------
1 | 'a'
2 | 'b'
then the query above will give you
a | b | x
------------
1 | 'a' | 1
3 | NULL | 3
if you want
a | b | x
----------------
1 | 'a' | 1
NULL | 'b' | 2
3 | NULL | 3
you have to use FULL OUTER JOIN instead of LEFT JOIN.
EDIT: As Larry Lustig told me (and I think is correct after rereading the question) the OP does not want any rows from B. So the query should be:
SELECT a.*
FROM tableA AS a
LEFT JOIN tableB AS b USING(x)
WHERE b.x IS NULL
that will yield
a | x
-------
3 | 3
The "opposite" of inner join is called outer join.
Outer join. Technically possibly FULL OUTER JOIN ;)
Adding to the Solution of Johannes Weiß the solution to your other question about finding objects in table a without objects in table b:
SELECT *
FROM tableA AS a
LEFT JOIN tableB AS b
USING(x)
WHERE b.foo IS NULL
You are looking for LEFT JOIN or FULL OUTER JOIN
Not all DBMS support FULL though.
Simulation of it is possible with UNION, example in MySQL:
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
I believe you are looking for FULL OUTER JOIN (works with Oracle 9i+).
EDIT: didn't read your question well... LEFT JOIN if you only want NULL values for the second table