【发布时间】:2018-01-10 13:51:03
【问题描述】:
我正在尝试将数量手风琴调整为年度或每月状态。
这是 Y = 每年和 M = 每月的列。
period_stats | Amount
Y | 12070.00
M | 2580.00
我想根据金额查询金额,如果period_stats是Y,那么金额应该除以12,如果period_stats是M,那么想得到不除的直接金额。
我不是 sql 方面的专家,所以经过一番搜索后我发现我可以做到 在 sql 中使用
case但我不知道如何使用它。
我已经尝试过这种方式。如果我用错了,请纠正我。
select period_stats, CASE period_stats
WHEN 'Y' THEN (amount / 12) as amount_monthly
ELSE amount END as 'amount_monthly' FROM tbale where id = 1;
如果有人知道任何其他技术,那么将不胜感激,我也想知道这一点。
结论:
正确查询以获得所需的结果。 (通过使用 Sebastian Brosch 的答案)
SELECT
period_stats,
ROUND(CASE WHEN (period_stats = 'Y') THEN (amount / 12) ELSE amount END, 2)
AS 'amount_monthly'
FROM table
WHERE id = 1;
结果:
amount_monthly
1005
【问题讨论】: