I have three tables and a complicated SQL query to extract data from these tables. To put it in context here is some sample data:
Table 1 aka Training (T1)
id
Name
1
Training 1
2
Training 2
Table 2 aka Roles (T2)
id
Name
1
Role 1
2
Role 2
3
Role 3
Table 3 aka TrainingRoles(T3)
id
idTraining
IdRoles
1
1
1
2
1
2
3
1
3
4
2
2
5
2
3
So far there are 2 tables and a "detail table (t3)"
so what I need to do is get this result:
Result Table
id
Training Name
Roles
1
Training 1
Role1, Role 2, Role 3,
2
Training 2
Role 2, Role 3,
I tried to do something like this but it gives me separated date even when I use Group By
select t1.Name,
(select string_agg(t2.Name, ',')
from T2 Td2
where Td2.Id = t3.IdCargo) AS Role
from
t1 t1
left join T3 t3 on t1.Id = t3.IdTraining
left join T2 t2 on t3.IdRoles = t2.id
group by t1.Name, t3.IdRole
I'm using left join so I can get Trainings that don't have a role linked
Ok so I tried this thx to #Larnu and got it solved:
select t1.Name,
string_agg(t2.Name, ',') as Role
from
t1 t1
left join T3 t3 on t1.Id = t3.IdTraining
left join T2 t2 on t3.IdRoles = t2.id
group by t1.Name
Related
I have two tables identical to each other like below
table 1
col1
1
2
3
4
5
table 2
col1
1
2
3
4
5
is there way to write a SQL query to join every row of table 1 to every row of table 2?
Do you want a Cartesian product? If so, use cross join:
select t1.col1, t2.col2
from table1 t1 cross join
table2 t2;
It sounds like an inner join as they are identical tables:
select t1.col, t2.col, ...
from table t1
inner join t2 on t1.col = t2.col
I'm not sure how to explain what I need but here's the data first:
Table 1
District
-1
3
2
1
3
Table 2
ID ID_Name
1 Main 1
2 Main 2
3 Main 3
How do I join the tables so that it looks like this?
District
-1
Main 3
Main 2
Main 1
Main 3
I'm assuming the second column is named Name for this, but you can do it with a COALESCE and a LEFT JOIN:
Select Coalesce(T2.Name, Str(T1.District)) As District
From Table1 T1
Left Join Table2 T2 On T1.District = T2.Id
assuming table 2 have
Table 2
ID col2
1 Main 1
2 Main 2
3 Main 3
you could use a left join
select table1.Distric, table2.col2
from table1
left join table2 on table1.dictrict = t2.ID
order by table2 col2
You can use left join:
Select coalesce(t2.col, t1.District) from table1 t1
left join table2 t2 on t1.District = t2.Id
Why does this query produce duplicates in some scenarios?
Table_1
ID
1
2
3
Table_2
ID
1
2
4
Table_3
ID
1
3
4
Query:
SELECT COALESCE(Table_1.ID, Table_2.ID, Table_3.ID)
FROM Table_1
FULL OUTER JOIN TABLE_2
ON Table1.ID=Table_2.ID
FULL OUTER JOIN TABLE_3
ON Table1.ID=Table3.ID;
Result:
1
2
3
4
4
The query duplicates all values where T1 is null and T2/T3 share the same value. Duplicates are removed for any other combination.
This is a little bit hard to explain. If you show the other ids, you will see the full range of what happens:
"coalesce" "id1" "id2" "id3"
1 1 1 1
2 2 2 .
3 3 . 3
4 . 4 .
4 . . 4
You can see the results here.
So, You get one row because t1 & t2 create a row with t2.id = 4 and t1.id = null when they don't match. Then, you get the same thing when t3.id = 4. The comparison is to t1.id -- so you get another row. There is no comparison to t2.id.
I suspect that you intend logic more like this:
select coalesce(t1.id, t2.id, t3.id)
from t1 full join
t2
using (id) full join
t3
using (id);
Here is the SQL Fiddle.
in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.
I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)