Skip to content

Commit

Permalink
fix: simplify segmentation with totals structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Liebmann committed Jan 22, 2025
1 parent e56fb9c commit 9cb0f58
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/actions/chatWithYourDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,25 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
* Add validation comments showing segment math
* Ensure segment values sum up to totals
- For segmentation with totals:
* Structure queries with proper ordering:
- Wrap segmented results and totals in a CTE
- Apply final ordering outside the UNION
- Example pattern:
WITH base_data AS (...),
segment_calcs AS (...),
total_calcs AS (...),
combined_results AS (
SELECT ..., 0 as sort_order FROM segment_calcs
UNION ALL
SELECT ..., 1 as sort_order FROM total_calcs
)
SELECT ... FROM combined_results
ORDER BY sort_order
* Add numeric sort_order column for reliable ordering
* Keep ORDER BY outside of UNIONed queries
* Document segment definitions in comments
- For segmented analysis with totals:
* Structure as:
WITH base_metrics AS (
-- Calculate base metrics
SELECT ... FROM source_table
),
all_segments AS (
SELECT 'With Discounts' as segment, 0 as sort_order, ...
FROM base_metrics WHERE condition
UNION ALL
SELECT 'No Discounts' as segment, 1 as sort_order, ...
FROM base_metrics WHERE NOT condition
UNION ALL
SELECT 'Total' as segment, 2 as sort_order, ...
FROM base_metrics
)
SELECT * FROM all_segments ORDER BY sort_order;
* Keep ORDER BY only in final SELECT
* Add sort_order for segment ordering
- For statistical calculations:
* Calculate correlations in steps:
Expand Down

0 comments on commit 9cb0f58

Please sign in to comment.