-
Notifications
You must be signed in to change notification settings - Fork 1
Add month-on-month purchase value difference analysis documentation for a specific supplier #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Month on Month Purchase Value Difference Analysis for a Specific Supplier - SSMM | ||
|
|
||
| > Completed supplier deliveries with product value, location, and creator details | ||
|
|
||
| ## Purpose | ||
|
|
||
| Lists completed supply deliveries for a single supplier and shows the purchase value at product level. | ||
|
|
||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | Example | | ||
| |-----------|------|-------------|---------| | ||
| | `start_date` | date | Optional lower bound for `created_date` | `2026-01-01` | | ||
| | `end_date` | date | Optional upper bound for `created_date` | `2026-01-31` | | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| SELECT | ||
| epk.name AS product_name, | ||
| esd.supplied_item_quantity AS quantity, | ||
| ep.purchase_price AS unit_price, | ||
| (ep.purchase_price * esd.supplied_item_quantity) AS value, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] Value is computed from the product's current price, not the price at time of delivery.
COALESCE(esd.total_purchase_price, ep.purchase_price * esd.supplied_item_quantity) AS value |
||
| DATE(esd.created_date) AS created_date, | ||
| org.name AS supplier_name, | ||
| fl.name AS destination_location, | ||
| CONCAT(u.first_name, ' ', u.last_name) AS created_by | ||
| FROM emr_supplydelivery esd | ||
| JOIN emr_deliveryorder edo | ||
| ON esd.order_id = edo.id | ||
| JOIN emr_product ep | ||
| ON esd.supplied_item_id = ep.id | ||
| JOIN emr_productknowledge epk | ||
| ON epk.id = ep.product_knowledge_id | ||
| JOIN users_user u | ||
| ON u.id = edo.created_by_id | ||
| JOIN emr_organization org | ||
| ON org.id = edo.supplier_id | ||
| JOIN emr_facilitylocation fl | ||
| ON fl.id = edo.destination_id | ||
| WHERE esd.status = 'completed' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] Missing None of WHERE esd.status = 'completed'
AND esd.deleted = FALSE
AND edo.deleted = FALSE
AND edo.origin_id IS NULL
AND edo.supplier_id = '20697'
AND edo.status = 'completed'
AND ep.purchase_price IS NOT NULL |
||
| AND edo.origin_id IS NULL | ||
| AND edo.supplier_id = '20697' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Low]
AND edo.supplier_id = 20697 |
||
| AND edo.status = 'completed' | ||
| AND ep.purchase_price IS NOT NULL | ||
| --AND ({{start_date}} IS NULL OR DATE(esd.created_date) > {{start_date}}::date) | ||
| --AND ({{end_date}} IS NULL OR DATE(esd.created_date) <= {{end_date}}::date) | ||
| ORDER BY value DESC; | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - **Supplier filter:** `edo.supplier_id = '20697'` is hardcoded, so the query is scoped to one supplier. | ||
| - **Completed deliveries only:** Both `esd.status` and `edo.status` must be `completed`. | ||
| - **Ordering:** Results are sorted by highest purchase value first. | ||
|
|
||
|
|
||
| *Last updated: 2026-08-31* | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Critical] Query does not deliver a month-on-month difference analysis.
The title and PR title promise a "Month on Month Purchase Value Difference Analysis", but the SQL is a flat, row-per-delivery itemized list ordered by
value DESC— there is noGROUP BYon month, no time-bucketing, and no computed delta comparing one period to the previous one (e.g.LAG()or a self-join). The## Purposetext ("Lists completed supply deliveries ... at product level") actually matches the SQL, but not the filename/title at all.ENG-974 could not be fetched to confirm the exact ask (JIRA returned 404/401 for this ticket), but going purely off the stated title: if a real MoM diff was intended, this query doesn't answer it — a dashboard consumer would see a raw delivery list, not a period-over-period comparison. Either the title/purpose need to be rewritten to describe what this query actually is (a supplier delivery drill-down), or the SQL needs monthly aggregation plus a delta calculation.
Suggested direction if a real MoM diff was intended: