I have a query like this:
SELECT SUM(price) AS GINTO,
(
(SELECT COUNT(price)*9.99 FROM sms_logs WHERE price = '200000')+
(SELECT COUNT(price)*3.99 FROM sms_logs WHERE price = '60000')+
(SELECT COUNT(price)*1.99 FROM sms_logs WHERE price = '24000')+
(SELECT COUNT(price)*0.99 FROM sms_logs WHERE price = '11000')
) AS USD,
DATE_FORMAT(DATE,'%Y-%m-%d') AS DATE FROM sms_logs
WHERE DATE >='2015-03-20' AND DATE <= '2015-04-30'
GROUP BY DATE_FORMAT(DATE,'%Y-%m-%d')
I want show data as:
GINTO || USD || DATE
2222000 || 200 || 2015-03-23
3366000 || 300 || 2015-03-24
11000 || 10 || 2015-03-25
But result with that query:
GINTO || USD || DATE
2222000 || 284.65|| 2015-03-23
3366000 || 284.65|| 2015-03-24
11000 || 284.65|| 2015-03-25
with USD column it shows a summary of all records. Why?
Your count
calls are run in separate queries, with no group by
clause, so they are applied on the entire table, regardless of the "main" query's goruping. One way to solve this would be to get rid of the subqueries and bring them into the "main" query:
SELECT SUM(price) AS GINTO,
SUM(CASE price WHEN '200000' THEN 9.99
WHEN '60000' THEN 3.99
WHEN '24000' THEN 1.99
WHEN '11000' THEN 0.99
END) AS usd,
DATE_FORMAT(date, '%Y-%m-%d') AS date
FROM sms_logs
WHERE date >='2015-03-20' AND date <= '2015-04-30'
GROUP BY DATE_FORMAT(date,'%Y-%m-%d')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With