2017-08-09 9 views
0

我更改了我的sql request根據哪個widget value is selected。 我糾正根據我的問題這一要求enter link description here 這下我的要求:如何使用case子句創建sql請求

SELECT "LRU", "Client", extract(month from "Facturation") as mt 


CASE {{w_widget6.selectedValue}} 

        WHEN {{w_widget6.selectedValue}}=1 THEN 
         (select avg("Montant_fac_eur") as c1 from "foundry_sync"."data" where "month" between 1 and 6) 

       when {{w_widget6.selectedValue}} =2 THEN 
         (select avg("Montant_fac_eur") as c2 from "foundry_sync"."data" where "month" between 2 and 7) 

       when {{w_widget6.selectedValue}} =3 THEN 
         (select avg("Montant_fac_eur") as c3 from "foundry_sync"."data" where "month" between 3 and 8) 

       when {{w_widget6.selectedValue}} = 4 THEN 
         (select avg("Montant_fac_eur") as c4 from "foundry_sync"."data" where "month" between 4 and 9) 

       when {{w_widget6.selectedValue}} =5 THEN 
         (select avg("Montant_fac_eur") as c5 from "foundry_sync"."data" where "month" between 5 and 10) 

       when {{w_widget6.selectedValue}} =6 THEN 
         (select avg("Montant_fac_eur") as c6 from "foundry_sync"."data" where "month" between 6 and 11) 

       when {{w_widget6.selectedValue}} =7 THEN 
         (select avg("Montant_fac_eur") as c7 from "foundry_sync"."data" where "month" between 7 and 12) 

END; 

from "foundry_sync"."data" 


group by "LRU", "Client" 

但始終我有同樣的錯誤:

ERROR: syntax error at or near "CASE" 
    Position: 68 

我使用foundry-postgate像我的數據源到真知晶球雲。 你能告訴我我的錯在哪?

+1

如果我們認爲這是您的確切SQL ...山後缺少逗號。選擇...'提取(從「Facturation」月份)作爲mt,' – xQbert

+0

我現在加了它,它返回這個錯誤:錯誤:列「LRU」不存在 位置:8 – vero

+0

可能的[如何做SQL請求使用CASE](https://stackoverflow.com/questions/45589804/how-to-do-an-sql-request-using-case) – yanman1234

回答

1

個人而言,我會重構整個查詢:被w_widget6.selectedvalue在

  • month傳遞

    • 假設一個變量是foundry_Sync.data列
    • 只需要在使用反引號保留字。
    • 月需要在組中。

    SELECT LRU 
        , Client 
        , extract(month from Facturation) as mt 
        , avg("Montant_fac_eur") AVG_MONT_FAC_EUR 
    FROM foundry_sync.data FSD 
    WHERE `month` between 0+{{w_widget6.selectedValue}} and 5+{{w_widget6.selectedValue}} 
    GROUP BY LRU, Client, extract(month from Facturation) 
    
  • +0

    xQbert謝謝你的非常好的答案,我正在考慮這個解決方案,每增加一次+5來恢復上個月。明天當我到辦公室時我會測試它。真的你是一個「邏輯問題解決者」 – vero

    +1

    有時候人們想要問題的答案(它只解決問題的症狀)有時人們想要問題的答案。我猜測重構可能會解決問題而不是症狀。我是一個很棒的ITIL/ITSM(IT服務管理)粉絲。修復您不必修復事件的問題。 – xQbert

    +0

    真的,我沒有別的詞來添加它們!向你致敬 – vero