-2

我在表中有数据,列 ID 和列日期

id  || date
1   || 2013-04-18 15:12:32.000  
2   || 2013-04-18 15:13:02.000  
3   || 2013-05-01 19:09:28.000  
4   || 2013-05-01 19:09:46.000  
5   || 2013-05-01 19:10:01.000  
6   || 2013-05-01 19:10:17.000  
7   || 2013-05-02 08:34:54.000  
8   || 2013-05-02 08:35:12.000  
9   || 2013-05-18 15:12:32.000   
10  || 2013-05-18 15:13:02.000  

如何查询仅显示一天计数?或一行计数。所以结果是。

    || date                    ||
1   || 2013-04-18 15:12:32.000 ||  2
2   || 2013-05-01 19:09:28.000 ||  4
3   || 2013-05-02 08:34:54.000 ||  2
4   || 2013-05-18 15:13:02.000 ||  2

我在 mysql 中尝试,它可以工作,有我的查询。

SELECT id, GROUP_CONCAT( date ) AS date, COUNT( id ) AS count
FROM petak_tetap
GROUP BY DATE( date )

但是当我尝试使用 sql server 2008 时,此查询错误

这是错误消息

Msg 195, Level 15, State 10, Line 1
'GROUP_CONCAT' is not a recognized built-in function name.

如何解决?

4

4 回答 4

3

首先,GROUP_CONCATSQL Server 中没有函数,但更重要的是,您不需要此函数提供的功能。使用聚合函数MIN()MAX()为每组获取一个日期时间值。

SELECT ROW_NUMBER () OVER (ORDER BY [date]) No, [date], [count]
FROM 
(SELECT MIN([date]) 'date', 
        COUNT(*) 'count'
   FROM petak_tetap
  GROUP BY CAST([date] AS DATE)
) t

输出:

| NO |                         DATE | COUNT |
---------------------------------------------
|  1 | April, 18 2013 15:12:32+0000 |     2 |
|  2 |   May, 01 2013 19:09:28+0000 |     4 |
|  3 |   May, 02 2013 08:34:54+0000 |     2 |
|  4 |   May, 18 2013 15:12:32+0000 |     2 |

这是SQLFiddle演示

在 sqlite 中更新

SELECT 
 (
   SELECT COUNT(*) 
     FROM 
   ( SELECT 1
       FROM petak_tetap
      WHERE id <= t.id
      GROUP BY DATE(date)
   ) q
 ) No, date, count
  FROM 
 (
   SELECT id, MIN(date) date, COUNT(*) count
     FROM petak_tetap
    GROUP BY DATE(date)   
 ) t

这是SQLFiddle演示

于 2013-05-30T04:49:10.027 回答
0

尝试这个...

SELECT ROW_NUMBER () OVER (ORDER BY DATE) SNo, DATE, COUNT(DATE)AS Count FROM Table
GROUP BY DATE 
于 2013-05-30T04:41:19.903 回答
0

仅在一个查询中使用完整输出尝试此操作..

SELECT ROW_NUMBER() OVER(Order BY Min(IDate)) srno,  Min(IDate) idate, COUNT(*) Cnt
FROM grpcontact
GROUP BY CONVERT(Varchar,IDate,101)

输出>>>

------------------------------------
srno | idate                   | Cnt
------------------------------------
1    | 2013-04-18 15:12:32.000 | 2
2    | 2013-05-01 19:09:28.000 | 4
3    | 2013-05-02 08:34:54.000 | 2
4    | 2013-05-18 15:12:32.000 | 2
------------------------------------
于 2013-05-30T06:19:01.683 回答
0

尝试这个

select p1.date,
      (select count(p2.date) from petak_tetap p2 where p2.date=p1.date)
from petak_tetap p1 group by p1.date
于 2013-05-30T05:58:08.217 回答