通过选择案例查询的操作来排序

时间:2015-09-19 12:06:03

标签: postgresql

我希望按多列和每个点的总和对表进行排名。

例如,我可以通过以下查询获得一些列和每个点。

select 
      (case when seller=true then 50 else 0 end) as sel, 
      (case when buyer=true then 40 else 0 end) as buy 
from company;

但我无法通过此查询

按此值排序
select 
      (case when seller=true then 50 else 0 end) as sel, 
      (case when buyer=true then 40 else 0 end) as buy 
from company 
order by (sel + by);

或者

select 
      (case when seller=true then 50 else 0 end) as sel, 
      (case when buyer=true then 40 else 0 end) as buy,
      (sell, buy) as sm
from company
order by sm;

我该怎么做?

1 个答案:

答案 0 :(得分:1)

哦,对不起,我找到了答案。

select * from 
    (select 
        (case when seller=true then 50 else 0 end) as sel, 
        (case when buyer=true then 40 else 0 end) as buy 
    from company) as tmp 
order by (tmp.sel + tmp.buy);