1

I have the next query:

SELECT DATE_FORMAT(stats_visits.date,'%H') AS zdate,
   count(DISTINCT stats_visits.id) AS visits,
   count(DISTINCT d.id) AS downloads,
   count(DISTINCT c.id) AS clicks,
   ROUND(SUM(d.revenue),3) AS zrevenue
FROM stats_visits
LEFT JOIN stats_downloads AS d on(DATE_FORMAT(d.date,'%y/%m/%d %H') = DATE_FORMAT(stats_visits.date,'%y/%m/%d %H')
                              AND d.user='4')
LEFT JOIN stats_clicks AS c on(DATE_FORMAT(c.date,'%y/%m/%d %H') = DATE_FORMAT(stats_visits.date,'%y/%m/%d %H')
                           AND c.user='4')
WHERE DATE(stats_visits.date) = '2014-01-31'
  AND stats_visits.user = '4'
GROUP BY zdate
ORDER BY zdate ASC

And it returns a wrong 'zrevenue'

enter image description here

If I delete that "SUM" it gives this response:

enter image description here

It gives one 'revenue', but in table there are 3 rows with 'date'=13 and 'revenue'=0.85.

The 'zrevenue' should be 2.55, not 10.200

The "revenue" is a decimal(8,2) and tables are "InnoDB"

Thanks in advance!

Community
  • 1
  • 1
Farse Creed
  • 33
  • 1
  • 5

0 Answers0