select sf.id,
sf.name,
sum( max(ordr.current_deadline) - min(o.order_date)) as timeline
from order_control.sp_foiv sf
join order_control.order_executor oe
on oe.foiv_id = sf.id
and not oe.obsolete
and oe.is_head_executor
join order_control.order o
on o.order_id = oe.order_id
and not o.obsolete
and o.status_id in (39644::bigint, 39645::bigint, 39646::bigint)
join order_control.project p
on p.project_id = o.project_id
and not p.obsolete
and p.status_id in (39644::bigint, 39645::bigint, 39646::bigint)
join order_control.order_result ordr
on ordr.project_id = p.project_id
and not ordr.obsolete
where not sf.obsolete
group by sf.id, sf.name
Assistant
Your SQL query is attempting to aggregate data from multiple tables related to orders and projects. Let's break it down step by step. Also, I'll provide some suggestions for clarity and performance.
Query Breakdown
Select Columns:
You are selecting sf.id, sf.name from the sp_foiv table.
You calculate a timeline which is the sum of the difference between the maximum current_deadline from the order_result table and the minimum order_date from the order table.
Joins:
You join sp_foiv (sf) with order_executor (oe) where the foiv_id matches, it is not obsolete, and is the head executor.
Then you join order (o) on its order_id, ensuring it’s not obsolete and it has a specific status.
Next, you join with project (p) on project_id, again checking for non-obsolete and valid status.
Lastly, you join with order_result (ordr) on project_id, also confirming it isn't obsolete.
Where Clause:
Finally, you add a condition to ensure that the sp_foiv entry is not obsolete.
Group By:
The results are grouped by the sf.id and sf.name.
Suggestions for Improvement
Sum and Aggregation:
The SUM function is applied to the calculation of the timeline. To ensure that your aggregation functions properly, you might want to first compute the timeline for each group before summing it.
Column Naming:
Consider renaming the calculated column for clarity. Instead of timeline, a more descriptive name like total_timeline might be better.
Common Table Expressions (CTEs):
If the query gets more complex, using CTEs can improve readability.
Here’s a refactored version of your SQL query that incorporates those suggestions:
sql
WITH OrderTimeline AS (
SELECT
sf.id,
sf.name,
MAX(ordr.current_deadline) - MIN(o.order_date) AS timeline
FROM
order_control.sp_foiv sf
JOIN
order_control.order_executor oe ON oe.foiv_id = sf.id
AND NOT oe.obsolete
AND oe.is_head_executor
JOIN
order_control.order o ON o.order_id = oe.order_id
AND NOT o.obsolete
AND o.status_id IN (39644::bigint, 39645::bigint, 39646::bigint)
JOIN
order_control.project p ON p.project_id = o.project_id
AND NOT p.obsolete
AND p.status_id IN (39644::bigint, 39645::bigint, 39646::bigint)
JOIN
order_control.order_result ordr ON ordr.project_id = p.project_id
AND NOT ordr.obsolete
WHERE
NOT sf.obsolete
GROUP BY
sf.id, sf.name
)
SELECT
id,
name,
SUM(timeline) AS total_timeline
FROM
OrderTimeline
GROUP BY
id, name;
Conclusion
The key changes include calculating the timeline in a CTE for better organization and clarity, and ensuring that your logical aggregation occurs at the right stages to yield accurate results. Review this and see if it fits your needs!