data example 我目前正试图解决问题。我有一个数据库,其表格为,id为co2_band,纳税年度,税率为。
我目前有这段代码
SELECT tax_year, CO2_BAND AS band,
CASE CO2_BAND = 116
WHEN CO2_band BETWEEN 1 AND 116 THEN'116'
WHEN CO2_band BETWEEN 95 AND 10 THEN 'fdjhgkjds'
ELSE 'here'
END AS b
FROM company_car_tax
WHERE tax_year LIKE '2015-16%'
LIMIT 1
我的问题是在二氧化碳值50-75之间我必须四舍五入到最接近的记录怎么能这样做?
示例数据 所以我有一个装满汽车的数据库和二氧化碳值。 如果我有一辆车,它的二氧化碳值在50-75之间 那么它需要选择75支架
答案 0 :(得分:0)
SELECT tax_year, CO2_BAND AS band,
CASE
WHEN CO2_band BETWEEN 1 AND 116
THEN'116'
WHEN CO2_band BETWEEN 95 AND 10
THEN 'fdjhgkjds'
WHEN CO2_band BETWEEN 50 AND 75
THEN select max(CO2_band) FROM company_car_tax where CO2_band BETWEEN 50 AND 75 group by tax_year
ELSE 'here'
END AS b
FROM company_car_tax
WHERE tax_year LIKE '2015-16%'
LIMIT 1
答案 1 :(得分:0)
我建议您尝试下面显示的代码。请注意CASE
条件的顺序很重要,因此我重新排列顺序以确保可以命中所有条件。我这样做是通过列出从限制最严格到限制最少的条件。但是,如果我自己这样做,我会指定条件,以便值中没有重叠。这将消除任何混淆。
SELECT tax_year, CO2_BAND as band,
CASE WHEN CO2_band BETWEEN 50 AND 75 THEN '75'
WHEN CO2_band BETWEEN 10 AND 95 THEN 'fdjhgkjds'
WHEN CO2_band BETWEEN 1 AND 116 THEN '116'
ELSE 'here' END AS b
FROM company_car_tax
WHERE tax_year LIKE '2015-16%'
LIMIT 1