SQL Sum当天的多个值

时间:2012-11-19 17:03:20

标签: sql sql-server sql-server-2008

这是我的表

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"

我希望我的结果显示:

TIME         "WAS FLOW"    
2012-11-11   0
2012-11-12   0
2012-11-15   2

2 个答案:

答案 0 :(得分:1)

这里的其他答案都有轻微的故障。这是我希望能为您直接开箱即用的

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)

答案 1 :(得分:0)

尝试以下:

   SELECT DISTINCT  CONVERT(VARCHAR(10), a.TIME, 101),
          CASE b.COUNT WHEN NULL THEN 0 ELSE b.COUNT END AS "WAS FLOW"
   FROM MyTable AS a 
       LEFT JOIN 
            (SELECT Count(STATUS) AS "COUNT"
             FROM MyTable
             WHERE Status = 'WAS_FLOW'
             GROUP BY Time
            ) AS b
   ON a.TIME= b.TIME;