其他解决方案比情况时

时间:2016-12-14 19:19:17

标签: sql case-when

我有一个firebird数据库和这个查询:

select distinct
 case
  when exists (select * from fixvar
               where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to) and costcenter_id=10)
  then (select fix from fixvar where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to) and costcenter_id=10)
  else (select fix from fixvar where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to))
 end as fix,
 case
  when exists (select * from fixvar
               where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to) and costcenter_id=10)
  then (select var from fixvar where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to) and costcenter_id=10)
  else (select var from fixvar where costtype_id=7 and extract(year from ct_from)<=2017 and
               2017<=extract(year from ct_to) and costcenter_id=10)
 end as var
from fixvar

我得到了正确的结果,但我可以做得更短吗?

我想要的是:我想查询字段fixvar。如果有一行costcenter_id10,那么我想要有相应的fixvar值,如果没有,那么我们要避开的值不是costcenter_id( - 1)。

编辑:

示例数据:

enter image description here

如您所见,有两行。所以,如果costcenter_id=10存在一行,那么我想要appr。 fixvar值,如果此costcenter_id没有行,那么我想要fixvar值,其中costcenter_id=-1

1 个答案:

答案 0 :(得分:0)

我想,我有解决方案:

$('#prev').click(function () {
    pageNumber--;
    showPage(pageNumber);
    if($('#more').prop('disabled')) {
        $('#more').prop('disabled', false);
    }
});

$('#more').click(function () {
    pageNumber++;
    showPage(pageNumber);
    if($('#prev').prop('disabled')) {
        $('#prev').prop('disabled', false);
    }
});