简体   繁体   中英

SQL Group by sum() and select first in denormalized data

I have the following denormalised data:

╔════╦═══════╦══════════╦════════════╗
║ id ║ value ║ group_id ║ group_name ║
╠════╬═══════╬══════════╬════════════╣
║  0 ║    10 ║        0 ║ Group 0    ║
║  1 ║    10 ║        0 ║ Group 0    ║
║  2 ║    10 ║        0 ║ Group 0    ║
║  3 ║    10 ║        0 ║ Group 0    ║
║  4 ║    10 ║        1 ║ Group 1    ║
║  5 ║    10 ║        1 ║ Group 1    ║
║  6 ║    10 ║        1 ║ Group 1    ║
║  7 ║    10 ║        2 ║ Group 2    ║
║  8 ║    10 ║        2 ║ Group 2    ║
║  9 ║    10 ║        2 ║ Group 2    ║
╚════╩═══════╩══════════╩════════════╝

I am currently running a query that sums value by group_id :

select sum(value), group_id
from table
group by group_id

How do I extend this query to also include the group_name for each column? I need my result to look like:

╔═══════╦══════════╦════════════╗
║ value ║ group_id ║ group_name ║
╠═══════╬══════════╬════════════╣
║    40 ║        0 ║ Group 0    ║
║    30 ║        1 ║ Group 1    ║
║    30 ║        2 ║ Group 2    ║
╚═══════╩══════════╩════════════╝

Is the only way to do this to use OVER ?

Use group by !

select sum(value) as value, group_id, group_name
from mytable
group by group_id, group_name

In standard SQL, your original query is not a valid aggregation query to start with. All non-aggregated columns must appear in the group by clause (so you needed group by group_id`), although some databases (namely, MySQL) can be tweaked to allow otherwise.

  • Please specify db vendor.
  • Your query select sum(value), group_id from table is invalid, I assume you run select sum(value), group_id from table group by group_id .
  • Assuming you trust your denormalization and group_name always depends on group_id , you can just add group_name into group by and select clause: select sum(value), group_id, group_name from table group by group_id, group_name

You have two options. One is to use an aggregation function:

select sum(value), group_id, max(group_name) as group_name
from table
group by group_id;

The second is to include the column unaggregated in the select and group by :

select sum(value), group_id, group_name
from table
group by group_id, group_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM