【发布时间】:2017-02-04 21:21:31
【问题描述】:
我通常会在 java 中使用流行的 if-else 语句,但在 sqlplus 中我使用 select 语句中的 case 来查询我在下面的条件语句。
select title, to_char(nvl2(((retail-cost)/cost)*100,
((retail-cost)/cost)*100,
((retail-cost)/cost)*100), '99999.99') "Margin",
to_char(discount, '99999.99') "Discount",
(case when ("Margin" >= 60) then 'Very High Profit'
when ("Margin" >= 30) then 'High Profit'
else ("Margin" >= 0) then 'Loss Leader'
end) "Pricing Structure"
from books
order by title;
我希望得到这样的结果,但我试图移动排序;我仍然每次都遇到错误。
TITLE Margin Discount Pricing Structure
------------------------------ -------- --------- ---------------------------------
BIG BEAR AND LITTLE DOVE 68.23 Very high profit
BODYBUILD IN 10 MINUTES A DAY 65.07 Very high profit
【问题讨论】:
-
这是错误的做法——难以编写、阅读和维护。相反,应该有一个小表,其中包含每个“定价结构”的阈值及其描述;只计算查询中的边距并加入这个小表。这样您就可以非常轻松地添加或删除级别、更改阈值和/或更改描述。