SQL按天划分许多记录

时间:2019-02-25 03:34:03

标签: mysql sql-server postgresql sqlite core-data

我的SQL表的结构非常简单,仅包含3个字段:

createDate(Date): time when record inserted;
title(String): title for record;
count(Integer32): count for record;

表中有10个以上的记录!表示一年内插入的记录:

  • 每天可以插入任意数量的记录(包括0条记录)

所以,我如何按天划分记录??

例如:表中有10条记录:

1. 2019-01-01 10:20:15 xxx
2. 2019-01-01 12:50:10 xxx
3. 2019-01-01 23:20:19 xxx

4. 2019-01-02 10:20:15 xxx

5. 2019-01-05 08:20:15 xxx
6. 2019-01-05 22:20:15 xxx

7. 2019-02-10 10:20:15 xxx
8. 2019-02-10 11:20:15 xxx
9. 2019-02-10 15:20:15 xxx

10. 2019-02-15 10:20:15 xxx

我想要结果:分为5个“集合”

收藏“ 2019-01-01”(包含3条记录):

- 2019-01-01 10:20:15 xxx
- 2019-01-01 12:50:10 xxx
- 2019-01-01 23:20:19 xxx

收藏“ 2019-01-02”(包含1条记录):

- 2019-01-02 10:20:15 xxx

收藏“ 2019-01-05”(包含2条记录):

- 2019-01-05 08:20:15 xxx
- 2019-01-05 22:20:15 xxx

收藏“ 2019-02-10”(包含3条记录):

- 2019-02-10 10:20:15 xxx
- 2019-02-10 11:20:15 xxx
- 2019-02-10 15:20:15 xxx

收藏“ 2019-02-15”(包含1条记录):

- 2019-02-15 10:20:15 xxx

2 个答案:

答案 0 :(得分:1)

如果我的表模式正确,那么这可能是您可能的解决方案。

    GO

    CREATE TABLE #tempRequestForMeList   
    (   
         createDate datetime,
         title nvarchar(50),
         [count] int
    )  

    GO

    insert into #tempRequestForMeList ( createDate, title, [count] )
    values ( '2016-09-20 17:17:04.840', 'dd', 0 )
    , ( '2016-09-20 17:17:04.840', 'dd', 1 )
    , ( '2016-09-20 07:17:04.840', 'dd', 1 )
    , ( '2016-09-20 05:17:04.840', 'dd', 1 )
    , ( '2016-09-20 13:17:04.840', 'dd', 1 )
    , ( '2016-09-19 12:17:04.840', 'dd', 1 )
    , ( '2016-09-19 02:17:04.840', 'dd', 1 )
    , ( '2016-09-19 01:17:04.840', 'dd', 1 )
    , ( '2016-09-18 02:17:04.840', 'dd', 1 )
    , ( '2016-09-18 03:17:04.840', 'dd', 1 )
    , ( '2016-09-18 05:17:04.840', 'dd', 1 )
    , ( '2016-09-18 07:17:04.840', 'dd', 1 )

    GO
    ; with cte as (
    select cast(createdate as date) as Date1, * from #tempRequestForMeList )
    update dd set dd.[count] = ct.co from #tempRequestForMeList as dd inner join (select count(date1) as co, date1 from cte group by Date1) as ct on cast(dd.createDate as DATE) = ct.Date1

    select * from #tempRequestForMeList  --- if require count with each row

    go

    drop table #tempRequestForMeList
    go

如果这不起作用,请显示您的表架构和预期的输出。

注意:这是用于SQL Server

答案 1 :(得分:1)

尝试通过COUNT使用PARTITION

SELECT 
t.*
, count( CONVERT(date, t.createDate)) OVER (PARTITION BY CONVERT(date, t.createDate) 
    ORDER BY CONVERT(date, t.createDate)) CountByDate    
FROM 
@tempRequestForMeList t

让我展示一个示例(感谢@DarkRob提供示例数据):

DECLARE @tempRequestForMeList TABLE
(
    createDate DATETIME,
    title NVARCHAR(50),
    [count] INT
);



INSERT INTO @tempRequestForMeList
(
    createDate,
    title,
    count
)
VALUES
('2016-09-20 17:17:04.840', 'dd', 0),
('2016-09-20 17:17:04.840', 'dd', 1),
('2016-09-20 07:17:04.840', 'dd', 1),
('2016-09-20 05:17:04.840', 'dd', 1),
('2016-09-20 13:17:04.840', 'dd', 1),
('2016-09-19 12:17:04.840', 'dd', 1),
('2016-09-19 02:17:04.840', 'dd', 1),
('2016-09-19 01:17:04.840', 'dd', 1),
('2016-09-18 02:17:04.840', 'dd', 1),
('2016-09-18 03:17:04.840', 'dd', 1),
('2016-09-18 05:17:04.840', 'dd', 1),
('2016-09-18 07:17:04.840', 'dd', 1),
('2016-10-20 17:17:04.840', 'dd', 0);

并查询:

SELECT 
t.*
, count( CONVERT(date, t.createDate)) OVER (PARTITION BY CONVERT(date, t.createDate) 
    ORDER BY CONVERT(date, t.createDate)) CountByDate    
FROM 
@tempRequestForMeList t

输出:

createDate                 title    count   CountByDate
2016-09-18 02:17:04.840     dd        1        4
2016-09-18 03:17:04.840     dd        1        4
2016-09-18 05:17:04.840     dd        1        4
2016-09-18 07:17:04.840     dd        1        4
2016-09-19 12:17:04.840     dd        1        3
2016-09-19 02:17:04.840     dd        1        3
2016-09-19 01:17:04.840     dd        1        3
2016-09-20 17:17:04.840     dd        0        5
2016-09-20 17:17:04.840     dd        1        5
2016-09-20 07:17:04.840     dd        1        5
2016-09-20 05:17:04.840     dd        1        5
2016-09-20 13:17:04.840     dd        1        5
2016-10-20 17:17:04.840     dd        0        1