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

Calculate the increase in new positive cases in SQL

Currently I have a table called test:

id test_date result
1 2021-11-25 positive
2 2021-11-25 positive
3 2021-11-25 positive
4 2021-11-26 negative
5 2021-11-26 positive

How could I get the increase in new positive cases on 2021-11-26 compared to 2021-11-25. The result should show a single number indicating the increment. If there are few new positive cases than yesterday, this number should be negative.

Expected results:

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

increment
-2

explanation: 1 – 3 = -2

>Solution :

Use conditional aggregation.

SELECT SUM(test_date = '2021-11-26') - SUM(test_date = '2021-11-25') AS increment
FROM table
WHERE test_date IN ('2021-11-25', '2021-11-26')
  AND result = 'positive'
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