I have the following statement: There exists some extra columns in table Positions that I want as output for “display data” but I don’t want in the group by statement. These are Site, Desk Final output would have the following columns: Ideally I’d want the data sorted like: How to achieve this? Answer It does not make sense to include
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
Can I use non-aggregate columns with group by?
You cannot (should not) put non-aggregates in the SELECT line of a GROUP BY query. I would however like access the one of the non-aggregates associated with the max. In plain english, I want a table …
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
MySQL “Group By” and “Order By”
I want to be able to select a bunch of rows from a table of e-mails and group them by the from sender. My query looks like this: The query almost works as I want it — it selects records grouped by e-mail. The problem is that the subject and timestamp don’t correspond to the most recent record for a
How to concatenate strings of a string field in a PostgreSQL ‘group by’ query?
I am looking for a way to concatenate the strings of a field within a group by query. So for example, I have a table: and I wanted to group by company_id to get something like: There is a built-in function in mySQL to do this group_concat Answer PostgreSQL 9.0 or later: Modern Postgres (since 2010) has the string_agg(expression, delimiter)