Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Sum Multiple values for that day

Here is my table

TIME                     Status
2012-11-15 8:30:00.000   "WAS_FLOW"
2012-11-15 9:00:00.000   "WAS_FLOW"
2012-11-15 10:30:00.000  "H2_FLOW"
2012-11-11 12:14:00.00   "O23HZ_FLOW"
2012-11-11 8:00.00.000   "AZ_FLOW"
2012-11-12 9:00.000      "BZ_FLOW"

I want my results to show:

TIME         "WAS FLOW"    
2012-11-11   0
2012-11-12   0
2012-11-15   2
like image 322
user1836442 Avatar asked Dec 11 '25 07:12

user1836442


2 Answers

Other answers here all had slight glitches. Here is one that I hope will work straight out of the box for you.

SELECT
  DATEADD(DAY, DATEDIFF(DAY, 0, [time]), 0)             AS [date],
  SUM(CASE WHEN status = 'WAS_FLOW' THEN 1 ELSE 0 END)  AS [count]
FROM
  yourTable
GROUP BY
  DATEADD(DAY, DATEDIFF(DAY, 0, [time]), 0)
like image 156
MatBailie Avatar answered Dec 12 '25 19:12

MatBailie


SELECT CAST(TIME AS date) AS TIME,
       COUNT(CASE WHEN Status = 'WAS_FLOW' THEN Status END) AS 'WAS_FLOW'
FROM dbo.your_table
GROUP BY CAST(TIME AS date)

Demo on SQLFiddle

like image 24
Aleksandr Fedorenko Avatar answered Dec 12 '25 19:12

Aleksandr Fedorenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!