Why JOIN LATERAL doesn't unpivot multiple fields - sql

I am very new to postgresql and would really appreciate any help. I am currently using this script (it works fine):
FROM dataset as d
LEFT JOIN metric m on d.datasetid=m.datasetid
LEFT JOIN value v on m.metricid=v.metricid
LEFT JOIN submetric_1 s1 ON v.submetric_1id=s1.submetric_1id
LEFT JOIN submetric_2 s2 ON v.submetric_2id=s2.submetric_2id
LEFT JOIN year y on v.valueid=y.valueid
LEFT JOIN quarter q on v.valueid=q.valueid
LEFT JOIN country c on v.valueid=c.valueid
LEFT JOIN region r on v.valueid=r.valueid
LEFT JOIN county co on v.valueid=co.valueid
LEFT JOIN ladistrict l on v.valueid=l.valueid
This script returns columns from SELECT clause as follows:
metric value […] country region county ladistrict
I need to unpivot four last geographical columns 'country','region','county' and 'ladistrict' to get a return as follows:
metric value […] Geography Geography name
Country Country name
Region Region name
County County name
Ladistrict Ladistrict name
I am trying LATERAL clause as follows:
FROM dataset as d
LEFT JOIN metric m on d.datasetid=m.datasetid
LEFT JOIN value v on m.metricid=v.metricid
LEFT JOIN submetric_1 s1 ON v.submetric_1id=s1.submetric_1id
LEFT JOIN submetric_2 s2 ON v.submetric_2id=s2.submetric_2id
LEFT JOIN year y on v.valueid=y.valueid
LEFT JOIN quarter q on v.valueid=q.valueid
LEFT JOIN LATERAL (VALUES
('Country',c.country)
,('Ladistrict', l.ladistrict)
,('Region', r.region)
, ('County',co.county))
s (Geography, geo_name)
And this one returns error. Please, any ideas how to make it work and get these fields unpivoted? Thanks

A left join needs a join condition. It could be as simple asON TRUE. However, it's easier to just change that left join lateral to a cross join lateral.

Related

Left join with multiple inner join

How can we make multiple left and inner join in SQL Server
Following query not returning all Employees because of Inner join
SELECT * from Employee E
LEFT JOIN Participants P on E.EmpID=P.EmpID
INNER JOIN HRDetails H on D. DeptID=H.DeptID
Left JOIN SalaryDetails S on S.participantID=P.participantID
You have used an alias D. which does not refer to any table, this may cause error.
But to answer your question I think inner join would reduce the number of rows if not all DeptID matches between two tables.

Sql join select all values that not availible joined table

is it posssible to select all rows from one table and some rows from other table using join,
here is the what i'm trying to do.
Select CT.COA_TypeId,CT.Code,CT.Types,SUM(GL.Amount) As Amount
from COA_Type CT
join ChartOfAccount CA on CT.COA_TypeId=CA.COA_Id
Join COA_Client CC on CA.COA_Id = CC.COA_Id
JOIN GeneralLedgerLine GL on CC.AccountId=GL.AccountId
Group BY CT.Code,CT.Types,CT.COA_TypeId
i want to select all CT.Types with amount, type rows that do not have Amount i want amount as null
Select CT.COA_TypeId,CT.Code,CT.Types,SUM(GL.Amount) As Amount
from COA_Type CT
INNER join ChartOfAccount CA on CT.COA_TypeId=CA.COA_Id
INNER Join COA_Client CC on CA.COA_Id = CC.COA_Id
LEFT JOIN GeneralLedgerLine GL on CC.AccountId=GL.AccountId
Group BY CT.Code,CT.Types,CT.COA_TypeId
By changing GeneralLedgerLine from an INNER JOIN to a LEFT JOIN, you will still get COA_Type records even though there isn't a matching GeneralLedgerLine.
NOTE: You might have to make the other joins into LEFT JOIN as well.

How to join two columns in a table with a primary key in another table?

I have two tables named BusCity and BusPath which BusCity has the cities and BusPath stores the paths. BusPath has two columns DepId and DesId.
I want to join these tables and get name of the cities.
Here is my code sample:
with cte as (
select
BusPath.DepId,
BusCity.CityName as 'مبدا'
from BusPath
inner join BusCity on BusCity.Id =BusPath.DesId
), ctf as(
select
BusPath.DesId,
BusCity.CityName as 'مقصد'
from BusPath
inner join BusCity on BusCity.Id =BusPath.DepId)
select * from cte , ctf
From this comment
In fact i want to have distinct join for each columns
I understand that your problem is that you have duplicates. It's because you make a cartesian product between cte and ctf
But I don't see why you make 2 CTE for just getting your city names...
Isn't simply that you are trying to do ?
select
P.DesId, Des.CityName as 'مقصد',
P.DepId, Dep.CityName as 'مبدا'
from BusPath P
inner join BusCity Dep on Dep.Id = P.DepId
inner join BusCity Des on Des.Id = P.DesId
second join is missing from your code
select
P.DesId, Des.CityName as 'مقصد',
P.DepId, Dep.CityName as 'مبدا'
from BusPath P
left outer join BusCity Dep on Dep.Id = P.DepId
left outer join BusCity Des on Des.Id = P.DesId

SQL two multi-row counts in one query

I have got two queries:
select m.name, count(distinct a.kursnr)
from trainer t
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e
on t.svnr = e.trainer
left outer join abhaltung a
on t.svnr = a.trainer
group by m.name, t.svnr;
select m.name, count(e.trainer)
from trainer t
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e
on e.trainer = t.svnr
group by m.name, e.trainer;
The first one returns the correct number of courses (kursnr) and the second number the correct number of individual classes (einzeltraining) hold by a trainer. However, I cannot make one SQL statement which shows both values in one table. Any help would be appreciated. Thank you.
While there is likely a more efficient way to do this, I wanted to show you the easy way to combine any two queries that share a common field such as this:
select coalesce(q2.name, q1.name) As Name, q1.KursnrCount, q2.TrainerCount
from
( --original first query
select m.name, count(distinct a.kursnr) as KursnrCount
from trainer t
left outer join abhaltung a
on t.svnr = a.trainer
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e on svnr = e.trainer
group by m.name, t.svnr
) q1
full join
( --original second query
select count(e.trainer) as TrainerCount, m.name
from trainer t
left outer join einzeltraining e
on e.trainer = t.svnr
left outer join mitarbeiter m
on t.svnr = m.svnr
group by e.trainer, m.name
) q2 on q2.name = q1.name
You could also use an inner join or left join, instead of a full join, depending on how the name fields from those queries match up.

Left Join two columns with the same type of data, different values

In my table I have two columns, cars.station1_id and cars.station2_id. These columns contain an ID number referencing stations.id.
I need to take the two ID's and retrieve stations.name for each of them.
How do I do this?
This is my code to join one column:
SELECT station1_id, station2_id FROM cars
LEFT JOIN stations
ON stations.id = cars.station_id
SELECT a.name, b.name
FROM cars
LEFT JOIN stations a ON a.id = station_id
LEFT JOIN stations b ON b.id = station2_id
The key is to use a different alias each time.
s1 and s2 in this case:
SELECT station1_id, station2_id
FROM cars
LEFT JOIN stations s1
ON s1.id = cars.station1_id
LEFT JOIN stations s2
ON s1.id = cars.station2_id