简体   繁体   English

SQL 查询按时间分组并汇总和连接字符串值

[英]SQL Query to group by time and roll up and concatenate string values

I am trying to get a particular format from a group of times and days between two tables.我正在尝试从两个表之间的一组时间和天数中获取特定格式。

Database: MeetingTime table has a relationship from MeetingTime.DayOfWeekId (foreign key) to table DayOfWeek.Id (Primary Key).数据库:MeetingTime 表具有从 MeetingTime.DayOfWeekId(外键)到表 DayOfWeek.Id(主键)的关系。 Example Query:示例查询:

select t.ClassId, d.Name, t.StartTime, t.EndTime
From MeetingTime t
Inner Join DaysOfWeek d on d.Id = t.DayOfWeekId
Where t.classId = 8

Results:结果:

在此处输入图像描述

My desired results for this set of data would be one row, because the start and end times are the same.我对这组数据的期望结果将是一行,因为开始时间和结束时间是相同的。

09:00-15:35 M/T/W/Th/F 09:00-15:35 M/T/W/Th/F

NOTE, the start and end time above, can be separate columns above, the main goal is display the days of the week for each grouped time.注意,上面的开始和结束时间可以是上面的单独列,主要目标是显示每个分组时间的星期几。

The monkey wrench is that the times can be completely different or the same.猴子扳手是时代可以完全不同或相同。 For example this data set:例如这个数据集: 在此处输入图像描述

I would want displayed in 2 rows:我希望显示为 2 行:

07:35-14:15 M/T/W 07:35-14:15 M/T/W

08:00-14:15 Th/F 08:00-14:15 周四

And finally, this dataset where all times are different:最后,这个数据集的所有时间都不同:

在此处输入图像描述

Would display in 5 rows:将显示为 5 行:

13:48-14:48 M 13:48-14:48 米

15:48-16:48 T 15:48-16:48 时间

05:49-23:53 W 05:49-23:53 西

14:49-16:49 Th 14:49-16:49

13:49-16:49 F 13:49-16:49 楼

I haven't had much success with grouping the times.我在分组时间方面没有取得多大成功。 I did figure out how to concatenate the days of the week rolling the days up into one column using the 'Stuff' Operator, but didn't get anywhere with the grouping of the start and end time coupled with this yet.我确实想出了如何使用“Stuff”运算符将一周中的天数串联到一列中,但是在开始和结束时间的分组以及这一点上还没有得到任何结果。

Concatenating and rolling up days:连接和汇总天数:

   STUFF((SELECT '/ ' + 
      (CASE
            WHEN d.[Name] = 'Thursday' THEN SUBSTRING(d.[Name], 1, 2)
            WHEN d.[Name] = 'Sunday' THEN 'U'
            WHEN d.[Name] != '' THEN SUBSTRING(d.[Name], 1, 1)
            ELSE NULL
        END)
      FROM MeetingTime m
        Inner Join [DayOfWeek] d on d.Id = m.DayOfWeekId
        Where m.ClassId = class.Id
      FOR XML PATH('')), 1, 1, '') [ClassSchedule]

I'm also not opposed to just returning the rows and handling the data manipulation in C# code, but wanted to see if SQL could handle it.我也不反对只返回行并在 C# 代码中处理数据操作,但想看看 SQL 是否可以处理它。

I was able to get this working.我能够得到这个工作。 Here is the query:这是查询:

select 
t.ClassId, 
t.StartTime, 
t.EndTime,
STUFF((SELECT '/' + (CASE
                        WHEN w.[Name] = 'Thursday' THEN SUBSTRING(w.[Name], 1, 2)
                        WHEN w.[Name] = 'Sunday' THEN 'U'
                        WHEN w.[Name] != '' THEN SUBSTRING(w.[Name], 1, 1)
                        ELSE NULL
                    END)
      From MeetingTime s
      Inner Join DayOfWeek w on w.Id = s.DayOfWeekId
      Where s.classId = 7 and s.DayOfWeekId > 0 
      and s.StartTime = t.StartTime 
      and s.EndTime = t.EndTime
      FOR XML PATH('')), 1, 1, '') [ClassSchedule]
From MeetingTime t
Inner Join DayOfWeek d on d.Id = t.DayOfWeekId
Where t.classId = 7 and t.DayOfWeekId > 0
Group by t.StartTime, t.EndTime, t.ClassId

Obviously hardcoded Id you would want to create a variable.显然,您想要创建一个变量的硬编码 Id。

Results where the start and end time are all the same:开始时间和结束时间都相同的结果: 在此处输入图像描述

Some times the same and some different:有时相同,有时不同: 在此处输入图像描述

Some times the same and some different with days not in order:有时相同,有时不同,日期不按顺序排列: 在此处输入图像描述

Times all different:时代不同:

在此处输入图像描述

Times with only Mon/Wed/Fri.只有周一/周三/周五的时间。

在此处输入图像描述

I feel pretty good about this, except I'd like to fix the order of the above result image where all times are different and the days are not in chronological order.我对此感觉很好,除了我想修复上述结果图像的顺序,其中所有时间都不同并且日子不是按时间顺序排列的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM