@@ -325,44 +325,80 @@ To compare assigned category values to a given account's balance:
325325-- -cmd ".parameter set @account_name_like %Savings%" \
326326-- -cmd ".parameter set @include_category_groups 'Home,Food'" \
327327-- < query.sql
328+ CREATE TEMP TABLE excess_query_results AS
328329WITH params AS (
329330 SELECT
330331 TRIM (COALESCE(@account_name_like, ' ' )) AS account_name_like
332+ , TRIM (COALESCE(@plan_id, ' ' )) AS plan_id
331333 , TRIM (COALESCE(@include_category_groups, ' ' ))
332334 AS include_category_groups
333335 , TRIM (COALESCE(@exclude_category_groups, ' ' ))
334336 AS exclude_category_groups
335337)
336338
337- , validation AS (
338- SELECT ' Set @account_name_like' AS error
339- FROM params AS p
340- WHERE p .account_name_like = ' '
339+ , scoped_plans AS (
340+ SELECT
341+ p .id
342+ , p .name
343+ FROM plans AS p
344+ CROSS JOIN params AS prm
345+ WHERE prm .plan_id = ' ' OR p .id = prm .plan_id
346+ )
347+
348+ , split_include_category_groups (value, rest) AS (
349+ SELECT
350+ ' '
351+ , prm .include_category_groups || ' ,'
352+ FROM params AS prm
341353 UNION ALL
342354 SELECT
343- ' Set only one of @include_category_groups '
344- || ' or @exclude_category_groups ' AS error
345- FROM params AS p
346- WHERE p . include_category_groups != ' ' AND p . exclude_category_groups != ' '
355+ TRIM (SUBSTR(rest, 1 , INSTR(rest, ' , ' ) - 1 ))
356+ , SUBSTR(rest, INSTR(rest, ' , ' ) + 1 )
357+ FROM split_include_category_groups
358+ WHERE rest != ' '
347359)
348360
349- SELECT v .error AS error_message
350- FROM validation AS v
351- WHERE v .error IS NOT NULL
352- ;
361+ , include_category_groups AS (
362+ SELECT value AS name
363+ FROM split_include_category_groups
364+ WHERE value != ' '
365+ )
353366
354- WITH params AS (
367+ , split_exclude_category_groups (value, rest) AS (
355368 SELECT
356- TRIM (COALESCE(@account_name_like, ' ' )) AS account_name_like
357- , TRIM (COALESCE(@include_category_groups, ' ' ))
358- AS include_category_groups
359- , TRIM (COALESCE(@exclude_category_groups, ' ' ))
360- AS exclude_category_groups
369+ ' '
370+ , prm .exclude_category_groups || ' ,'
371+ FROM params AS prm
372+ UNION ALL
373+ SELECT
374+ TRIM (SUBSTR(rest, 1 , INSTR(rest, ' ,' ) - 1 ))
375+ , SUBSTR(rest, INSTR(rest, ' ,' ) + 1 )
376+ FROM split_exclude_category_groups
377+ WHERE rest != ' '
378+ )
379+
380+ , exclude_category_groups AS (
381+ SELECT value AS name
382+ FROM split_exclude_category_groups
383+ WHERE value != ' '
384+ )
385+
386+ , matching_accounts AS (
387+ SELECT
388+ sp .id AS plan_id
389+ , sp .name AS plan_name
390+ , COUNT (* ) AS matches
391+ FROM scoped_plans AS sp
392+ INNER JOIN accounts AS a ON sp .id = a .plan_id
393+ CROSS JOIN params AS prm
394+ WHERE NOT a .deleted AND a .name LIKE prm .account_name_like
395+ GROUP BY sp .id , sp .name
361396)
362397
363398, validation AS (
364399 SELECT
365400 p .account_name_like
401+ , p .plan_id
366402 , p .include_category_groups
367403 , p .exclude_category_groups
368404 FROM params AS p
@@ -378,11 +414,62 @@ WITH params AS (
378414 || ' or @exclude_category_groups' AS error
379415 FROM validation AS v
380416 WHERE v .include_category_groups != ' ' AND v .exclude_category_groups != ' '
417+ UNION ALL
418+ SELECT ' No plan matched @plan_id' AS error
419+ FROM validation AS v
420+ WHERE
421+ v .plan_id != ' ' AND NOT EXISTS (
422+ SELECT 1
423+ FROM scoped_plans
424+ )
425+ UNION ALL
426+ SELECT ' No account names matched @account_name_like' AS error
427+ FROM validation AS v
428+ WHERE
429+ v .account_name_like != ' ' AND NOT EXISTS (
430+ SELECT 1
431+ FROM matching_accounts
432+ )
433+ UNION ALL
434+ SELECT
435+ ' Matched more than 1 account in plan: '
436+ || ma .plan_name AS error
437+ FROM matching_accounts AS ma
438+ WHERE ma .matches > 1
439+ UNION ALL
440+ SELECT
441+ ' Unknown include category group in plan '
442+ || sp .name
443+ || ' : '
444+ || icg .name AS error
445+ FROM scoped_plans AS sp
446+ CROSS JOIN include_category_groups AS icg
447+ LEFT JOIN category_groups AS cg
448+ ON
449+ sp .id = cg .plan_id
450+ AND NOT COALESCE(cg .deleted , 0 )
451+ AND LOWER (cg .name ) = LOWER (icg .name )
452+ WHERE cg .id IS NULL
453+ UNION ALL
454+ SELECT
455+ ' Unknown exclude category group in plan '
456+ || sp .name
457+ || ' : '
458+ || ecg .name AS error
459+ FROM scoped_plans AS sp
460+ CROSS JOIN exclude_category_groups AS ecg
461+ LEFT JOIN category_groups AS cg
462+ ON
463+ sp .id = cg .plan_id
464+ AND NOT COALESCE(cg .deleted , 0 )
465+ AND LOWER (cg .name ) = LOWER (ecg .name )
466+ WHERE cg .id IS NULL
381467)
382468
383469, valid_params AS (
384470 SELECT
385471 v .account_name_like
472+ , v .plan_id
386473 , v .include_category_groups
387474 , v .exclude_category_groups
388475 FROM validation AS v
@@ -405,7 +492,7 @@ WITH params AS (
405492 TRUE
406493 AND NOT a .deleted
407494 AND a .name LIKE v .account_name_like
408- AND (COALESCE(@ plan_id, ' ' ) = ' ' OR p .id = @ plan_id)
495+ AND (v . plan_id = ' ' OR p .id = v . plan_id )
409496)
410497
411498, category_totals AS (
@@ -420,36 +507,61 @@ WITH params AS (
420507 AND c .category_group_name != ' Internal Master Category'
421508 AND (
422509 v .include_category_groups = ' '
423- OR INSTR(
424- ' ,'
425- || LOWER (REPLACE(v .include_category_groups , ' , ' , ' ,' ))
426- || ' ,'
427- , ' ,' || LOWER (c .category_group_name ) || ' ,'
510+ OR EXISTS (
511+ SELECT 1
512+ FROM include_category_groups AS icg
513+ WHERE LOWER (icg .name ) = LOWER (c .category_group_name )
428514 )
429- > 0
430515 )
431516 AND (
432517 v .exclude_category_groups = ' '
433- OR INSTR(
434- ' ,'
435- || LOWER (REPLACE(v .exclude_category_groups , ' , ' , ' ,' ))
436- || ' ,'
437- , ' ,' || LOWER (c .category_group_name ) || ' ,'
518+ OR NOT EXISTS (
519+ SELECT 1
520+ FROM exclude_category_groups AS ecg
521+ WHERE LOWER (ecg .name ) = LOWER (c .category_group_name )
438522 )
439- = 0
440523 )
441- AND (COALESCE(@ plan_id, ' ' ) = ' ' OR c .plan_id = @ plan_id)
524+ AND (v . plan_id = ' ' OR c .plan_id = v . plan_id )
442525 GROUP BY c .plan_id
443526)
444527
445528SELECT
446- ma .plan_name AS " plan"
529+ ve .error AS error_message
530+ , NULL AS " plan"
531+ , NULL AS account
532+ , NULL AS total
533+ , NULL AS excess
534+ FROM validation_errors AS ve
535+
536+ UNION ALL
537+
538+ SELECT
539+ NULL AS error_message
540+ , ma .plan_name AS " plan"
447541 , ma .account_name AS account
448542 , PRINTF(' %.2f' , COALESCE(ct .total , 0 )) AS total
449543 , PRINTF(' %.2f' , ma .account_amount - COALESCE(ct .total , 0 )) AS excess
450544FROM matched_accounts AS ma
451545LEFT JOIN category_totals AS ct ON ma .plan_id = ct .plan_id
452- ORDER BY " plan" , account
546+ ;
547+
548+ SELECT error_message
549+ FROM excess_query_results
550+ WHERE error_message IS NOT NULL
551+ ;
552+
553+ SELECT
554+ eqr." plan"
555+ , eqr .account
556+ , eqr .total
557+ , eqr .excess
558+ FROM excess_query_results AS eqr
559+ WHERE
560+ NOT EXISTS (
561+ SELECT 1
562+ FROM excess_query_results AS eqr_errors
563+ WHERE eqr_errors .error_message IS NOT NULL
564+ )
453565;
454566```
455567
0 commit comments