如何生成周表并按天和小时拆分

Posted

技术标签:

【中文标题】如何生成周表并按天和小时拆分【英文标题】:How to Generate a Week Table and Split by Day and Hour 【发布时间】:2015-11-30 13:23:42 【问题描述】:

首先,要明确;有一个这样的例子:

我可以通过这个查询获得课程:

$qb
    ->select("c")
    ->from("ATLReservationBundle:Course", "c")
    ->where("WEEK(c.courseDate, 1) = WEEK(CURRENT_DATE(), 1)")
    ->orderBy("c.courseDate", "ASC");

查询结果:

   "courses": [
    
      "id": 110,
      "name": "Pilates",
      "quota": 8,
      "course_date": "2015-08-31T12:15:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 135,
      "name": "Spinning",
      "quota": 8,
      "course_date": "2015-08-31T19:30:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 154,
      "name": "Kick Box",
      "quota": 6,
      "course_date": "2015-08-31T20:00:00+0000",
      "studio": 
        "id": 1,
        "name": "Stüdyo 1"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 133,
      "name": "Spinning",
      "quota": 8,
      "course_date": "2015-09-01T12:15:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 113,
      "name": "Crunch 30 Min.",
      "quota": 8,
      "course_date": "2015-09-01T18:30:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 112,
      "name": "Pilates",
      "quota": 8,
      "course_date": "2015-09-01T19:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 150,
      "name": "Spinning",
      "quota": 8,
      "course_date": "2015-09-01T19:30:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 136,
      "name": "TRX Group",
      "quota": 5,
      "course_date": "2015-09-01T20:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 159,
      "name": "Pilates",
      "quota": 8,
      "course_date": "2015-09-02T10:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 156,
      "name": "Stretching",
      "quota": 7,
      "course_date": "2015-09-02T18:30:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 127,
      "name": "Zumba",
      "quota": 6,
      "course_date": "2015-09-02T19:00:00+0000",
      "studio": 
        "id": 1,
        "name": "Stüdyo 1"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 148,
      "name": "Kick Box",
      "quota": 6,
      "course_date": "2015-09-02T20:00:00+0000",
      "studio": 
        "id": 1,
        "name": "Stüdyo 1"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 138,
      "name": "Crunch 30 Min.",
      "quota": 8,
      "course_date": "2015-09-03T12:15:00+0000",
      "studio": 
        "id": 1,
        "name": "Stüdyo 1"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 121,
      "name": "Pilates",
      "quota": 8,
      "course_date": "2015-09-03T19:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 117,
      "name": "TRX",
      "quota": 8,
      "course_date": "2015-09-03T20:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 131,
      "name": "Spinning",
      "quota": 8,
      "course_date": "2015-09-03T20:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 142,
      "name": "Pilates",
      "quota": 8,
      "course_date": "2015-09-04T12:15:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 147,
      "name": "LBT 30 min.",
      "quota": 8,
      "course_date": "2015-09-04T19:00:00+0000",
      "studio": 
        "id": 1,
        "name": "Stüdyo 1"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 141,
      "name": "Crunch 30 Min.",
      "quota": 8,
      "course_date": "2015-09-04T19:30:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
    ,
    
      "id": 130,
      "name": "Crunch 30 Min.",
      "quota": 8,
      "course_date": "2015-09-05T14:00:00+0000",
      "studio": 
        "id": 2,
        "name": "Stüdyo 2"
      ,
      "trainer": 
        "id": 1,
        "name": "-"
      
       ] 

但我想将这些数据分组;

DAY - 31
    HOUR - 12:15 > Coursename
    HOUR - 19:30 > Coursename
DAY - 01
    HOUR - 10:00...

我不知道该怎么做,有人吗?

更新

新查询:

   $qb
        ->select("c, HOUR(c.courseDate) as hour, DAY(c.courseDate) as day")
        ->from("ATLReservationBundle:Course", "c")
        ->where("WEEK(c.courseDate, 1) = WEEK(CURRENT_DATE(), 1)")
        ->orderBy("c.courseDate", "ASC")
        ->groupBy("hour, day");

但是;此查询存在问题,我无法按HOUR(c.courseDate), DAY(c.courseDate) 分组,因为我收到此错误:

错误:无法按未定义的标识或结果变量分组。

所以,我在选择区域中将小时和天定义为变量。 我不想将日期和时间视为不同的变量。

