2016-02-11 40 views
-2

我正在運行一個比賽,列出學校教師觀看的所有視頻,但是如果他們觀看的視頻之一是「幹」視頻,我希望他們的名字被列出兩次,以便他們可以放入圖紙兩次。
基本上我有一個case when子句,所以如果一個視頻在標題中包含單詞'stem',我表格的最後一列就會說'Stem'else'0'。在最後一欄說乾的情況下,我想重複整行。我希望這是有道理的。這是我到目前爲止;試圖添加一個重複的行

SELECT * 
FROM (
SELECT District, 
     SchoolName, 
     PersonnelId, 
     Name, 
     EmailAddress, 
     BeganViewingDate, 
     CASE WHEN SecondsCompleted >= SeventyFivePercent 
      THEN ContentName 
      ELSE '0' END as 'ContentName', 
     SegmentLengthInSeconds, 
     SecondsCompleted, 
     CASE WHEN contentname LIKE '%Stem%' 
      THEN 'Stem' 
      ELSE '0' END As 'Stem' 
FROM (

SELECT vp.PersonnelId, 
     cp.EmailAddress, 
     CONCAT(cp.LastName, ', ', cp.FirstName) as Name, 
     vp.BeganViewingDate, 
     vp.SecondsCompleted, 
     c.ContentId, 
     c.ContentName, 
     c.SegmentLengthInSeconds, 
     ROUND(SegmentLengthInSeconds*0.75) as SeventyFivePercent,  
     cv.SchoolName, 
     cv.District 
FROM PD360v2.ViewingProgress as vp 
JOIN PD360v2.Content as c on vp.ContentId = c.ContentId 
JOIN PD360v2.ClientPersonnel as cp on vp.PersonnelId = cp.PersonnelId 
JOIN PD360v2.ClientView as cv on cp.ClientId = cv.Id 
WHERE vp.BeganViewingDate BETWEEN '2016-02-02 08:00:00' AND '2016-02-09 07:59:00' 
AND cv.NcesDistrictId IN (List Of districts here) 

) as a 

ORDER BY PersonnelId) as b 
WHERE ContentName not like 0 
ORDER BY District, SchoolName, Name; 
+0

如何使用UNION ALL列? – kiro

回答

0

我只是介紹了SQL,你應該添加要通過urself

Select vp.name,'0' from PD360v2.ViewingProgress as vp 
Where vp.BeganViewingDate between '2016-02-02 08:00:00' and '2016-02-09 07:59:00' And cv.NcesDistrictId in (List Of districts here) 
Union all 
Select vp.name,'Stem' from PD360v2.ViewingProgress as vp 
join PD360v2.Content as c on vp.contentid=c.contentid 
Where vp.BeganViewingDate between '2016-02-02 08:00:00' and '2016-02-09 07:59:00' And cv.NcesDistrictId in (List Of districts here) and contentname like '%Stem%' 
+0

原始問題中的列號不匹配。你不能'聯合所有' – Wistar

+0

但是我想複製一行,而不是一列。 – Aidandebradney