3

这是我的桌子

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
4

3 回答 3

1
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)

SQLFiddle上的演示

于 2012-11-19T18:54:51.470 回答
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)
于 2012-11-19T17:09:42.060 回答
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;
于 2012-11-19T17:09:27.267 回答