Skip to content

How to implement LEFT OUTER JOIN, SUM and HAVING condition #373

Open
@hrdwdmrbl

Description

@hrdwdmrbl

I have something like a Products table and a Sales table. So a product has many sales. I would like to filter for products with total sales value over X. So my SQL is something like

Product.left_outer_joins(:sales).select("products.id", "SUM(DISTINCT sales.quantity AS sales_quantity)")

but the problem is that the datatable total rows and page rows queries use a .count which removes the select'd columns but still want to retain my having("sales_quantity > X") condition. So the query fails because there is no "sales_quantity" column.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions