Sum values in a Mysql Count

Asked

Viewed 213 times

0

Good evening, I’m with a doubt I have a system that runs to seguiten query:

SELECT IDUsuario ,COUNT('TotalDeUps') as Total,  FROM uploads
        WHERE Time >= '2018-02-20 00:00:00'
        AND Time < '2018-03-21 00:00:00'
GROUP BY IDUsuario
ORDER BY COUNT("UserID") DESC

I have inside this table uploads a column called Size , I wonder if I can add the values of the size of an ID to return , I thought something like this just didn’t work:

SELECT IDUsuario ,COUNT('TotalDeUps') as Total,SUM(Size) as TotalDeUp  FROM uploads
        WHERE Time >= '2018-02-20 00:00:00'
        AND Time < '2018-03-21 00:00:00'
GROUP BY IDUsuario
ORDER BY COUNT("UserID") DESC

The result was zero in all columns.

  • Maybe you have some record NULL, utilize COALESCE to see if that’s the problem: SUM(COALESCE(Size, 0))

  • It worked, thanks a lot. In case there was no null value in any column the SUM(Size) would work?

  • yes, it would work

  • Put your answer so I give Ok, thank you!

1 answer

0


You probably have some lines with the value NULL in the column size. Utilize COALESCE to consider these values as 0:

...
SUM(COALESCE(Size, 0)) AS TotalDeUp
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.