【问题标题】:SQL divide many records by daySQL 按天划分多条记录
【发布时间】:2019-07-18 10:34:38
【问题描述】:

我的 SQL 表的结构很简单,只包含 3 个字段:

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

表中有 10w+ 条记录!表示一年内插入的记录:

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

那么,我怎么能按天划分记录???

eg:表中有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

【问题讨论】:

  • 你有没有尝试过?日期部分()?
  • @VitalyBorisov 我现在按代码划分!我只想知道 SQL 有什么办法
  • 您使用什么确切的 SQL?
  • 这不能同时是 MySQL 和 SQL Server...

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


【解决方案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 服务器的

【讨论】:

    【解决方案2】:

    尝试使用COUNT by 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多