基于相同的日期时间合并Sql行

时间:2015-07-22 10:55:41

标签: sql merge sql-server-2008-r2 sql-merge

我正在努力完成以下任务。我有一张这样的桌子:

+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+-------------------+
| Id  |      Assigned Date      |   Assigned Starttime    |    Assigned Endtime     | lesson_subject_id | lesson_teacher_id | lesson_classes_id |
+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+-------------------+
| 116 | 2016-03-03 00:00:00.000 | 1900-01-01 13:15:00.000 | 1900-01-01 14:00:00.000 | SU_SK             | Adlberger         | CL_HEB 15/I B     |
| 112 | 2016-03-03 00:00:00.000 | 1900-01-01 13:15:00.000 | 1900-01-01 14:00:00.000 | SU_SK             | Jakob             | CL_HEB 15/I A     |
| 113 | 2016-03-03 00:00:00.000 | 1900-01-01 14:15:00.000 | 1900-01-01 15:00:00.000 | SU_SK             | Jakob             | CL_HEB 15/I A     |
| 117 | 2016-03-03 00:00:00.000 | 1900-01-01 14:15:00.000 | 1900-01-01 15:00:00.000 | SU_SK             | Adlberger         | CL_HEB 15/I B     |
| 118 | 2016-03-03 00:00:00.000 | 1900-01-01 15:15:00.000 | 1900-01-01 16:00:00.000 | SU_SK             | Adlberger         | CL_HEB 15/I B     |
| 114 | 2016-03-03 00:00:00.000 | 1900-01-01 15:15:00.000 | 1900-01-01 16:00:00.000 | SU_SK             | Jakob             | CL_HEB 15/I A     |
| 115 | 2016-03-03 00:00:00.000 | 1900-01-01 16:15:00.000 | 1900-01-01 17:00:00.000 | SU_SK             | Jakob             | CL_HEB 15/I A     |
| 119 | 2016-03-03 00:00:00.000 | 1900-01-01 16:15:00.000 | 1900-01-01 17:00:00.000 | SU_SK             | Adlberger         | CL_HEB 15/I B     |
+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+-------------------+

正如您所看到的,有些条目同时开始和结束。

我需要做的是将单元格合并到一起并删除不必要的行。

结果看起来像这样

+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+----------------------------------+
| Id  |      Assigned Date      |   Assigned Starttime    |    Assigned Endtime     | lesson_subject_id | lesson_teacher_id |         lesson_classes_id        |
+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+----------------------------------+
| 116 | 2016-03-03 00:00:00.000 | 1900-01-01 13:15:00.000 | 1900-01-01 14:00:00.000 | SU_SK             | Adlberger, Jakob  | CL_HEB 15/I A, CL_HEB 15/I B     |
| 113 | 2016-03-03 00:00:00.000 | 1900-01-01 14:15:00.000 | 1900-01-01 15:00:00.000 | SU_SK             | Adlberger, Jakob  | CL_HEB 15/I A, CL_HEB 15/I B     |
| 118 | 2016-03-03 00:00:00.000 | 1900-01-01 15:15:00.000 | 1900-01-01 16:00:00.000 | SU_SK             | Adlberger, Jakob  | CL_HEB 15/I A, CL_HEB 15/I B     |
| 115 | 2016-03-03 00:00:00.000 | 1900-01-01 16:15:00.000 | 1900-01-01 17:00:00.000 | SU_SK             | Adlberger, Jakob  | CL_HEB 15/I A, CL_HEB 15/I B     |
+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+----------------------------------+

我是如何实现这一目标的?我在Sql Server 2008 R2上

关心卢卡斯

2 个答案:

答案 0 :(得分:0)

如果你没有进入解决方案,如果你正在尝试像你建议的那样更新你的表:这似乎是糟糕的数据库(重新)设计,imo。

为什么要这样做是第一个问题?

您可以在UI上对数据进行分组(需要进行一些开发),或者通过拆分lesson_subjects和lesson_classes / teachers表并在它们之间建立关系来更改设计。 对不起,这可能不是您需要的答案:)

编辑:我看到其他人也有类似的问题,所以我很想找到答案。 在oracle中做到这一点的一种方法(建立在Kibadachi的答案之上):

SELECT  MIN(ID),
        ASSIGNED_DATE , 
        ASSIGNED_STARTTIME,
        ASSIGNED_STARTTIME, 
        LESSON_SUBJECT_ID,
        LISTAGG(TRIM(LESSON_TEACH_ID) , ',') WITHIN GROUP (ORDER BY LESSON_TEACH_ID ) AS LESSON_TEACH_ID,
        LISTAGG(TRIM(LESSON_CLASS_ID) , ',') WITHIN GROUP (ORDER BY LESSON_CLASS_ID ) AS LESSON_CLASS_ID 
FROM ATABLE
GROUP BY ASSIGNED_DATE, 
        ASSIGNED_STARTTIME,
        ASSIGNED_STARTTIME, 
        LESSON_SUBJECT_ID;

答案 1 :(得分:0)

您希望将lesson_teacher_id列连接起来: 计划A:

SELECT  assignedDate , 
        assignedStarttime , 
        assignedEndtime , 
        lesson_subject_id , 
        lesson_classes_id , 
        LISTAGG(lesson_teacher_id , ',')WITHIN GROUP (ORDER BY lesson_teacher_id ) AS lesson_teacher_id 
FROM myTable 
GROUP BY assignedDate,
         assignedStarttime ,
         assignedEndtime ;

计划B:

SELECT  assignedDate , 
            assignedStarttime , 
            assignedEndtime , 
            lesson_subject_id , 
            lesson_classes_id , 
            wm_concat(lesson_teacher_id , ',')WITHIN GROUP (ORDER BY lesson_teacher_id ) AS lesson_teacher_id 
    FROM myTable 
    GROUP BY assignedDate,
             assignedStarttime ,
             assignedEndtime ;