Skip to content
Advertisement

How to filter PostgresSQL query result dependent on value on some column?

I have this query:

SELECT
    time as "time",
    tag as "tag",
    value as "value"
FROM metrics
WHERE 
     tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_32A' OR   tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_63A' OR   tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_80A' OR  tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_125A'
GROUP BY 2, 1, 3
ORDER BY time desc
LIMIT 4

Which results in this:

enter image description here

Instead of returning all the 4 rows, I’d like to return the column which has a value “1”. How can I achieve this?

Advertisement

Answer

If you just want it to show the column with value = 1, you already have the answer. With mi answer you select all the columns with values different to 0.

SELECT
    time as "time",
    tag as "tag",
    value as "value"
FROM metrics
WHERE 
     value != 0 AND (
     tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_32A' OR   tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_63A' OR   tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_80A' OR  tag = 'KEB1.DB_IN.01.STATUS_CMD.A_LIM_125A' )
GROUP BY 2, 1, 3
ORDER BY time desc
LIMIT 4
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement