多行到一行。获取所有日子而不是那些匹配课程的日子。以下是结果

时间:2018-05-09 14:05:24

标签: sql sql-server tsql

select distinct
       dbo.Courses.DisplayName AS CourseInternalName,
       dbo.courses.CourseID, 
       stuff((select distinct ','+ (case when CourseSchedules.dayid = 1 then 'Mon'
                                         when CourseSchedules.dayid = 2 then 'Tue'
                                         when CourseSchedules.dayid = 3 then 'Wed' 
                                         when CourseSchedules.dayid = 4 then 'Thur'
                                         when CourseSchedules.dayid = 5 then 'Fri'
                                         when CourseSchedules.dayid = 6 then 'Sat'
                                         when CourseSchedules.dayid = 7 then 'Sun'
                                     end) 
               FROM  dbo.Courses 
               left outer join dbo.CourseSchedules 
                    on dbo.Courses.CourseGUID = dbo.CourseSchedules.CourseGUID
               for xml path('')),1,1,'') AS Days

from  dbo.Courses 
left outer join dbo.CourseSchedules 
     on dbo.Courses.CourseGUID = dbo.CourseSchedules.CourseGUID
Where courses.CourseID = 5815

结果是:

CourseInternalName  CourseID    Days
Red Cross Sunfish - CC  5815    Mon,Fri,Wed,Tue,Thur,Sat

这是一周中的所有日子,而不仅仅是课程运行的日子。

还需要订购天。

如果我在Stuff语句中删除了from子句,我会得到正确的日子,但是会有多行。

2 个答案:

答案 0 :(得分:0)

您的左连接是多余的,导致您的查询返回意外结果。

请改为尝试:

首先,创建并填充样本表(在将来的问题中保存此步骤):

CREATE TABLE Courses
(
    CourseID int,
    DisplayName varchar(10),
    CourseGUID uniqueidentifier DEFAULT(NEWID())
)

CREATE TABLE CourseSchedules
(
    dayid int,
    CourseGUID uniqueidentifier
)

INSERT INTO Courses(CourseID, DisplayName)
VALUES (5815, 'Test')

INSERT INTO CourseSchedules
SELECT 1, CourseGUID FROM Courses UNION
SELECT 3, CourseGUID FROM Courses UNION
SELECT 5, CourseGUID FROM Courses 

查询:

SELECT DISTINCT
       dbo.Courses.DisplayName AS CourseInternalName,
       dbo.courses.CourseID, 
       STUFF((SELECT ','+ CASE WHEN dayid = 1 THEN 'Mon'
                               WHEN dayid = 2 THEN 'Tue'
                               WHEN dayid = 3 THEN 'Wed' 
                               WHEN dayid = 4 THEN 'Thur'
                               WHEN dayid = 5 THEN 'Fri'
                               WHEN dayid = 6 THEN 'Sat'
                               WHEN dayid = 7 THEN 'Sun'
                          END
               FROM  dbo.CourseSchedules 
               WHERE dbo.CourseSchedules.CourseGUID = dbo.Courses.CourseGUID
               ORDER BY dayid
               FOR XML PATH('')),1,1,'') AS Days
FROM  dbo.Courses 
WHERE courses.CourseID = 5815

结果:

CourseInternalName  CourseID    Days
Test                5815        Mon,Wed,Fri

答案 1 :(得分:0)

问题来自你的子查询,你没有告诉它只选择一门课程,你要告诉它选择任何与课程相匹配的课程。修正如下。实际上,当子查询完全分离时,不需要第一次连接

select distinct
       dbo.Courses.DisplayName AS CourseInternalName,
       dbo.courses.CourseID, 
       stuff((select distinct ','+ (case when CourseSchedules.dayid = 1 then 'Mon'
                                         when CourseSchedules.dayid = 2 then 'Tue'
                                         when CourseSchedules.dayid = 3 then 'Wed' 
                                         when CourseSchedules.dayid = 4 then 'Thur'
                                         when CourseSchedules.dayid = 5 then 'Fri'
                                         when CourseSchedules.dayid = 6 then 'Sat'
                                         when CourseSchedules.dayid = 7 then 'Sun'
                                     end) 
               FROM  dbo.CourseSchedules
               Where CourseSchedules.CourseID = 5815
               ORDER BY CourseSchedules.dayid ASC
               for xml path('')),1,1,'') AS Days

from  dbo.Courses 
Where courses.CourseID = 5815