1

我有这样的查询

select distinct
tran_date,
ZONE_NAME,                  
ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2)APC,
ROUND((nvl(SUM(SALES_VALUE),0)+nvl(sum(total_sales),0)-nvl(sum(net_sales),0)+nvl(sum(discount),0))/nvl(sum(BILLS_WITHOUT_CGROUP),0))ASPB,
from OUTLET_PAYMODE_REPORT_FACT A,OUTLET_DETAILS B
WHERE A.OUTLET_ID=B.OUTLET_ID and SALES_VALUE>0
group by zone_name,tran_date

我的问题是我有一个日期字段,例如tran_date 格式===dd-mmm-yyyy我必须按日期字段分隔列值,就像我在下面提到的

例如:

  2012-AUG       ||2013-AUG || 2013-JULY

previous year|| current month || last month

    100       || 200           || 150
    120       || 300           || 500
    etc...
    etc...

如何为此分离列和框架查询?

4

1 回答 1

1

假设tran_date是您想要调整的内容并且它确实是一个日期,您可以执行以下操作:

select zone_name,
       sum(case when tran_date between trunc(add_months(sysdate, -12), 'month') and
                                       trunc(add_months(sysdate, -11), 'month')
                then APC
           end) as PrevYear,
       sum(case when tran_date between trunc(add_months(sysdate, 0), 'month') and
                                       trunc(add_months(sysdate, 0), 'month')
                then APC
           end) as CurerntMonth,
       sum(case when tran_date between trunc(add_months(sysdate, -1), 'month') and
                                       trunc(add_months(sysdate, -1), 'month')
                then APC
           end) as LastMonth
from (select tran_date, ZONE_NAME,
             ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2) as APC,
             ROUND((nvl(SUM(SALES_VALUE),0)+nvl(sum(total_sales),0)- 
                    nvl(sum(net_sales),0)+nvl(sum(discount),0))/nvl(sum(BILLS_WITHOUT_CGROUP),0)
                  ) as ASPB
      from OUTLET_PAYMODE_REPORT_FACT A join
           OUTLET_DETAILS B
           on A.OUTLET_ID=B.OUTLET_ID 
      where SALES_VALUE>0
      group by zone_name,tran_date
     ) t
group by ZONE_NAME;

如果日期确实是字符串格式,则将其转换为日期,然后按照相同的方法进行操作。

于 2013-08-14T10:23:20.480 回答