2014-10-20 57 views
0

您可以請協助分割日期結果集到sql server中的列

我有一個查詢,顯示教師的數量,訪問的網站以及他們訪問的日期。該查詢查看過去一週的所有教師訪問情況。

我想將dateattended字段拆分爲列以顯示過去一週的每日訪問。下面是它的外觀。

EmployeeNumber Name     HomeSite Site Attended Day Attended 
TP-000322789 Samuel Mohlamnyane Teacher   Port Elizabeth 2014-10-18 07:23 
TP-000148774 Jean Smoothie   Teacher  Hennopsview 2014-10-13 08:55 
TP-000148774 Jean Smoothie   Teacher  Hennopsview 2014-10-16 08:43 
TP-000148122 Anthony Mike   Teacher  Tzaneen  2014-10-19 09:19 
TP-000148122 Anthony Mike   Teacher  Tzaneen  2014-10-15 08:26 
TP-000328452 Geneve Gorridon   Teacher  Tzaneen  2014-10-14 07:44 
TP-000346529 Edmos Dube    Teacher  Melrose  2014-10-18 07:47 
TP-000321374 Anita Rene Classen  Teacher  Johannesburg 2014-10-17 07:57 
TP-000324511 Anthonysia White  Teacher  Durbanville 2014-10-15 07:53 
TP-000324511 Anthonysia White  Teacher  Durbanville 2014-10-18 12:26 
TP-000327471 Moses Mathebula   Teacher  Polokwane  2014-10-13 05:50 
TP-000148194 Nonhlanhla Ndlovu  Teacher  Vereeniging 2014-10-15 07:06 
TP-000323383 Lerato Manyanka   Teacher  Bedfordview 2014-10-13 07:26 
TP-000323383 Lerato Manyanka   Teacher  Bedfordview 2014-10-16 06:51 
TP-000323384 Lerato Manyanka   Teacher  Bedfordview 2014-10-17 08:57 

現在我想拆分日出席日期不同列從昨天走到過去七天。

下面是我用來獲得上述結果集的代碼。結果應該如何。

EmployeeNumber Name    HomeSite Site Attended Day 1 Day2 Day 3    Day 4    Day 5    Day 6   Day 7 
TP-000148194 Nonhlanhla Ndlovu Teacher Vereeniging              2014-10-15 07:06 
TP-000323383 Lerato Manyanka Teacher Bedfordview     2014-10-17 08:57 2014-10-16 06:51          2014-10-13 07:26 



SELECT mdet.MemRefNo      AS 'EmployeeNumber' 
     , cont.FirstName + ' ' + cont.LastName AS Name 
     , s.Name        AS 'HomeSite' 
     , Attend.VisitedSite     AS 'Site Attended' 
     , Attend.Weekdays      AS 'Day Attended' 

FROM MemberDetail mdet 
INNER JOIN MembershipHistory mhis ON mdet.CurrentMembershipID = mhis.ID32 
INNER JOIN contacts cont ON cont.GUID = mdet.ContactGUID 
INNER JOIN Sites s ON s.id = cont.HomeSiteID 

INNER JOIN Packages pg ON pg.ID = mhis.PackageID 


CROSS APPLY 
(
SELECT min(a1.attenddate) AS Weekdays , a1.contactguid, a1.SiteID , s.Name as VisitedSite  FROM dbo.attendance a1 
    INNER JOIN Sites s ON s.id = a1.Siteid 
WHERE DATEDIFF(DAY,a1.attenddate,GETDATE()) <= 7 
and ContactGuid = mdet.ContactGuid 
AND a1.isswipesuccessful = 1 
GROUP BY a1.ContactGuid, DATEPART(DW, a1.attenddate),a1.SiteID , s.Name 
) Attend 

WHERE pg.Description LIKE '%Teacher%' 
+0

您可以編輯的問題增加的新列應該是什麼樣子的例子嗎? – acfrancis 2014-10-20 15:10:45

+0

嗨,我已編輯上述查詢,以顯示這應該看起來如何 – user1538257 2014-10-21 06:29:39

+0

好,所以你已經寫了一個選擇。它產生你想要的結果嗎?它靠近嗎? – acfrancis 2014-10-21 09:11:15

回答

0

我想這是你想要的查詢,它使用與現有查詢作爲源通過一個共同的表表達式的結果的支點。理想情況下,代碼可以合併到一個查詢中,但由於您沒有提供源表中的任何測試數據,但只有輸出我沒有嘗試重寫它。請注意,如果一個人在一天內多次訪問同一個網站,則只會顯示最近的時間。

