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

How to subtract count of two columns in MYSQL

I am wanting to get the difference of count value of two columns in same table so far i come up with this code

select (select count( user_id)
        from weekly_customer_RFM_TABLE
        where SEGMENT = 'champion'
          and SEGMENTED_DATE = '2022-10-14'
            -
                               (select
                                    count( user_id)
                                    from weekly_customer_RFM_TABLE
                                    where SEGMENT = 'champion'
                                    and SEGMENTED_DATE = '2022-10-07')) as total_changes

i am getting result 0 here but the original result will be 45
I cannot figure out what i am missing here. Please help me to solve the problem. Those data are in same table but filter by segment and dates

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 :

We can use conditional aggregation here with a single pass over the weekly_customer_RFM_TABLE table:

SELECT SUM(SEGMENTED_DATE = '2022-10-14') -
       SUM(SEGMENTED_DATE = '2022-10-07') AS total_changes
FROM weekly_customer_RFM_TABLE
WHERE SEGMENT = 'champion';
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