0

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

+-----+-------------------------+-------------------------+-------------------------+-------------------+-------------------+-------------------+
| 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

问候卢卡斯

4

2 回答 2

0

如果您尝试按照建议的方式更新表,则无需进入解决方案:这似乎是糟糕的数据库(重新)设计,imo。

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

您可以在 UI 上对数据进行分组(需要进行一些开发)或通过拆分 course_subjects 和 course_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;
于 2015-07-22T11:18:27.100 回答
0

您希望将 course_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 ;
于 2015-07-22T11:31:42.920 回答