将列值与 CASE WHEN 失败时的常量进行比较

问题描述 投票:0回答:1

MySQL 8.0.40

以下代码完全按照预期工作。 我创建了两个 CTE,每个返回两列。 然后,我将 2022 年 1 月列的值与 2021 年 1 月列的值进行比较。 如果 2022 年 1 月更大, 创建一个名为“2022_is_greater”的新列并填充 1,否则填充 0。

但是,如果我尝试过滤新创建的“2022_is_greater”列,则不起作用。

ON
 Pr_2021 = Pr_2022
WHERE
 '2022_is_greater' = 1;

附加到最终结果返回零行(应为 18)。

为了笑我也尝试过:

ON
 Pr_2021 = Pr_2022
WHERE
 '2022_is_greater' > x;

其中“x”是任意整数,0 或更大,它返回所有 18 行。 (事实证明,每一行都是 2022 年 1 月 值较大)。 但是对于任何大于 0 的 x 值,这应该返回零行。

当我通过 WHERE 子句添加比较时,我也得到了这个:

截断不正确的 DOUBLE 值:'2022_is_greater'

我查了一下,但我不明白为什么我会得到这个。 我只是检查列中的值是否为 1。 我没有执行字符串比较。

在最后没有 WHERE 子句的情况下正确运行完整查询:

WITH Jan2021 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2021,
     pr.Product as Pr_2021
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2021-01-01' AND '2021-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2021 DESC
),
Jan2022 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2022,
     pr.Product as Pr_2022
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2022-01-01' AND '2022-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2022 DESC
)
SELECT
 SUM_Jan_2021,
 SUM_Jan_2022,
 Pr_2021,
 Pr_2022,
 CASE WHEN SUM_Jan_2022 > SUM_Jan_2021 THEN 1
      ELSE 0
 END AS "2022_is_greater"
FROM
 Jan2021 AS J21
INNER JOIN
 Jan2022 AS J22
ON
 Pr_2021 = Pr_2022;

enter image description here

mysql where-clause
1个回答
0
投票

您可以进一步制作 CTE 并将其用作基础

WITH Jan2021 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2021,
     pr.Product as Pr_2021
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2021-01-01' AND '2021-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2021 DESC
),
Jan2022 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2022,
     pr.Product as Pr_2022
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2022-01-01' AND '2022-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2022 DESC
), S_UM  as (
SELECT
 SUM_Jan_2021,
 SUM_Jan_2022,
 Pr_2021,
 Pr_2022,
 CASE WHEN SUM_Jan_2022 > SUM_Jan_2021 THEN 1
      ELSE 0
 END AS "2022_is_greater"
FROM
 Jan2021 AS J21
INNER JOIN
 Jan2022 AS J22
ON
 Pr_2021 = Pr_2022)
SELECT
 SUM_Jan_2021,
 SUM_Jan_2022,
 Pr_2021,
 Pr_2022
 FROM
 S_UM
 WHERE 2022_is_greater = 1;

或者您可以使用

HAVING

WITH Jan2021 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2021,
     pr.Product as Pr_2021
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2021-01-01' AND '2021-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2021 DESC
),
Jan2022 AS (
    SELECT
     SUM(s.Amount) as SUM_Jan_2022,
     pr.Product as Pr_2022
    FROM
     sales AS s
    INNER JOIN
     products AS pr
    ON
     pr.PID = s.PID 
      AND DATE(s.SaleDate) BETWEEN '2022-01-01' AND '2022-01-31' AND pr.Category != 'Other'
    GROUP BY
     pr.Product
    ORDER BY
     SUM_Jan_2022 DESC
)
SELECT
 SUM_Jan_2021,
 SUM_Jan_2022,
 Pr_2021,
 Pr_2022,
 CASE WHEN SUM_Jan_2022 > SUM_Jan_2021 THEN 1
      ELSE 0
 END AS "2022_is_greater"
FROM
 Jan2021 AS J21
INNER JOIN
 Jan2022 AS J22
ON
 Pr_2021 = Pr_2022
 HAVING  2022_is_greater = 1;
© www.soinside.com 2019 - 2024. All rights reserved.