如何在同一天列中添加主题而不是多个

时间:2017-05-03 20:21:24

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

我有以下记录。我希望在星期一列中显示所有科目的科目,周一出现开始时间和结束时间。我该怎么做?

SELECT t.teacher_name, tci.class_name, tsb.Subject_Name, tdn.DaysName, 
       tss.subject_start, tss.subject_end
FROM   tblsubjectSchedule tss
INNER JOIN tblsubjects tsb ON tss.subject_id = tsb.Idx
INNER JOIN tblclassinfo tci ON tss.class_id = tci.Idx
INNER JOIN tbldaysnames tdn ON tss.days_id = tdn.Idx 
INNER JOIN tblteacher t ON tss.techer_id = t.Idx
WHERE tss.class_id = 2 AND t.school_id = 1

enter image description here

1 个答案:

答案 0 :(得分:0)

您的附件并不完全清楚。你会在结果上忽略teacher_name吗? 在您的屏幕截图中,您还没有包含' samad teacheer'周一结果。您希望查询仅用于星期一日期吗?

如果你只专注于课程,日期和日期,我就得到了以下内容。请参阅附件。让我知道它是否适合您并接受我的回答。  enter image description here

SELECT t1.class_name,t1.daysname,
       subject_name =REPLACE( (SELECT  (subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
           FROM [practice].[dbo].[test] t2
          WHERE t2.daysname = t1.daysname
          ORDER BY subject_name
            FOR XML PATH('')
            ), ' ', ';')
      FROM [practice].[dbo].[test] t1
      GROUP BY daysname,class_name ;

您还可以将教师名称添加到计划列:请参阅附件 enter image description here

SELECT t1.class_name,t1.daysname,
       schedule =REPLACE( (SELECT  (teacher_name+'-'+subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
           FROM [practice].[dbo].[test] t2
          WHERE t2.daysname = t1.daysname
          ORDER BY subject_name
            FOR XML PATH('')
            ), ' ', ';')
      FROM [practice].[dbo].[test] t1
      GROUP BY daysname,class_name ;

以下是仅针对'星期一'

的查询
SELECT t1.class_name,t1.daysname,
       schedule =REPLACE( (SELECT  (teacher_name+'-'+subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
           FROM [practice].[dbo].[test] t2
          WHERE t2.daysname = t1.daysname
          ORDER BY subject_name
            FOR XML PATH('')
            ), ' ', ';')
      FROM [practice].[dbo].[test] t1 where daysname='Monday'
      GROUP BY daysname,class_name ;