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

Commit 228490d

Browse files
author
Michael Liebmann
committed
fix: simplify segmented aggregation structure
1 parent 9cb0f58 commit 228490d

File tree

1 file changed

+25
-72
lines changed

1 file changed

+25
-72
lines changed

src/actions/chatWithYourDb.ts

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -337,86 +337,39 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
337337
* Add validation comments showing segment math
338338
* Ensure segment values sum up to totals
339339
340-
- For segmented analysis with totals:
340+
- For segmented aggregations:
341341
* Structure as:
342342
WITH base_metrics AS (
343-
-- Calculate base metrics
344-
SELECT ... FROM source_table
345-
),
346-
all_segments AS (
347-
SELECT 'With Discounts' as segment, 0 as sort_order, ...
348-
FROM base_metrics WHERE condition
349-
UNION ALL
350-
SELECT 'No Discounts' as segment, 1 as sort_order, ...
351-
FROM base_metrics WHERE NOT condition
352-
UNION ALL
353-
SELECT 'Total' as segment, 2 as sort_order, ...
354-
FROM base_metrics
355-
)
356-
SELECT * FROM all_segments ORDER BY sort_order;
357-
* Keep ORDER BY only in final SELECT
358-
* Add sort_order for segment ordering
359-
360-
- For statistical calculations:
361-
* Calculate correlations in steps:
362-
WITH base_metrics AS (
363-
SELECT key_id,
364-
AVG(value1) as metric1,
365-
AVG(value2) as metric2
366-
FROM source_table
367-
GROUP BY key_id
368-
),
369-
means AS (
370-
SELECT
371-
AVG(metric1) as avg1,
372-
AVG(metric2) as avg2
373-
FROM base_metrics
374-
),
375-
deviations AS (
343+
-- First calculate row-level metrics
376344
SELECT
377-
base_metrics.*,
378-
means.avg1,
379-
means.avg2,
380-
(metric1 - means.avg1) * (metric2 - means.avg2) as deviation_product,
381-
POWER(metric1 - means.avg1, 2) as dev1_squared,
382-
POWER(metric2 - means.avg2, 2) as dev2_squared
383-
FROM base_metrics CROSS JOIN means
384-
)
385-
SELECT
386-
SUM(deviation_product) / SQRT(SUM(dev1_squared) * SUM(dev2_squared)) as correlation
387-
FROM deviations;
388-
389-
- For complex aggregations with segments:
390-
* Structure multi-level aggregations properly:
391-
WITH
392-
base_calculations AS (
393-
-- Calculate raw metrics
394-
SELECT ... FROM source_table
345+
CASE WHEN condition THEN 'A' ELSE 'B' END as segment,
346+
field1,
347+
field2,
348+
field3
349+
FROM source_table
395350
),
396351
segment_metrics AS (
397-
-- Calculate segment-specific metrics
352+
-- Then aggregate by segment
398353
SELECT
399-
'Segment Name' as segment_name,
400-
metrics...
401-
FROM base_calculations
402-
WHERE segment_condition
403-
),
404-
total_metrics AS (
405-
-- Calculate overall totals
406-
SELECT
407-
'Total' as segment_name,
408-
metrics...
409-
FROM base_calculations
410-
),
411-
combined_results AS (
412-
-- Combine segments and totals
413-
SELECT *, 0 as sort_order FROM segment_metrics
354+
segment,
355+
AVG(field1) as avg1,
356+
SUM(field2) as sum2,
357+
COUNT(*) as count
358+
FROM base_metrics
359+
GROUP BY segment
414360
UNION ALL
415-
SELECT *, 1 as sort_order FROM total_metrics
361+
SELECT
362+
'Total' as segment,
363+
AVG(field1) as avg1,
364+
SUM(field2) as sum2,
365+
COUNT(*) as count
366+
FROM base_metrics
416367
)
417-
-- Final selection with ordering
418-
SELECT * FROM combined_results
419-
ORDER BY sort_order, segment_name;
368+
SELECT * FROM segment_metrics
369+
ORDER BY CASE
370+
WHEN segment = 'Total' THEN 'Z'
371+
ELSE segment
372+
END;
420373
* Never use ORDER BY within UNIONed queries
421374
* Add sort columns for segment ordering
422375
* Keep aggregation logic consistent across segments

0 commit comments

Comments
 (0)