0

これはOracleSQLの難問です。

クリーンアップしたいフルーツテーブルに時折重複がある場合に、消費された日付/フルーツ/数量のテーブルがあるという問題があります。たとえば、同じ日に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 に答える