Problems with Postgresql ERROR: subquery uses ungrouped column "ev.title" from outer query - postgresql

I have a query like this:
select c.id, c.name, c.website, c.longdescription, c.description, c.email,
(SELECT jsonb_agg(ev) FROM
(SELECT ev.title, ev.description, ev.longdescription,
(SELECT jsonb_agg(ed) FROM
(SELECT ed.startdate, ed.enddate, ed.id WHERE ed.id notnull)ed) as dates, ev.id WHERE ev.id notnull) ev) as events,
(SELECT jsonb_agg(ca) FROM (SELECT ct.zip, ca.id, ca.street1, ca.street2, ca.addresstype_id, ST_Y(ca.geopoint::geometry) as latitude, ST_X(ca.geopoint::geometry) as longitude
WHERE ca.id notnull)ca) as addresses
FROM companies c
LEFT JOIN events ev ON ev.company_id = c.id
LEFT JOIN companyaddresses ca ON ca.company_id = c.id
LEFT JOIN cities ct ON ct.id = ca.city_id
LEFT JOIN eventdates ed ON ed.event_id = ev.id
GROUP by c.id
I am getting the error "ERROR: subquery uses ungrouped column "ev.title" from
outer query Position: 125".
Can't figure out how to group it correctly for the subqueries. Any suggestions?

Give this a try:
SELECT c.id, c.name, c.website, c.longdescription, c.description, c.email,
    (SELECT jsonb_agg(ev) FROM
        (SELECT even.title, even.description, even.longdescription,
            (SELECT jsonb_agg(ed) FROM
                (SELECT eventdates.startdate, eventdates.enddate, eventdates.id FROM eventdates WHERE eventdates.event_id = even.id)ed) as dates,
        even.id FROM events even WHERE even.company_id = c.id) ev) as events,
jsonb_agg((SELECT ca FROM (SELECT ct.zip, ca.id, ca.street1, ca.street2, ca.addresstype_id, ST_Y(ca.geopoint::geometry) as latitude, ST_X(ca.geopoint::geometry) as longitude WHERE ca.id notnull)ca)) as addresses
FROM companies c
LEFT JOIN companyaddresses ca ON ca.company_id = c.id
LEFT JOIN cities ct ON ct.id = ca.city_id
Group by c.id

Related

PSQL Query Not Modifications

Can someone please edit this query for psql. I try alot but pgAdmin gives me Error that "d.artifact_type as text" has as Error
SELECT
a.id, a.name, a.description, a.user_id, a.created_at, a.updated_at,
array_agg(row_to_json(c.id, c.first_name, c.last_name, c.email,
b.workspace_id, b.share_id, b.share_with_type, b.write_access)) as users,
array_agg(row_to_json(d.id, d.artifact_name, d.original_artifact_name,
d.artifact_size,d.artifact_type, d.file_address,
d.user_id,d.file_type, d.created_at, f.user_assigned_to)) as files,
array_agg(row_to_json(d.artifact_type as text, d.artifact_type as value)) extensions
FROM workspaces a
LEFT JOIN share_workspaces b ON a.id = b.workspace_id
LEFT JOIN users c ON b.share_id = c.id
LEFT JOIN share_artifacts e ON a.id = e.share_id
LEFT JOIN artifacts d ON d.id = e.artifact_id
LEFT JOIN assigned_artifacts f ON f.artifact_id = d.id
WHERE (a.user_id='${user_id}' AND d.is_deleted is not true)
Group By a.id, a.name, a.description, a.user_id, a.created_at, a.updated_at
ORDER BY a.created_at ASC

postgres error 42803 with subquery and group by

Hi all postgres developer,
below code can run success
select p.* from
(select p.*, count(distinct p1.id) n from TMB p
left join TMB p1 on p.id = p1.pid
left join TUR u on p.id = any(u.jks)
group by p.id) p
join TUR u on u.id = p.uid
but, below code with error message
[42803] ERROR: column "p.xxxx" must appear in the GROUP BY clause or be used in an aggregate function
select p.* from
(select p.*, count(distinct p1.id) n from (select * from TMB) p
left join TMB p1 on p.id = p1.pid
left join TUR u on p.id = any(u.jks)
group by p.id) p
join TUR u on u.id = p.uid
I want to do some where filter on TMB table before left join, so I think can speed up left join.
I think (select * from TMB) is a subquery equal as TMB. I can not understand why this error message. anyone can tell me detail?
The difference is that without the subquery, PostgreSQL can deduce that id is the primary key of tmb, so you need not add all columns of tmb to the GROUP BY clause. With the subquery, PostgreSQL cannot make that deduction, so you have to add all columns.

