如何使用case子句创建sql请求

时间:2017-08-09 15:23:02

标签: mysql

我根据sql request更改了widget value is selected。 我根据我的问题enter link description here更正了此请求 这是根据我的要求:

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类似我的数据来源进入Palantir Cloud。 你能告诉我哪里是我的错吗?

1 个答案:

答案 0 :(得分: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)