Skip to content
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

Support for Placing Aggregate Filters in HAVING Clause #361

Open
UmakanthKaspa opened this issue Nov 21, 2024 · 0 comments
Open

Support for Placing Aggregate Filters in HAVING Clause #361

UmakanthKaspa opened this issue Nov 21, 2024 · 0 comments

Comments

@UmakanthKaspa
Copy link
Contributor

Description:

When a filter with an aggregate function is added (e.g., (SUM(\tabGL Entry.debit` - `tabGL Entry.credit`) > 0)), the generated SQL places the filter in the WHERE` clause by default:

SELECT count(*) AS `Party`
FROM `tabGL Entry` AS `tabGL Entry`
WHERE SUM(`tabGL Entry`.debit - `tabGL Entry`.credit) > 0
LIMIT 100;

This query results in an SQL error because aggregate functions like SUM cannot be used in the WHERE clause. Instead, they should be placed in the HAVING clause to ensure proper execution after aggregation.

Expected SQL:

SELECT count(*) AS `Party`
FROM `tabGL Entry` AS `tabGL Entry`
GROUP BY `tabGL Entry`.party
HAVING SUM(`tabGL Entry`.debit - `tabGL Entry`.credit) > 0
LIMIT 100;

Proposed Solution:

  1. Enhance Query Logic:

    • Automatically detect aggregate functions in filters and place them in the HAVING clause.
  2. Provide User Control:

    • Add an option in the filter configuration UI for users to explicitly specify whether a filter should apply to the WHERE or HAVING clause.

Steps to Reproduce:

  1. Open Frappe Insights.
  2. Create a visual type "Query".
  3. Select the data source.
  4. Select the table Gl Entry.
  5. Select the column Party.
  6. Add a filter using the expression (SUM(\tabGL Entry.debit` - `tabGL Entry.credit`) > 0)`.
  7. Generate the SQL query.
  8. Observe that the filter is incorrectly placed in the WHERE clause, causing an error.

Screenshots:

image
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant