【问题标题】:How to frame query for this?如何为此构建查询?
【发布时间】:2013-08-14 18:23:20
【问题描述】:

我有这样的查询

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 with format===dd-mmm-yyyy 我必须按日期字段分隔列值,就像我在下面提到的那样 p>

例如:

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

previous year|| current month || last month

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

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

【问题讨论】:

  • 是mysql还是sqlserver??
  • @shaikibrahim OP 已将该问题标记为 Oracle

标签: sql oracle


【解决方案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;

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

【讨论】:

  • 有没有其他方法可以做到这一点..?
猜你喜欢
  • 2021-10-26
  • 1970-01-01
  • 2021-11-09
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多