查询结果:


  "courses": [
    
      "0": 
        "id": 110,
        "name": "Pilates",
        "quota": 8,
        "course_date": "2015-08-31T12:15:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "12",
      "day": "31"
    ,
    
      "0": 
        "id": 135,
        "name": "Spinning",
        "quota": 8,
        "course_date": "2015-08-31T19:30:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "19",
      "day": "31"
    ,
    
      "0": 
        "id": 154,
        "name": "Kick Box",
        "quota": 6,
        "course_date": "2015-08-31T20:00:00+0000",
        "studio": 
          "id": 1,
          "name": "Stüdyo 1"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "20",
      "day": "31"
    ,
    
      "0": 
        "id": 133,
        "name": "Spinning",
        "quota": 8,
        "course_date": "2015-09-01T12:15:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "12",
      "day": "1"
    ,
    
      "0": 
        "id": 113,
        "name": "Crunch 30 Min.",
        "quota": 8,
        "course_date": "2015-09-01T18:30:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "18",
      "day": "1"
    ,
    
      "0": 
        "id": 112,
        "name": "Pilates",
        "quota": 8,
        "course_date": "2015-09-01T19:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "19",
      "day": "1"
    ,
    
      "0": 
        "id": 136,
        "name": "TRX Group",
        "quota": 5,
        "course_date": "2015-09-01T20:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "20",
      "day": "1"
    ,
    
      "0": 
        "id": 159,
        "name": "Pilates",
        "quota": 8,
        "course_date": "2015-09-02T10:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "10",
      "day": "2"
    ,
    
      "0": 
        "id": 156,
        "name": "Stretching",
        "quota": 7,
        "course_date": "2015-09-02T18:30:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "18",
      "day": "2"
    ,
    
      "0": 
        "id": 127,
        "name": "Zumba",
        "quota": 6,
        "course_date": "2015-09-02T19:00:00+0000",
        "studio": 
          "id": 1,
          "name": "Stüdyo 1"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "19",
      "day": "2"
    ,
    
      "0": 
        "id": 148,
        "name": "Kick Box",
        "quota": 6,
        "course_date": "2015-09-02T20:00:00+0000",
        "studio": 
          "id": 1,
          "name": "Stüdyo 1"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "20",
      "day": "2"
    ,
    
      "0": 
        "id": 138,
        "name": "Crunch 30 Min.",
        "quota": 8,
        "course_date": "2015-09-03T12:15:00+0000",
        "studio": 
          "id": 1,
          "name": "Stüdyo 1"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "12",
      "day": "3"
    ,
    
      "0": 
        "id": 121,
        "name": "Pilates",
        "quota": 8,
        "course_date": "2015-09-03T19:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "19",
      "day": "3"
    ,
    
      "0": 
        "id": 117,
        "name": "TRX",
        "quota": 8,
        "course_date": "2015-09-03T20:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "20",
      "day": "3"
    ,
    
      "0": 
        "id": 142,
        "name": "Pilates",
        "quota": 8,
        "course_date": "2015-09-04T12:15:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "12",
      "day": "4"
    ,
    
      "0": 
        "id": 141,
        "name": "Crunch 30 Min.",
        "quota": 8,
        "course_date": "2015-09-04T19:30:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "19",
      "day": "4"
    ,
    
      "0": 
        "id": 130,
        "name": "Crunch 30 Min.",
        "quota": 8,
        "course_date": "2015-09-05T14:00:00+0000",
        "studio": 
          "id": 2,
          "name": "Stüdyo 2"
        ,
        "trainer": 
          "id": 1,
          "name": "-"
        
      ,
      "hour": "14",
      "day": "5"
    
  ]

我想我现在很接近了。但一定是这样的;

DAY
    HOUR
    HOUR
DAY
    HOUR
    HOUR

【问题讨论】:

您可以使用分组方式 我试过按小时和天分组。修复错误后会看到。 你能给我提供一些数据的表架构吗 更新了我的问题。 使用HOUR(c.courseDate) 代替hourday 相同 【参考方案1】:

要获得可视化示例表,请使用:

工作日(日期)

返回日期的工作日索引(0 = 星期一,1 = 星期二,... 6 = 周日

select hour(date), -- group by hour
case when weekday(date)=0 then course_name else null end as Monday, -- 1 col for each weekday
.....
case when weekday(date)=6 then course_name else null end as Sunday

from table 
where -- select a monday to sunday calendar week unique by year 
date(date) - interval weekday(date)=date(curdate()) - interval weekday(curdate())
group by hour(date)

要获得您提到的其他变体:

select right(date, 2), -- day of month,
hour(date) --hour,
course_name
from table
group by 1,2

【讨论】:

以上是关于如何生成周表并按天和小时拆分的主要内容,如果未能解决你的问题,请参考以下文章

Linux查看文件总的数据行数,并按行拆分

如何动态生成表并对行进行分组

如何连接多个表,包括查找表并按行返回数据

如何基于 ManyToManyField 内部连接表并按参数分组并在 Django 中获取最新的表?

如何在mysql程序中创建临时表并生成不同的名称?

从 SQL 数据库导入表并按日期过滤行时,将 Pandas 列解析为 Datetime