Oracle SQL - Sum and group data by week

前端 未结 3 1693
时光说笑
时光说笑 2020-12-10 01:50

I have records related to dates:

DATE         AMOUNT
16.03.2013   3
16.03.2013   4
16.03.2013   1
16.03.2013   3
17.03.2013   4
17.03.2014   3
3条回答
  •  暖寄归人
    2020-12-10 02:16

    You can use TRUNC function to truncate date to the first day of week. There are a few ways of defining week. For example, if you want to treat that the first day of week is Monday, you can IW format, like this:

    select trunc(date, 'IW') week, sum(amount)
    from YourTable
    group by trunc(date, 'IW');
    

    You can also TO_CHAR function as the "@Vignesh Kumer"'s answer.

    The point is that you should truncate the date in the same week into one value. Then group by the value. That's it.

提交回复
热议问题