加入表格,将字段汇总为一个,按日期和其他字段分开

时间:2014-09-30 14:56:36

标签: sql oracle

我正在加入两张桌子。然后我在一个表中对两个不同日期的两列进行求和。

然而,我还想做的是将日期类别进一步分为由一个表的不同字段上的WHERE条件定义的两组字段。

但是我无法得到这个结果,我得到了ORA-00936:缺少表达。

这很可能是一个语法错误,但是我无法按照我想要的多个日期和条件找到一个示例,所以我找不到如何写这个的例子。

我的代码:

select SOME_CATEGORY_1, SOME_CATEGORY_2,
                WHERE (SOME_CATEGORY_3 = 'Value A', 
                sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.cash_sales else 0 end) A_TY_CashSales,
                sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.unit_sales else 0 end) A_TY_UnitSales,
                sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.cash_sales else 0 end) A_LY_CashSales,
                sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.unit_sales else 0 end) A_LY_UnitSales
                ),
                WHERE (SOME_CATEGORY_3 = 'Value B', 
                sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.cash_sales else 0 end) B_TY_CashSales,
                sum(case when oc.week_start_date between SYSDATE-(52*7) and SYSDATE then ms.unit_sales else 0 end) B_TY_UnitSales,
                sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.cash_sales else 0 end) B_LY_CashSales,
                sum(case when oc.week_start_date between SYSDATE-(104*7) and SYSDATE-(53*7) then ms.unit_sales else 0 end) B_LY_UnitSales
                )           
from MY_TABLE mt
join MY_OTHER_TABLE mot on mot.mykey = mt.mykey
join calendar_table ct on ct.week_id = ms.week_id
group by SOME_CATEGORY_1, SOME_CATEGORY_2
order by 1,2,3 desc

结果可以使用两个单独的选择来完成,但是如果可能的话,我希望有一个查询显示这一年和去年在单个查询中对两个案例的总和。

我该如何做到这一点?

作为参考,此查询将应用于Oracle数据库。

1 个答案:

答案 0 :(得分:0)

case语句中添加另一个子句以限制some_category_3例如

select some_category_1
     , some_category_2
     , sum(case
             when     some_category_3 = 'value a'
                  and oc.week_start_date between sysdate-(52*7) and sysdate
               then ms.cash_sales
             else 0
           end) a_ty_cashsales
     , sum(case
             when     some_category_3 = 'value a'
                  and oc.week_start_date between sysdate-(52*7) and sysdate
               then ms.unit_sales
             else 0
           end) a_ty_unitsales
     , sum(case
             when     some_category_3 = 'value a'
                  and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
               then ms.cash_sales
             else 0
           end) a_ly_cashsales
     , sum(case
             when     some_category_3 = 'value a'
                  and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
               then ms.unit_sales
             else 0
           end) a_ly_unitsales
     , sum(case
             when     some_category_3 = 'value b'
                  and oc.week_start_date between sysdate-(52*7) and sysdate
               then ms.cash_sales
             else 0
           end) b_ty_cashsales
     , sum(case
             when     some_category_3 = 'value b'
                  and oc.week_start_date between sysdate-(52*7) and sysdate
               then ms.unit_sales
             else 0
           end) b_ty_unitsales
     , sum(case
             when     some_category_3 = 'value b'
                  and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
               then ms.cash_sales
             else 0
           end) b_ly_cashsales
     , sum(case
             when     some_category_3 = 'value b'
                  and oc.week_start_date between sysdate-(104*7) and sysdate-(53*7)
               then ms.unit_sales
             else 0
           end) b_ly_unitsales
from my_table mt
     join my_other_table mot
       on mot.mykey = mt.mykey
     join calendar_table ct
       on ct.week_id = ms.week_id
group by some_category_1
       , some_category_2
order by some_category_1
       , some_category_2