-- using original query as source 
;WITH visits AS (
    SELECT mdet.MemRefNo      AS 'EmployeeNumber' 
     , cont.FirstName + ' ' + cont.LastName AS Name 
     , s.Name        AS 'HomeSite' 
     , Attend.VisitedSite     AS 'Site Attended' 
     , Attend.Weekdays      AS 'Day Attended' 

    FROM MemberDetail mdet 
    INNER JOIN MembershipHistory mhis ON mdet.CurrentMembershipID = mhis.ID32 
    INNER JOIN contacts cont ON cont.GUID = mdet.ContactGUID 
    INNER JOIN Sites s ON s.id = cont.HomeSiteID 
    INNER JOIN Packages pg ON pg.ID = mhis.PackageID 
    CROSS APPLY 
    (
    SELECT min(a1.attenddate) AS Weekdays , a1.contactguid, a1.SiteID , s.Name as VisitedSite  FROM dbo.attendance a1 
     INNER JOIN Sites s ON s.id = a1.Siteid 
    WHERE DATEDIFF(DAY,a1.attenddate,GETDATE()) <= 7 
    and ContactGuid = mdet.ContactGuid 
    AND a1.isswipesuccessful = 1 
    GROUP BY a1.ContactGuid, DATEPART(DW, a1.attenddate),a1.SiteID , s.Name 
    ) Attend 
    WHERE pg.Description LIKE '%Teacher%' 
) 

-- query to produce results 
SELECT 
    EmployeeNumber, 
    Name, 
    HomeSite, 
    [Site Attended], 
    [1] AS 'Day 1', 
    [2] AS 'Day 2', 
    [3] AS 'Day 3', 
    [4] AS 'Day 4', 
    [5] AS 'Day 5', 
    [6] AS 'Day 6', 
    [7] AS 'Day 7' 
FROM (
    SELECT *, DATEDIFF(day, [Day Attended], GETDATE()) diff 
    FROM visits 
    WHERE [Day Attended] > (GETDATE()-7) -- adjust this to limit date range 
    ) a 
PIVOT (
    MAX([Day Attended]) FOR [diff] in ([1],[2],[3],[4],[5],[6],[7]) 
) AS Pivoted; 

修改現有查詢以獲得所需結果可能微不足道。

Sample SQL Fiddle(使用樣本輸出數據作爲源)。

樣品結果:

EmployeeNumber  Name          HomeSite    Site Attended  Day 1     Day 2     Day 3     Day 4     Day 5     Day 6     Day 7 
-------------------- ---------------------------------------- -------------------- -------------------- ----------------------- ----------------------- ----------------------- ----------------------- ----------------------- ----------------------- ----------------------- 
TP-000148122   Anthony Mike        Teacher    Tzaneen    NULL     2014-10-19 09:19:00.000 NULL     NULL     NULL     2014-10-15 08:26:00.000 NULL 
TP-000148194   Nonhlanhla Ndlovu      Teacher    Vereeniging   NULL     NULL     NULL     NULL     NULL     2014-10-15 07:06:00.000 NULL 
TP-000148774   Jean Smoothie       Teacher    Hennopsview   NULL     NULL     NULL     NULL     2014-10-16 08:43:00.000 NULL     NULL 
TP-000321374   Anita Rene Classen      Teacher    Johannesburg   NULL     NULL     NULL     2014-10-17 07:57:00.000 NULL     NULL     NULL 
TP-000322789   Samuel Mohlamnyane      Teacher    Port Elizabeth  NULL     NULL     2014-10-18 07:23:00.000 NULL     NULL     NULL     NULL 
TP-000323383   Lerato Manyanka       Teacher    Bedfordview   NULL     NULL     NULL     2014-10-17 08:57:00.000 2014-10-16 06:51:00.000 NULL     NULL 
TP-000324511   Anthonysia White       Teacher    Durbanville   NULL     NULL     2014-10-18 12:26:00.000 NULL     NULL     2014-10-15 07:53:00.000 NULL 
TP-000346529   Edmos Dube        Teacher    Melrose    NULL     NULL     2014-10-18 07:47:00.000 NULL     NULL     NULL     NULL 
+0

嗨jpw,非常感謝你指點我正確的方向。除了您提供的解決方案之外,您在限制日期範圍內使用的過濾器。 「WHERE [Day Attended>(GETDATE() - 7)」您可以在系統時間(運行查詢的時間)的同一時間過濾以查看所有出席情況(大於7天后)。爲確保當天包括所有時間,我們可以用「DATEADD(d,-7,DATEDIFF(d,0,GETDATE()))」替換「getdate() - 7」。毫無疑問,這是我正在尋找的。謝謝 – user1538257 2014-10-21 10:38:00

+0

@ user1538257事實上,'DATEADD(d,-7,DATEDIFF(d,0,GETDATE()))'會更好。我的答案是否解決了您的問題? – jpw 2014-10-21 10:42:51

+0

哦,是的,你絕對回答我的問題。就像現在補充說的,我希望我的別名從Day1,Day2等發生變化,然後添加一個類似Day1(20-10-2014),Day2(19-10-2014)的通用日期,如果您有線索我如何能做到這一點,我真的很感激它。 – user1538257 2014-10-21 11:04:25