-3

Using MySQL, I can do something like:

SELECT sum(capacity) FROM table_name GROUP BY HOUR(time) ;

My Output:

1240
987
1092
1205

but instead I just want 1 row, 1 col:

Expected Output:

1240, 987, 1092, 1205
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Udi Nugroho
  • 1
  • 1
  • 4
  • 1
    Research [GROUP_CONCAT](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat) , pretty sure that is what you are asking here.. – Raymond Nijland Aug 02 '19 at 13:01
  • Your question is a duplicate of [this](https://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field) one. – Angel Politis Aug 02 '19 at 13:02
  • 1
    Consider handling issues of data display in application code - Note that in your example, you have no way of knowing which sum belongs to which hour. – Strawberry Aug 02 '19 at 13:09
  • 1
    to add to @Strawberry 's comment also the results are most likely different between MySQL 5 and MySQL 8 as MySQL 8 has removed implict `GROUP BY` sorting as it was deprecated since MySQL 5.7.. Also meaning that this will result into non deterministic ("random") sorting results on MySQL 8 – Raymond Nijland Aug 02 '19 at 13:16

1 Answers1

0

You could use group concat on your result

  SELECT group_concat(my_sum)
  from  (
    select  sum(capacity) my_sum
    FROM table_name 
    GROUP BY HOUR(time) 
    ORDER BY Hour(time)
  ) t
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Note: MySQL 8 has removed implict `GROUP BY` sorting as it was deprecated since MySQL 5.7.. Also meaning that this will result into non deterministic ("random") sorting results on MySQL 8.. So this might need to have a `ORDER BY` – Raymond Nijland Aug 02 '19 at 13:18
  • Well `ORDER BY Hour(time)` can still cause non deterministic ("random") sorting results if the values "ties" forgot to mention... You will need to add atleast one deterministic column to the `ORDER BY`, meaning the column must have a primary or unique key to get pure 100% deterministic (fixed) result on every run. – Raymond Nijland Aug 02 '19 at 13:23