Postgres获取列组中至少有一个特定值的行由另一列

时间:2017-02-08 08:08:24

标签: sql postgresql

我有一个sql命令:

select location,status,total from my_table

此回报:

location - status - total
city a - active - 3
city a - pending - 2
city a - cancel - 4
city b - pending - 4
city b - cancel - 4
city c - active - 2
city c - cancel - 6

我如何只返回城市中至少有一次状态有效并且总> = 1的行?

所以,我想回来:

    location - status - total
    city a - active - 3
    city a - pending - 2
    city a - cancel - 4
    city c - active - 2
    city c - cancel - 6

正如您所注意到我不想返回城市b因为它没有至少有一次活动状态且总计> = 1。

1 个答案:

答案 0 :(得分:0)

select location,status,total from my_table
where location in (
    select location from my_table
    where status = 'active'
        and total >= 1
    )