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

Need help in rewriting the query

The original query is given below:

SELECT
     id
    ,COUNT(A_NO) AS count
FROM table1
WHERE date = '2022-02-02'
AND p_no IS NOT NULL
GROUP BY id; 

I need to rewrite the above query by not using p_no IS NOT NULL condition in the WHERE clause. So I tried the below 2 queries but they are taking long time to execute

SELECT
     id
    ,SUM(CASE WHEN A_NO IS NOT NULL AND p_no IS NOT NULL THEN 1 ELSE 0 END) AS count
FROM table1
WHERE date = '2022-02-02'
GROUP BY id;
    
    
SELECT
     id
    ,COUNT_IF(A_NO IS NOT NULL AND p_no IS NOT NULL) AS count
FROM table1
WHERE date = '2022-02-02'
GROUP BY id;

Is there any other way to rewrite the SQL query?

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

>Solution :

Your COUNT_IF seem pretty good.

Order of next steps:

  • If DATE is not a DATE/TIMESTAMP and is a VARCHAR/TEXT then store it as a real date.
  • Order/Cluster you table/data by DATE
  • Make the warehouse bigger.
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