1、有如下数据:
A | B | C |
---|---|---|
0 | 0 | 0 |
0 | 97.06 | 0 |
0 | 330.72 | 330.72 |
0 | 0 | 6 |
9.65 | 0 | 0 |
27.88 | 27.85 | 0 |
86.92 | 0 | 86.92 |
219.42 | 219.4 | 219.41 |
需要展示为如下形式:
A | B | C | 结果列 |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 97.06 | 0 | 97.06 |
0 | 330.72 | 330.72 | 330.72 |
0 | 0 | 6 | 6 |
9.65 | 0 | 0 | 9.65 |
27.88 | 27.85 | 0 | 27.88 |
86.92 | 0 | 86.92 | 86.92 |
219.42 | 219.4 | 219.41 | 219.42 |
解决办法:
sql
一、创建表
CREATE TABLE t1 (
A DECIMAL(10, 2),
B DECIMAL(10, 2),
C DECIMAL(10, 2)
);
二、插入数据
INSERT INTO t1 (A, B, C) VALUES
(0, 0, 0),
(0, 97.06, 0),
(0, 330.72, 330.72),
(0, 0, 6),
(9.65, 0, 0),
(27.88, 27.85, 0),
(86.92, 0, 86.92),
(219.42, 219.4, 219.41);
最终结果:
sql
第一种解决办法:
SELECT
A,
B,
C,
GREATEST(A, B, C) AS 结果列
FROM
t1;
第二种解决办法:
SELECT
A,
B,
C,
CASE
WHEN A >= B AND A >= C THEN A
WHEN B >= A AND B >= C THEN B
ELSE C
END AS 结果列
FROM
t1;