New answers tagged group-by
0
votes
Conditional selection in a GROUP BY clause
Using FIRST_VALUE the query would be a little simpler.
Using the following data example
CREATE TABLE cities (
country VARCHAR(20),
city VARCHAR(20),
population INT
);
INSERT INTO cities ...
0
votes
Conditional selection in a GROUP BY clause
You need to join the country and population as follows
SELECT A.country,A.city,A.pop population FROM
(SELECT country,city,population pop FROM cities) A INNER JOIN
(SELECT country,MAX(population) pop ...
1
vote
Accepted
Why use $group / group_by instead of multiple requests?
why perform a query involving $group which in principle processes each document sequentially
This is not necessarily true. The database engine is designed to be optimized for such type of operations, ...
1
vote
Adding subtotals using GROUPING SETS
This is a translation of your description into SQL:
... GROUP BY GROUPING SETS ((store, department, gender), (store))
1
vote
Select first row of a particular group with one attribute selected as 0 while all other rows should be selected as is
I am currently doing the above by breaking/splitting my queries. Is it possible to do this in a single query?
Sure, you can use a window function in a subquery to order the rows within a grouping (...
Top 50 recent answers are included
Related Tags
group-by × 733mysql × 294
postgresql × 140
sql-server × 131
join × 94
aggregate × 69
count × 64
oracle × 55
select × 45
query × 41
performance × 40
greatest-n-per-group × 38
sum × 37
order-by × 35
t-sql × 31
subquery × 30
optimization × 25
distinct × 22
query-performance × 21
index × 20
mariadb × 20
window-functions × 20
gaps-and-islands × 15
group-concatenation × 15
case × 14