使用新变量来 CASE WHEN
Use a new variable to CASE WHEN
我有以下 table:
CREATE TABLE my_table
(
the_debt_id varchar(6) NOT NULL,
the_debt_amount int NOT NULL,
the_debt_date date NOT NULL
)
INSERT INTO my_table
VALUES ('LMUS01', '180', '12/2/2019'),
('LMUS01', '200', '11/2/2019'),
('LMUS01', '300', '13/2/2019'),
('LMUS02', '100', '10/2/2019'),
('LMUS02', '150', '12/2/2019')
我想创建一个名为 debt_taxed 的变量,然后创建一个基于 debt_taxed 的变量,在某些限制下将其命名为 "exceed"、"contained" 或“滚动:
SELECT the_debt_amount * 1.18 as debt_taxed,
CASE WHEN the_debt_taxed >= 250 THEN 'Exceed'
WHEN (the_debt_taxed < 250 and the_debt_taxed >= 200) THEN 'contained'
ELSE 'rolling'
END AS status
FROM my_table
但我收到一条错误消息,指出 the_debt_taxed 不存在。
拜托,我们将不胜感激。
只需将 CASE
中的 debt_taxed
替换为实际计算即可。
SELECT
debt_taxed = the_debt_amount * 1.18
,status = CASE
WHEN the_debt_amount * 1.18 >= 250 THEN 'Exceed'
WHEN
(
the_debt_amount * 1.18 < 250
AND the_debt_amount * 1.18 >= 200
) THEN 'contained'
ELSE 'rolling'
END
FROM my_table;
如果您想使用 CTE(常见 table 表达式),您可以这样做:
;WITH CTE AS
(
SELECT
debt_taxed = m.the_debt_amount * 1.18
,m.the_debt_id
,m.the_debt_date
FROM my_table m
)
SELECT
c.debt_taxed
,c.the_debt_id
,c.the_debt_date
,status = CASE
WHEN c.debt_taxed >= 250 THEN 'Exceed'
WHEN
(
c.debt_taxed < 250
AND c.debt_taxed >= 200
) THEN 'contained'
ELSE 'rolling'
END
FROM CTE c;
使用子查询执行此操作:
将 table my_table 替换为包含名为 debt_taxed
的列 the_debt_amount * 1.18 的 table
SELECT debt_taxed,
CASE WHEN the_debt_taxed >= 250 THEN 'Exceed'
WHEN (the_debt_taxed < 250 and the_debt_taxed >= 200) THEN 'contained'
ELSE 'rolling'
END AS status
FROM (SELECT *, the_debt_amount * 1.18 as debt_taxed from my_table) a
我有以下 table:
CREATE TABLE my_table
(
the_debt_id varchar(6) NOT NULL,
the_debt_amount int NOT NULL,
the_debt_date date NOT NULL
)
INSERT INTO my_table
VALUES ('LMUS01', '180', '12/2/2019'),
('LMUS01', '200', '11/2/2019'),
('LMUS01', '300', '13/2/2019'),
('LMUS02', '100', '10/2/2019'),
('LMUS02', '150', '12/2/2019')
我想创建一个名为 debt_taxed 的变量,然后创建一个基于 debt_taxed 的变量,在某些限制下将其命名为 "exceed"、"contained" 或“滚动:
SELECT the_debt_amount * 1.18 as debt_taxed,
CASE WHEN the_debt_taxed >= 250 THEN 'Exceed'
WHEN (the_debt_taxed < 250 and the_debt_taxed >= 200) THEN 'contained'
ELSE 'rolling'
END AS status
FROM my_table
但我收到一条错误消息,指出 the_debt_taxed 不存在。
拜托,我们将不胜感激。
只需将 CASE
中的 debt_taxed
替换为实际计算即可。
SELECT
debt_taxed = the_debt_amount * 1.18
,status = CASE
WHEN the_debt_amount * 1.18 >= 250 THEN 'Exceed'
WHEN
(
the_debt_amount * 1.18 < 250
AND the_debt_amount * 1.18 >= 200
) THEN 'contained'
ELSE 'rolling'
END
FROM my_table;
如果您想使用 CTE(常见 table 表达式),您可以这样做:
;WITH CTE AS
(
SELECT
debt_taxed = m.the_debt_amount * 1.18
,m.the_debt_id
,m.the_debt_date
FROM my_table m
)
SELECT
c.debt_taxed
,c.the_debt_id
,c.the_debt_date
,status = CASE
WHEN c.debt_taxed >= 250 THEN 'Exceed'
WHEN
(
c.debt_taxed < 250
AND c.debt_taxed >= 200
) THEN 'contained'
ELSE 'rolling'
END
FROM CTE c;
使用子查询执行此操作: 将 table my_table 替换为包含名为 debt_taxed
的列 the_debt_amount * 1.18 的 tableSELECT debt_taxed,
CASE WHEN the_debt_taxed >= 250 THEN 'Exceed'
WHEN (the_debt_taxed < 250 and the_debt_taxed >= 200) THEN 'contained'
ELSE 'rolling'
END AS status
FROM (SELECT *, the_debt_amount * 1.18 as debt_taxed from my_table) a