PSQL Query Not Modifications - postgresql

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

Related

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;

Jasper Report: error executing SQL statement by Collection Parameter

I'm creating a report which's passing by collection parameter.
The parameter name as 'ids' and the class is java.util.Collection.
My query:
select * from order_deliveryorder a
left join order_deliveryorderline b on a.id = b.d_order_id
left join order_item c on c.id = b.product_id
left join order_orderline e on e.id = b.s_order_id
left join order_order f on e.order_id = f.id
left join order_itemsection g on g.id = f.section_id
left join order_location d on d.id = a.location_id
left join order_location h on h.id = a.from_location_id
where a.id in ('2377900603251741014','2377900603251740997','2377900603251740967')
the query in jasper:
select * from order_deliveryorder a
left join order_deliveryorderline b on a.id = b.d_order_id
left join order_item c on c.id = b.product_id
left join order_orderline e on e.id = b.s_order_id
left join order_order f on e.order_id = f.id
left join order_itemsection g on g.id = f.section_id
left join order_location d on d.id = a.location_id
left join order_location h on h.id = a.from_location_id
where $X{IN, a.id, ids}
The value of parameter "ids" is ["2377900603251741014","2377900603251740997","2377900603251740967"]
In the End, had met the error executing sql statement.

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

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

Postgres UPDATE statement

I have moved from mysql to psql, but find it hard to get my head around the UPDATE statement using multiple left joins.
How would you rewrite this in Postgres? (I am using postresql 9.4)
update task t
left join project p on t.project_id = p.id
left join client c on t.client_id = c.id
left join user u on t.user_id = u.id
set t.project_name = p.name,
t.client_name = c.name,
t.user_name = u.name;
Any pointer will be welcome.
Here you go:
WITH task_data AS (
SELECT t.id,
p.name AS project_name,
c.name AS client_name,
u.name AS user_name
FROM task t
LEFT JOIN project p ON t.project_id = p.id
LEFT JOIN client c ON t.client_id = c.id
LEFT JOIN "user" u ON t.user_id = u.id
)
UPDATE task t
FROM task_data d
SET
project_name = d.project_name,
client_name = d.client_name,
user_name = d.user_name
WHERE t.id = d.id
I would be curious to see if there is a more efficient way

Filter table that I do a LEFT JOIN with

Here's my query at the moment:
SELECT A.*
FROM A
LEFT JOIN B ON B.a_id = A.id
Now, suppose there is a table C which relates directly to B. What I'd like to do is to filter out the records of table B based on C - how do I do that? I've tried placing the filter in the WHERE section:
SELECT A.*
FROM A
LEFT JOIN B ON B.a_id = A.id
LEFT JOIN C ON B.c_id = C.id
WHERE C.id > 10
The problem is that the preceding queryalso filters out table A's results. How do I achieve the result that I want?
you need to put that filter in join :
SELECT *
FROM A
LEFT JOIN
(
SELECT B.*
FROM B
INNER JOIN C ON B.c_id = C.id AND C.id > 10
) AS B ON B.a_id = A.id
it will not filter out the A's results .