Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL query where i aggregate count based its status

user enabled_app app_id
h@gmail.com active 2553
h@gmail.com inactive 2553
h@gmail.com waiting 2553
h@gmail.com active 2554
b@gmail.com active 2555
b@gmail.com waiting 2555
b@gmail.com active 2556

I have a above table, and expected output would be

email count
h@gmail.com 1
b@gmail.com 2

Let me explain we are displaying each user has how many enabled app which are active state.

So lets take the first email id h@gmail.com which returns 1 because this user has 2 app_id associated with it. 2553 and 2554. now in 2553 we wont consider because although it has active it also has an inactive value in it. But for app_id 2554 we have an active enabled_app but there is no inactive value for it. Hence h@gmail.com has count as 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

similarly b@gmail.com has 2 active apps of different app_id.

>Solution :

SELECT
    [user], COUNT([app_id])
FROM
    YourTable
WHERE
    enabled_app = 'active'
    AND [app_id] NOT IN (SELECT [app_id] FROM YourTable WHERE enabled_app = 'inactive')
GROUP BY
    [user]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading