简体   繁体   中英

group by sql oracle

I have a table with multiple fields, among which are: num1 (INTEGER), name(STRING) and datatime(TIMESTAMP). I would like to get the sample with name, sum(num1) and count(num1) grouped by month. I'm trying to do this:

SELECT name, sum(num1), count(num1) from mytable group by to_char(datatime, 'YYYY-MM')

But it doesn't work. Help me, please, and sorry for my English:)

  1. Sum not sun
  2. Group By not grouped by
  3. all non-aggregates from select must be in the group by so add name .

.

SELECT name, sum(num1), count(num1) 
FROM  mytable 
GROUP BY to_char(datatime, 'YYYY-MM'), name

Generally if you're going to group your data by yyyy-mm you would generally want to see that in the select... but maybe you have your reasons... otherwise how do you tell multiple values of name apart? What's in ()'s are not in your results so... which John is which?

John 123 23  (1990-01)
John 234 44  (1990-02)
John 323 22  (1990-03)

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