Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 6ef14ff

Browse files
author
Michael Liebmann
committed
refactor: remove specific order pattern, keep generic multi-level aggregation
1 parent bb603c4 commit 6ef14ff

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

src/actions/chatWithYourDb.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -373,50 +373,55 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
373373
SELECT * FROM segment_metrics
374374
ORDER BY segment;
375375
376-
5. Order-Level Analysis with Line Items:
377-
WITH order_totals AS (
376+
5. Multi-Level Aggregation Pattern:
377+
WITH base_aggregates AS (
378378
SELECT
379-
o.order_id,
380-
COUNT(*) as line_items,
381-
SUM(CAST(od.quantity AS NUMERIC)) as total_quantity,
382-
SUM(CAST(od.unit_price * od.quantity AS NUMERIC)) as total_value,
383-
SUM(CAST(od.unit_price * od.quantity * od.discount AS NUMERIC)) as total_discount,
384-
MAX(CASE WHEN od.discount > 0 THEN 1 ELSE 0 END) as has_discount
385-
FROM orders o
386-
JOIN order_details od ON od.order_id = o.order_id
387-
GROUP BY o.order_id
379+
t1.primary_key, -- Replace with actual PK
380+
COUNT(*) as detail_count,
381+
SUM(CAST(t2.numeric_field1 AS NUMERIC)) as sum_field1,
382+
SUM(CAST(t2.numeric_field2 AS NUMERIC)) as sum_field2,
383+
CASE WHEN MAX(t2.condition_field) > 0 THEN 1 ELSE 0 END as has_condition
384+
FROM parent_table t1 -- Replace with actual tables
385+
JOIN child_table t2 ON t2.foreign_key = t1.primary_key
386+
GROUP BY t1.primary_key
388387
),
389388
segments AS (
390389
SELECT
391390
'Segment' as result_type,
392-
CASE WHEN has_discount = 1 THEN 'With Discount'
393-
ELSE 'No Discount' END as segment,
394-
COUNT(*) as order_count,
395-
AVG(total_quantity) as avg_order_size,
396-
AVG(total_value) as avg_order_value,
397-
AVG(total_discount) as avg_discount_value,
391+
CASE
392+
WHEN has_condition = 1 THEN 'Condition Met'
393+
ELSE 'Condition Not Met'
394+
END as segment,
395+
COUNT(*) as record_count,
396+
ROUND(AVG(detail_count), 2) as avg_details,
397+
ROUND(AVG(sum_field1), 2) as avg_sum1,
398+
ROUND(AVG(sum_field2), 2) as avg_sum2,
398399
ROUND(
399-
SUM(total_discount) * 100.0 /
400-
NULLIF(SUM(total_value), 0),
400+
SUM(sum_field2) * 100.0 /
401+
NULLIF(SUM(sum_field1), 0),
401402
2
402-
) as discount_percentage
403-
FROM order_totals
404-
GROUP BY has_discount
403+
) as calculated_percentage
404+
FROM base_aggregates
405+
GROUP BY
406+
CASE
407+
WHEN has_condition = 1 THEN 'Condition Met'
408+
ELSE 'Condition Not Met'
409+
END
405410
),
406411
totals AS (
407412
SELECT
408413
'Total' as result_type,
409-
'All Orders' as segment,
410-
COUNT(*) as order_count,
411-
AVG(total_quantity) as avg_order_size,
412-
AVG(total_value) as avg_order_value,
413-
AVG(total_discount) as avg_discount_value,
414+
'All Records' as segment,
415+
COUNT(*) as record_count,
416+
ROUND(AVG(detail_count), 2) as avg_details,
417+
ROUND(AVG(sum_field1), 2) as avg_sum1,
418+
ROUND(AVG(sum_field2), 2) as avg_sum2,
414419
ROUND(
415-
SUM(total_discount) * 100.0 /
416-
NULLIF(SUM(total_value), 0),
420+
SUM(sum_field2) * 100.0 /
421+
NULLIF(SUM(sum_field1), 0),
417422
2
418-
) as discount_percentage
419-
FROM order_totals
423+
) as calculated_percentage
424+
FROM base_aggregates
420425
)
421426
SELECT * FROM segments
422427
UNION ALL

0 commit comments

Comments
 (0)