2014-09-30 12 views
0

我加入兩個表格。然後我在兩張不同的日期期間在一張表格中總結兩列。加入表格,總計在一個字段中,按日期和字段分隔其他

但是我也希望做的是將日期類別進一步分爲兩組字段,這兩個字段由一個表的不同字段上的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數據庫。

+0

您可以將創建和插入語句作爲示例數據發佈到測試用例。 – 2014-09-30 15:29:59

回答

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 
+0

完美地工作!非常感謝。 – branches 2014-09-30 15:29:29

+0

無需測試用例就可以花費很多時間和精力對邏輯進行逆向工程:-( – 2014-09-30 15:30:39

相關問題