go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.52k stars 118 forks source link

How to use rollup in groupby #182

Closed 1llu5ion closed 1 year ago

1llu5ion commented 1 year ago

Hello, i want to use select statement like this

SELECT warehouse, SUM(quantity) FROM inventory GROUP BY ROLLUP (warehouse);

but i don't know how to do it in JET

go-jet commented 1 year ago

Hi @1llu5ion . ROLLUP is not supported at the moment, but you can add a custom support. Check the wiki.

You can define a custom ROLLUP function:

func ROLLUP(expressions ...Expression) Expression {
    return Func("ROLLUP", expressions...)
}

Which than can be used, in the queries, like any other jet function:

stmt := SELECT(
    Inventory.Warehouse,
    SUM(Inventory.Quantity),
).FROM(
    Inventory,
).GROUP_BY(
    ROLLUP(Inventory.Warehouse),
)
1llu5ion commented 1 year ago

@go-jet Thank you for your feedback.

go-jet commented 1 year ago

ROLLUP support added in v2.10.0 release - wiki.