Postgresql query between 2 tables - postgresql

There are 3 fields in TABLE_A (id1,id2,value), 2 fields in TABLE_B(id,name). I want to get the name instead of id1/id2 according to id=id1/id=di2
select id1,id2,value from TABLE_A;
select name from TABLE_B where id=id1;
select name from TABLE_B where id=id2;
How to combine these three statements into one, and return the results like following:
name(id1) | name(id2) | value

Try this:
select c.name, b.name, a.value from TABLE_A a, TABLE_B b, TABLE_B c
where b.id = a.id1 and c.id = a.id2

Related

How to reference foreign table column when doing group by

TableA
id | b_id
TableB
id | name
select b_id, count(*) from TableA group by b_id order by count(*) asc
I am getting the result as
50 1
100 2
150 3
I want corresponding names to 50,100, 150 as my result
item50 1
item100 2
item150 3
You may add the name column to the GROUP BY and SELECT clauses:
SELECT
b.id,
b.name,
COUNT(a.b_id) AS cnt
FROM TableB b
LEFT JOIN TableA a
ON b.id = a.b_id
GROUP BY
b.id,
b.name
ORDER BY
COUNT(*);
select tableB.name||tableA.b_id:: character varying as b_id,count(*)
from tableA
inner join tableB on tableA.b_id = tableB.id
group by tableB.name,tableA.b_id

How do I join two tables with same column names keeping values from one the tables

I have these two tables:
Table1
-------
id|a|b|c|d|
1 |0|1|0|6|
and
Table2
-------
id|a|c|
1 |3|2|
How do I join these two tables keeping the values from table2 but also the columns from table1, so that the table would look like this afterwards:
TableJoined
-------
id|a|b|c|d|
1 |3|1|2|6|
Tried with
SELECT * FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
hoping that the first table mentioned would be the overrider
Or I guess you could do:
SELECT b.id, a.a, b.b, a.c, b.d FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
You can achieve that by qualifying the * with the table alias:
SELECT a.* FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
But you should never use * in a SELECT list except in ad-hoc queries. (The exception is count(*) which is OK to use).

Postgresql how to select values in the column from one table that are only available in another table?

I am using Postgresql and need to query two tables like this:
Table1
ID Bill
A 1
B 2
B 3
C 4
Table2
ID
A
B
I want a table with all the columns in Table1 but keeping only the records with IDs that are available in Table2 (A and B in this case). Also, Table2's ID is unique.
ID Bill
A 1
B 2
B 3
Which join I should use or if I can use WHERE statement?
Thanks!
SELECT Table1.*
FROM Table1
INNER JOIN Table2 USING (ID);
or
SELECT *
FROM Table1
WHERE ID IN (SELECT ID FROM Table2);
but the first one is better for performance reason.
SELECT *
FROM Table1
WHERE EXISTS (
SELECT 1 FROM Table2 WHERE Table2.ID = Table1.ID LIMIT 1
)

T-SQL: how to sort table rows based on 2 columns

I'm quite stuck with this problem for sometime now..
How do I sort column A depending on the contents of Column B?
I have this sample:
ID count columnA ColumnB
-----------------------------------
12 1 A B
13 2 C D
14 3 B C
I want to sort it like this:
ID count ColumnA ColumnB
-----------------------------------
12 1 A B
14 3 B C
13 2 C D
so I need to sort the rows if the previous row of ColumnB = the next row of ColumnA
I'm thinking a loop? but can't quite imagine how it will work...
I was thinking it will go like this (maybe)
SELECT
a.ID, a.ColumnA, a.ColumnB
FROM
TableA WITH a (NOLOCK)
LEFT JOIN
TableA b WITH (NOLOCK) ON a.ID = b.ID AND a.counts = b.counts
WHERE
a.columnB = b.ColumnA
the above code isn't working though and I was thinking more on the lines of...
DECLARE #counts int = 1
DECLARE #done int = 0
--WHILE #done = 0
BEGIN
SELECT
a.ID, a.ColumnA, a.ColumnB
FROM
TableA WITH a (NOLOCK)
LEFT JOIN
TableA b WITH (NOLOCK) ON a.ID = b.ID AND a.counts = #counts
WHERE
a.columnB = b.ColumnA
set #count = #count +1
END
If this was a C code, would be easier for me but T-SQL's syntax is making it a bit harder for a noobie like me.
Any help is greatly appreciated!
Edit: sample code
drop table tablea
create table TableA(
id int,
colA varchar(10),
colb varchar(10),
counts int
)
insert INTO TableA
(id, cola, colb, counts)
select 12, 'Bad', 'Cat', 3
insert INTO TableA
(id, cola, colb, counts)
select 13, 'Apple', 'Bad', 1
insert INTO TableA
(id, cola, colb, counts)
select 14, 'Cat', 'Dog', 2
select * FROM TableA
SELECT a.ID, a.ColA, a.ColB
FROM TableA a WITH (NOLOCK)
LEFT JOIN TableA b WITH (NOLOCK)
ON a.ID = b.ID
Where a.colB = b.ColA
ORDER BY a.ColA ASC
you just need to add ORDER BY clause
-- SELECT a.ID, a.ColumnA, a.ColumnB
-- FROM TableA WITH a (NOLOCK)
-- LEFT JOIN TableA b WITH (NOLOCK)
-- ON a.ID = b.ID
-- and a.counts = b.counts
-- Where a.columnB = b.ColumnA
ORDER BY a.ColumnA ASC
This is all you need. Sometimes you have to think simple
select * from table A
order by columnA asc

sql server 2005/2008 conditional join

Is there such thing like conditional join:
SELECT *
FROM TABLE1 A
IF (a=='TABLE2') THEN INNER JOIN TABLE2 B ON A.item_id=B.id
ELSE IF (a=='TABLE3') THEN INNER JOIN TABLE3 C ON A.item_id=C.id
While a is a field in TABLE1.
I like to use this in stored procedures without using dynamic sql (without writing query as string and EXEC(#query)).
EDIT: I can't write:
IF (a=='TABLE2) THEN queryA
ELSE IF (a=='TABLE3') THEN queryB
Because a is a field of TABLE1.
EDIT: Modified answer based on comment below:
You could try to get clever with some left joins. This will return more columns, so you'd probably want to be more discriminating than just SELECT *.
SELECT *
FROM TABLE1 A
LEFT JOIN TABLE2 B
ON A.item_id = B.id
AND A.a = 'TABLE2'
LEFT JOIN TABLE3 C
ON A.item_id = C.id
AND A.a = 'TABLE3'
WHERE (B.id IS NOT NULL AND A.a = 'TABLE2')
OR (C.id IS NOT NULL AND A.a = 'TABLE3')
Updated the query as requried:
SELECT * FROM
(
SELECT *
FROM TABLE1 A INNER JOIN TABLE2 B
ON A.a='TABLE2' --This will eleminate the table rows if the value of A.a is not 'TABLE2'
AND A.item_id=B.id) A,
(SELECT * FROM
INNER JOIN TABLE3 C
ON A.a='TABLE3' --This will eleminate the table rows if the value of A.a is not 'TABLE3'
AND A.item_id=C.id
) B
) a