如何选择COUNT DISTINCT?

时间:2013-06-23 06:45:08

标签: sql sql-server-2008 select distinct-values

我有选择COUNT的问题:我需要的答案是6。 时间差异是<计数为300Sec为1。 我的sql:

SELECT COUNT(DISTINCT(cast([Date] as date) + cast([time] as datetime)))
FROM [Table1]WHERE [DATE] = '2013-06-22'
AND ErrCode = 'Scrubber Failure'AND Frequency = 1

1 个答案:

答案 0 :(得分:1)

<强>已更新

SELECT COUNT(DISTINCT 
             ROUND(CONVERT(DECIMAL(20, 4), 
                     DATEDIFF(minute, 
                              CONVERT(datetime, '2013-01-01 00:00:00', 120), 
                              CONVERT(datetime, date + ' ' + time, 120))) / 5, 0)) n
  FROM table1
 WHERE date = '2013-06-22'
   AND errcode = 'Scrubber Failure'
   AND frequency = 1

输出:

| N |
-----
| 6 |

这是 SQLFiddle 演示。