SQL Teradata - 在查询中创建新列,如果特定值为真,则将列乘以 2
SQL Teradata - in query create new column that multiplies column by 2 if certain value is true
我有一个 sql 查询,我 运行 导出 2 列,成本和月份。 months 列的值为 6 或 2。我想创建一个新列来检查 months 列并查看值是什么。如果月份值为 6,则将成本列乘以 2,如果月份值为 12,则只需在成本列中复制相同的数字。示例数据:
cost months
100 6
200 12
400 6
expected result:
cost months total
100 6 200
200 12 200
400 6 800
一个简单的 case 语句应该可以工作:
select
cost,
months,
case when months = 6 then cost * 2
else cost
end as total
from <your table>
我有一个 sql 查询,我 运行 导出 2 列,成本和月份。 months 列的值为 6 或 2。我想创建一个新列来检查 months 列并查看值是什么。如果月份值为 6,则将成本列乘以 2,如果月份值为 12,则只需在成本列中复制相同的数字。示例数据:
cost months
100 6
200 12
400 6
expected result:
cost months total
100 6 200
200 12 200
400 6 800
一个简单的 case 语句应该可以工作:
select
cost,
months,
case when months = 6 then cost * 2
else cost
end as total
from <your table>