开发者

In SQL, how find the total of a row over time?

开发者 https://www.devze.com 2023-03-08 00:53 出处:网络
A table and开发者_StackOverflow中文版 I want to know the total of my rows over time. For example.Here\'s my table:

A table and开发者_StackOverflow中文版 I want to know the total of my rows over time. For example. Here's my table:

Date   Fruit   Sold
Mon    apple   4
Mon    pear    5
Mon    orange  2
Tues   apple   3
Tues   pear    2
Tues   orange  1

The table I want back is:
Fruit   Sold
apple   7
pear    7
orange  3

What is a query that I can do this? However, with my real situation, I have hundreds of types of fruit. So how do I query with out specifying each type of fruit each time?


That would be along the lines of:

select fruit, sum(sold) as sold
from fruitsales
group by fruit
-- adding something like <<where date = 'Mon'>> if you want to limit it.

This will aggregate the individual sold columns (by summing) for each fruit type.


here is how to do it:

select fruit, sum(sold)
from table
group by fruit

cheers...


Group by Time

select fruit, sum(sold),substring(saletime,1,3) from table group by fruit,substring(saletime,1,3)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号