Skip to content

Tag: group-by

GROUP BY – do not group NULL

I’m trying to figure out a way to return results by using the group by function. GROUP BY is working as expected, but my question is: Is it possible to have a group by ignoring the NULL field. So that it does not group NULLs together because I still need all the rows where the specified field is NULL. So

Query to group by maximum depth of PostgreSQL Ltree?

I want to query for all products with the name “Shania Twain”, but I want group them only by the tree with the deepest nlevel. Assuming I have a table like the following with the name categories So, for example, The problem lies with the HAVING clause requiring a boolean expression. The clause MAX(nlevel(cat.tree)) will return an integer. How do

Group by date range on weeks/months interval

I’m using MySQL and I have the following table: I want to be able to generate reports like this, where periods are done in the last 4 weeks: or in the last 3 months: Any ideas how to make select queries that can generate the equivalent date range and clicks count? Answer

Using group by on multiple columns

I understand the point of GROUP BY x. But how does GROUP BY x, y work, and what does it mean? Answer Group By X means put all those with the same value for X in the one group. Group By X, Y means put all those with the same values for both X and Y in the one group.

MAX on columns generated by SUM and GROUP BY

I’m trying to get the MAX on a column which is generated dynamically using the SUM statement. The SUM statement is used together with the ‘GROUP by’ syntax. This is the original query, however it needs to be modified to work with grouping, sums and of course MAX. As you can see SUM is adding all the values inside video_plays