PostgreSQL - Joining a table to itself - sql

Does PostgreSQL support self joining or is there another way to solve this?
For example, lets say I have a single table (table a) with the following columns:
id name supid
------------------------
1 a 2
2 b 3
3 c 4
4 d 5
.. .. ..
Is there a way to output the data in the following format?
id name sup name
-------------------------
1 a b
2 b c
3 c d
4 d ..
.. .. ..

How about a simple JOIN?
SELECT a.id,a.name,b.name "sup name"
FROM tablea a
JOIN tablea b
ON a.supid = b.id
An SQLfiddle to test with.

Related

postgresql Join two table without relation

lets say i have table like this
Table A:
column_a
1
2
3
Table B:
column_b
a
b
c
and i want to have result like this:
column_a column_b
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
does it possible to join two table without relation like this using query?
Yes, you're looking for cross join
select a.column_a, b.column_b
from a
cross join b
order by a.column_a, b.column_b;

SQL query - Cumulatively concatenate strings in consecutive rows

I'm a data analyst, so I write SQL queries to retrieve data from a database. I'm not sure what kind of SQL exactly, just assume the most standard (also not things like 'DECLARE #tbl', and no create functions etc.)
Here is my problem.
Given the following table:
name
number
letter
A
1
a
A
2
b
A
3
c
A
4
d
B
1
a
B
2
b
B
3
c
B
4
d
I want the following result: (concatenate letter cumulatively, order by number))
name
number
letter
result
A
1
a
a
A
2
b
a,b
A
3
c
a,b,c
A
4
d
a,b,c,d
B
1
a
a
B
2
b
a,b
B
3
c
a,b,c
B
4
d
a,b,c,d
Any help is highly appreciated. Thanks very much.
This answers the original version of the question which was tagged MySQL.
MySQL doesn't support group_concat() as a window function. So a subquery may be your best alternative:
select t.*,
(select group_concat(t2.letter order by t2.number)
from t t2
where t2.name = t.name and t2.number <= t.number
) as letters
from t;

Access - Join two tables on two fields, get all records from table A

OK, so in MS Access I am trying to join two tables on two fields (customer ID and product type), have table A use a sum of each product type, and have all the records from table A so I can know what is missing from table B.
In table A, there are multiple records for each customer for each product type by year. But in table B there is only one record per product type. And in table B not all the product types are there.
Example Tables:
Table A:
Cust ID ProdType Year Number
1 A 2014 5
1 A 2013 8
1 B 2014 3
2 A 2014 13
2 C 2014 2
3 B 2014 1
3 C 2014 4
Table B:
Number
Cust ID ProdType Arrived
1 A 5
2 A 13
2 C 2
3 B 1
3 C 2
Final Result should look like:
Sum of Number
Cust ID ProdType Number Arrived
1 A 13 5
1 B 3
2 A 13 13
2 C 2 2
3 A 1 1
3 C 4 2
Try this,
MySQL Syntax
select a.cust_id, a.prodtype, sum(a.number), b.arrived
from table_a a left join table_b b on a.cust_id=b.cust_id and a.prodtype=b.prodtype
group by a.cust_id, a.prodtype
Here is DEMO (MySQL)
Ms-Access
select a.cust_id, a.prodtype, sum(a.number), b.arrived
from table_a a left join table_b b
on a.cust_id=b.cust_id and
on a.prodtype=b.prodtype
group by a.cust_id, a.prodtype

Oracle: Joins two tables to duplicate rows for 2 table

I have 2 tables like below:
Table 1
---------
1
2
3
Table 2
--------
A
B
C
How do i join to get an output like below:
Output
---------
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C
Use Cross join:
SELECT *
FROM Table1
CROSS JOIN Table2
You can change the order by replacing * with table fields.
Read more about Cross Join.
OR you could do this:
SELECT Table1.*,Table2.*
FROM Table2,Table1
Result:
ID NAME
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C
You want to do a CROSS JOIN and that will give you the Cartesian product of all the rows.
See http://en.m.wikipedia.org/wiki/Join_(SQL)
select * from table 1,table 2 .
For o/p like A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
just

Using multiple joins (e.g left join)

I would like to know what's the logic for multiple joins (for example below)
SELECT * FROM B returns 100 rows
SELECT B.* FROM B LEFT JOIN C ON B.ID = C.ID returns 120 rows
As I know using left join will returns any matching data from the left table which is B if data are found for both table. But how come when using left join, it returns more data than table B itself?
What am I do wrong or misunderstood here? Any guidance are very appreciated. Thanks in advance.
Let be table B:
id
----
1
2
3
Let be table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Any id from b is matched with ids from c, then id=2 will be matched twice. So a left join on id will return 4 rows even if base table B has 3 rows.
Now look at a more evil example:
Table B
id
----
1
2
2
3
4
table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Every id from b is matched with ids from c, then first id=2 will be matched twice and second id=2 will be matched twice so the result of
select b.id, c.name
from b left join c on (b.id = c.id)
will be
id name
------------
1 John
2 Mary
2 Mary
2 Anne
2 Anne
3 Stef
4 (null)
The id=4 is not matched but appears in the result because is a left join.
Look at the following example :
B = {1,2}
C = {(1,a),(1,b),(1,c),(1,d),(1,e)}
The result of B left join C will be :
1 | a
1 | b
1 | c
1 | d
1 | e
2 | null
The number of rows in the result is definitely larger than rows in B (2).
In general the number of rows in result of B left join C is bounded by B.size + C.size and not only by B.size as you think...
As per your query it do the join to B Table with C and B table is Left Table so it will display all the records of Left table in our case it is B and related from other Table in our Case it is C.