How to add id sequence from select query in postgresql

I would like to add sequence id after I select data from more than one table
this is my query:
SELECT DISTINCT a.value_suggested_row, c.id as question_id, c.question, b.value
from survey_user_input_line a
LEFT JOIN survey_label b on b.id = a.value_suggested_row
LEFT JOIN survey_question c on c.id = a.question_id
where survey_id = 6
ORDER BY question_id
and this is the result
how to do the correct query to add the id sequence to the query so that the results are like this
Can Anyone help me, please?
In the select list add ROW_NUMBER () OVER (ORDER BY question_id) as id_sequence
SELECT DISTINCT #rownum:=#rownum+1 id_sequence, a.value_suggested_row, c.id as
question_id, c.question, b.value
from survey_user_input_line a
LEFT JOIN survey_label b on b.id = a.value_suggested_row
LEFT JOIN survey_question c on c.id = a.question_id
where survey_id = 6
ORDER BY question_id, (SELECT #rownum:=0) r;

How to use DISTINCT ON in ARRAY_AGG()?

I have the following query:
SELECT array_agg(DISTINCT p.id) AS price_ids,
array_agg(p.name) AS price_names
FROM items
LEFT JOIN prices p on p.item_id = id
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE id = 1;
When I LEFT JOIN the third_table all my prices are duplicated.
I'm using DISTINCT inside ARRAY_AGG() to get the ids without dups, but I want the names without dups aswell.
If I use array_agg(DISTINCT p.name) AS price_names, it will return distinct values based on the name, not the id.
I want to do something similar to array_agg(DISTINCT ON (p.id) p.name) AS price_names, but it is invalid.
How can I use DISTINCT ON inside ARRAY_AGG()?
Aggregate first, then join:
SELECT p.price_ids,
p.price_names,
t3.*
FROM items
LEFT JOIN (
SELECT pr.item_id,
array_agg(pr.id) AS price_ids,
array_agg(pr.name) AS price_names
FROM prices pr
GROUP BY pr.item_id
) p on p.item_id = items.id
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE items.id = 1;
Using a lateral join might be faster if you only pick a single item:
SELECT p.price_ids,
p.price_names,
t3.*
FROM items
LEFT JOIN LATERAL (
SELECT array_agg(pr.id) AS price_ids,
array_agg(pr.name) AS price_names
FROM prices pr
WHERE pr.item_id = items.id
) p on true
LEFT JOIN third_table t3 on third_table.item_id = id
WHERE items.id = 1;

Region-Area-Point of interest query optimization

My database has following structure:
Every Region (CountryStates) can have many cities (Areas). Every Area can have many tourist attractions or points of interest (POIs). I want to get list of how many Areas and Attractions are there in each Region. If there is none, I want to display 0.
This is my query:
SELECT Tab1.Reg AS Reg, CountAreas, CountPois
FROM
(SELECT
c.Name AS Reg,
COUNT(a.Id) AS CountAreas
FROM
CountryStates as c LEFT JOIN
Areas AS a ON a.CountryStates_Id = c.Id
GROUP BY c.Name
) as Tab1 left join
(SELECT
c1.Name AS Reg,
COUNT(p.Id) AS CountPois
FROM
CountryStates as c1 LEFT JOIN
Areas AS a ON a.CountryStates_Id = c1.Id LEFT JOIN
POIs AS p ON a.Id = p.Areas_Id
GROUP BY c1.Name
) as Tab2 on Tab1.Reg = Tab2.Reg
How can I make this query in just one SELECT?
This query returns the same result:
SELECT c.Name AS Reg,
COUNT(DISTINCT(a.Id)) AS CountAreas,
COUNT(p.Id) AS CountPois
FROM CountryStates as c
LEFT JOIN Areas as a ON a.CountryStates_Id = c.Id
LEFT JOIN POIs AS p ON a.Id = p.Areas_Id
GROUP BY c.Name