Postgres UPDATE statement - postgresql

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

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.

Postgresql INTERSECT randomly not working

I've got a problem with the syntax of a SQL query. It works sometimes and sometimes not. It's defenitly a problem with the query.
An error at or near "INTERSECTSELECT".
SQL Error: 0, SQLState: 42601
SELECT DISTINCT s.id,
s.title,
s.question,
s.comments,
s.start_date,
s.end_date,
s.motivation,
s.goals,
s.method,
s.type,
s.channel,
s.rhythm,
s.sample
FROM study s
LEFT OUTER JOIN study_customer_information sci
ON sci.study_id = s.id
LEFT OUTER JOIN customer_information ci
ON ci.id = sci.customer_information_id
WHERE s.is_active = :isActive
AND
s.language = :language AND s.id IN (SELECT sci.study_id
FROM study_customer_information sci
INNER JOIN customer_information ci
ON ci.id = sci.customer_information_id
WHERE ci.name_de in (:customer_informations)
GROUP BY sci.study_id
HAVING COUNT(sci.study_id) = :number_customer_informations INTERSECT SELECT st.study_id
FROM study_touchpoint st
INNER JOIN touchpoint tp ON tp.id = st.touchpoint_id
INNER JOIN subject_area sa ON sa.id = tp.subject_area_id
WHERE sa.name_DE = :subject_areas_0 INTERSECT SELECT st.study_id
FROM study_touchpoint st
INNER JOIN touchpoint tp ON tp.id = st.touchpoint_id
INNER JOIN touch_point_type tpt ON tpt.id = tp.touch_point_type_id
WHERE tpt.name_DE = :touchpoint_types_0 INTERSECT SELECT sth.study_id
FROM study_theme sth
INNER JOIN theme th
ON th.id = sth.theme_id
WHERE th.name_de in (:themes)
GROUP BY sth.study_id
HAVING COUNT(sth.study_id) = :number_themes) ORDER BY s.title

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.

MOODLE - i need a query that returns latest enrolment date in course

this query is not returning correct latest_enrolment date. whenever i enrol a user in course, it doesnot updates enrolment date in database.. can anyone help?
SELECT TRIM(c.id) course_id,TRIM(c.fullname) course_fullname,FROM_UNIXTIME(u.timecreated) as registration ,FROM_UNIXTIME( ra.timemodified ) latest_enrolment_date,COUNT( * ) AS enrol_count
FROM mdl_user u
INNER JOIN mdl_role_assignments ra ON ra.userid = u.id
INNER JOIN mdl_context ct ON ct.id = ra.contextid
INNER JOIN mdl_course c ON c.id = ct.instanceid
INNER JOIN mdl_role r ON r.id = ra.roleid
INNER JOIN mdl_course_categories cc ON cc.id = c.category
WHERE r.id =5 GROUP BY c.id
Something like this
SELECT MAX(ue.timecreated) AS latest_enrolment_date
FROM mdl_enrol e
JOIN mdl_user_enrolments ue ON ue.enrolid = e.id
WHERE e.courseid = xx