Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aggregate function to return true if value exists

I've got some SQL query which returns the sum of certain fields, and there's a flag column associated with these values, either 'Y' or 'N'.

Is there an aggregate function which will return a true/false on this flag column if any one record contains a 'Y'?

like image 314
atamata Avatar asked Oct 25 '25 05:10

atamata


2 Answers

You could wrap conditional aggregation inside a CASE expression:

CASE
    WHEN COUNT(CASE WHEN column = 'Y' THEN 1 END) > 0 THEN 'true'
    ELSE 'false'
END
like image 70
Salman A Avatar answered Oct 26 '25 20:10

Salman A


select nvl(max(1),0) from your_table
where column = 'Y'
like image 21
t v Avatar answered Oct 26 '25 20:10

t v