简体   繁体   English

案例表达式不返回Postgres查询中的“ else”值

[英]Case expression does not return the “else” value in Postgres query

I'm creating a SQL query with a lot of nested queries and I'm trying to use the CASE expression but it is behaving weirdly. 我正在创建带有很多嵌套查询的SQL查询,并且尝试使用CASE表达式,但是它表现得很奇怪。

This is my query at the moment: 这是我目前的查询:

select t.fpl_id, t.team_name,
sum(pf.points)as gwpts,
(
    select sum(transfers_malus)
    from gameweeks
    where gameweeks.team_id = t.id and gameweeks.number = g.number
)as malus,
(
    select sum(points)
    from player_fixtures as pfix
    where gw_number = g.number and pfix.player_id = CASE WHEN minutes_played > 0 THEN g.captain_id ELSE g.vice_captain_id END
)
as cpts,
 (
    select max(web_name)
    from players
    join player_fixtures on players.id = player_fixtures.player_id and gw_number = g.number
    where players.id = CASE WHEN player_fixtures.minutes_played > 0 THEN g.captain_id ELSE g.vice_captain_id END
) as captain_name,
array_agg(p.id) as lineup
from teams as t
join gameweeks as g on t.id  = g.team_id
join gameweeks_players as gp on gp.gameweek_id = g.id
join players as p on gp.player_id = p.id
join player_fixtures as pf on p.id = pf.player_id  and pf.gw_number = g.number
where t.id = 1
group by t.id, g.id
order by g.number asc

The bit where I have a problem is when I'm doing the case statement to see if one of the player ( the captain ) hasn't played based on "minutes_played": 我有问题的地方是当我在做case语句时,根据“ minutes_played”来查看一名球员(队长)是否没有参加比赛:

(select sum(points)
    from player_fixtures as pfix
    where gw_number = g.number and pfix.player_id = CASE WHEN minutes_played > 0 THEN g.captain_id ELSE g.vice_captain_id END
) as cpts

and

 (
    select max(web_name)
    from players
    join player_fixtures on players.id = player_fixtures.player_id and gw_number = g.number
    where players.id = CASE WHEN player_fixtures.minutes_played > 0 THEN g.captain_id ELSE g.vice_captain_id END
) as captain_name,

When minutes_played is indeed greater than 0, everything works fine. 当minutes_played确实大于0时,一切正常。 However, when it is equal to 0 it just doesn't "return" anything and I get a NULL in return in my row. 但是,当它等于0时,它只是不“返回”任何东西,并且我在行中返回NULL。 minutes_played is an integer and when I try with the following script on the player_fixtures where minutes_played is = 0 my > evaluation works and I get the correct "BAD". minutes_played是一个整数,当我在players_fixtures上尝试以下脚本,其中minutes_played = 0时,我的评估工作正常,并且我得到了正确的“ BAD”。

DO LANGUAGE plpgsql $$
    BEGIN
        IF (select minutes_played from player_fixtures where gw_number = 19 and player_id = 266 ) > 0 THEN 
            RAISE NOTICE 'GOOD';
        ELSE
            RAISE NOTICE 'BAD';
        END IF;
    END;
$$;

I'm pretty new at this whole DB thing so I'm probably making a rookie mistake but I've been trying for the past 6hours without any luck. 我在整个数据库领域都还很新,所以我可能犯了一个菜鸟错误,但是过去6个小时我一直在尝试,没有运气。 Could someone point me in the right direction? 有人可以指出我正确的方向吗?

The problem is that when there are no rows that match, sum() returns a null, and no comparison with null is ever true. 问题在于,当没有匹配的行时, sum()返回null, 并且没有与null的比较永远是真的。

Instead, use exists() , for example: 而是使用exists() ,例如:

where exists(select * from player_fixtures
    where gw_number = 19
    and player_id = 266 
    and minutes_played > 0)

or you can use if exists(...) then in a stored proc. 或者您可以在存储的过程中使用if exists(...) then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM