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

What is the best approach to do this condition in SQL if user has a multiple activity and I only want to get a specific status

Status will only show :

  • Done – if there is atleast 1 completed

-In Progress –if there is no any completed but atleast 1 in progress

  • Hold – if all activities is in hold

Sample Data :

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

MyTable (TableName)

StatusColumn (ColumnName)

Done

In Progress

Hold

I’m thinking to use the CASE() function but I am quite confused since I’m still a beginner in SQL , or if there’s any other approach that you guys can suggest. Thanks.

>Solution :

You can do it using Case Expresssion in Google BigQuery as :

SELECT 
  CASE
    WHEN SUM(CASE WHEN StatusColumn = 'Done' THEN 1 ELSE 0 END) > 0 THEN 'Done'
    WHEN SUM(CASE WHEN StatusColumn = 'In Progress' THEN 1 ELSE 0 END) > 0 THEN 'In Progress'
    ELSE 'Hold'
  END AS Status
FROM MyTable;

You can also do it using COUNTIF function too.

SELECT 
  CASE
    WHEN COUNTIF(StatusColumn = 'Done') > 0 THEN 'Done'
    WHEN COUNTIF(StatusColumn = 'In Progress') > 0 THEN 'In Progress'
    ELSE 'Hold'
  END AS Status
FROM MyTable
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