如何使用CASE执行SQL请求

时间:2017-08-09 11:48:09

标签: mysql

我尝试使用SQL request条款来case,但始终会返回此错误:

ERROR: syntax error at or near "CASE"

我的要求如下:

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

from "foundry_sync"."data"

    CASE month
  WHEN "month" =1 THEN 
    select avg("Montant_fac_eur") as c1 where "Facturation" between 1 and 6

 when "month" =2 THEN 
 select avg("Montant_fac_eur") as c2 where "Facturation" between 2 and 7

 when "month" =3 THEN
 select avg("Montant_fac_eur") as c3 where "Facturation" between 3 and 8

 when "month" = 4 then 
 select avg("Montant_fac_eur") as c4 where "Facturation" between 4 and 9

 when "month"=5 THEN
 select avg("Montant_fac_eur") as c5 where "Facturation" between 5 and 10

 when "month"=6 THEN 
 select avg("Montant_fac_eur") as c6 where "Facturation" between 6 and 11

 when "month"=7 THEN
 select avg("Montant_fac_eur") as c7 where "Facturation" between 7 and 11

 else ''

END 

group by "LRU", "Client", "Facturation"
order by "Client", "month"

我正在使用MySQLFacturationdate

有人可以告诉我,我的错在哪里? 谢谢。

2 个答案:

答案 0 :(得分:2)

有太多错误,很难知道从哪里开始。

你混淆了两种形式的CASE表达式。一种形式是:

CASE <expression>
    WHEN <value> THEN <result>
    WHEN <value> THEN <result>
    ...
END

另一个是:

CASE
    WHEN <condition> THEN <result>
    WHEN <condition> THEN <result>
    ...
END

您尝试使用SELECT查询作为值,但它缺少FROM子句,您必须在括号中包装查询以将其用作值。我怀疑你想要从同一个表中查询,在这种情况下你不应该做一个子查询,你应该只使用主查询中的聚合函数。

CASE表达式应该是SELECT列表的一部分,而不是FROM子句之后。

如果要在输出中为每种情况创建单独的列,则它们不能位于一个CASE表达式中。

您的所有表名和列名都用双引号,MySQL使用反引号来引用名称。

使用SELECT DISTINCT时,您不需要GROUP BY

SELECTGROUP BYORDER BY外,您无法在同一查询的HAVING列表中引用别名。

应该是:

SELECT MONTH(Facturation) AS month, LRU, Client,
    AVG(CASE WHEN MONTH(Factuation) = 1 AND Facturation BETWEEN 1 AND 6
        THEN Montant_fac_eur END) AS c1,
    AVG(CASE WHEN MONTH(Factuation) = 2 AND Facturation BETWEEN 2 AND 7
        THEN Montant_fac_eur END) AS c2,
    AVG(CASE WHEN MONTH(Factuation) = 3 AND Facturation BETWEEN 3 AND 8
        THEN Montant_fac_eur END) AS c3,
    AVG(CASE WHEN MONTH(Factuation) = 4 AND Facturation BETWEEN 4 AND 9
        THEN Montant_fac_eur END) AS c4,
    ...
FROM foundry_sync.data
GROUP BY `LRU`, `Client`, `Facturation`
ORDER BY Client, month

答案 1 :(得分:1)

您必须在FROM之前设置CASE。我已经取消了所有的“,你需要在每个选择语句之后使用brakets,我会取消GROUP BY和ORDER BY子句。正如有人所说,你需要在每个选择中使用FROM子句

SELECT distinct extract(month from Facturation) as month, LRU, Client,
    CASE month
  WHEN month =1 THEN 
    (select avg(Montant_fac_eur) as c1 where Facturation between 1 and 6)

 when month =2 THEN 
    (select avg(Montant_fac_eur) as c2 where Facturation between 2 and 7)

 when month =3 THEN
    (select avg(Montant_fac_eur) as c3 where Facturation between 3 and 8)

 when month = 4 then 
    (select avg(Montant_fac_eur) as c4 where Facturation between 4 and 9)

 when month=5 THEN
    (select avg(Montant_fac_eur) as c5 where Facturation between 5 and 10)

 when month=6 THEN 
    (select avg(Montant_fac_eur) as c6 where Facturation between 6 and 11)

 when month=7 THEN
    (select avg(Montant_fac_eur) as c7 where Facturation between 7 and 11)

 else ''

END 

from foundry_sync.data
group by LRU, Client, Facturation
order by Client, month