0

这是一个 Oracle SQL 难题。

我有一个问题,我有一张日期/水果/消费量表,我想清理的水果表中偶尔有重复项。例如,我在同一天吃了 3 个苹果和 2 个苹果,我想把它结合起来就说吃了 5 个苹果。

我猜它使用 GROUP BY 函数,但我想不出一种方法来避免对所有苹果进行分组,而不仅仅是在特定日期对所有苹果进行分组。

    Date    Fruit   Eaten
    1-May   Lemon   2
    1-May   Apple   3
    1-May   Apple   2
    1-May   Orange  4
    2-May   Lemon   1
    2-May   Apple   2
    2-May   Orange  3
    2-May   Pear    4

这是我希望它看起来的样子:

   Date     Fruit   Eaten
   1-May    Lemon   2
   1-May    Apple   5
   1-May    Orange  4
   2-May    Lemon   1
   2-May    Apple   2
   2-May    Orange  3
   2-May    Pear    4
4

2 回答 2

2

尝试这个:

select to_char(Date, 'DD-MON') as Date,Fruit,sum(Eaten) as Eaten
from Your_Table
group by to_char(Date, 'DD-MON') ,Fruit

或尝试trunc如下使用:

select trunc(Date, 'MONTH') as Date,Fruit,sum(Eaten) as Eaten
from Your_Table
group by trunc(Date, 'MONTH') ,Fruit
于 2013-01-24T10:32:26.133 回答
1

尝试对多个列进行分组:

select Date,Fruit,sum(Eaten)
from Your_Table
group by Date,Fruit
于 2013-01-24T10:34:15.343 回答