Home > Software engineering >  How to do GROUP BY [empty]
How to do GROUP BY [empty]

Time:11-06

Is there a way for explicitly specifying an empty aggregation, such as the following:

SELECT NULL AS Product, SUM(Revenue) FROM Sales GROUP BY ()

Rather than the implicit version which doesn't have a GROUP BY clause:

SELECT NULL AS Product, SUM(Revenue) FROM Sales

Sample input/output:

WITH Sales (Product, Revenue) AS (VALUES ('a',10),('b',20))
select NULL Product, SUM(Revenue) from Sales;
┌─────────┬──────────────┐
│ product ┆ sum(revenue) │
╞═════════╪══════════════╡
│         ┆           30 │
└─────────┴──────────────┘

The query is correct as-is, I'm just wondering if there is a way to make the GROUP BY explicit. This is for a very basic downstream parser that checks if it is an aggregation query based on if the words GROUP BY are in the normalized query.

CodePudding user response:

You can use expressions to group by. so grouping by an expression that is constant is equivalent removing it from the group by statement

i.e. the following are equivalent:

WITH Sales (Product, Revenue) AS (VALUES ('a',10),('b',20))
select NULL Product, SUM(Revenue) from Sales;

and

WITH Sales (Product, Revenue) AS (VALUES ('a',10),('b',20))
select NULL Product, SUM(Revenue) from Sales group by 1=1;
  • Related