简体   繁体   English

Oracle SQL查询每月汇总数据

[英]Oracle sql query sum data every month

Let's say i have selected data from oktober until desember like this 假设我从oktober到12月这样选择了数据

在此处输入图片说明

i want to get sum of buying customer in every months (okt-des) The result i want is like table below 我想获得每个月购买客户的总金额(okt-des)我想要的结果如下表所示

在此处输入图片说明

i already know how to get last day of months 我已经知道如何获得几个月的最后一天
but i don't have idea query in SQL to get result like i need 但是我没有在SQL中进行想法查询来获得所需的结果

One way to do this - if you just need the data for those three months - is to use conditional aggregation: 一种方法是-如果您只需要这三个月的数据-使用条件汇总:

select   name,
         sum(case when dt >= date '2017-10-01' and dt < date '2017-11-01'
                  then buying end) as oktober,
         sum(case when dt >= date '2017-11-01' and dt < date '2017-12-01'
                  then buying end) as november,
         sum(case when dt >= date '2017-12-01' and dt < date '2018-01-01'
                  then buying end) as desember
from     YOUR_TABLE
where    dt >= date '2017-10-01' and dt < date '2018-01-01'
group by name
;

Note that date is an Oracle keyword which should not be used as a column name; 注意date是一个Oracle关键字,不应用作列名。 I changed it to dt . 我将其更改为dt YOUR_TABLE should be your actual table name. YOUR_TABLE应该是您的实际表名。

Try this 尝试这个

 DESC TABLE_NAME
        NAME    VARCHAR2(20 BYTE)
        BUYING  NUMBER
        BUYING_DATE DATE

    select * from
    (
    select name,buying,RTRIM(to_char(buying_Date,'Month')) dd
    from
    TABLE_NAME
    )
    PIVOT
    (
    SUM(buying)
    for dd IN ('October','November','December')
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM