将日期结果集拆分为sql server中的列

时间:2014-10-20 14:47:51

标签: sql-server sql-server-2012 pivot

你能帮忙吗

我有一个查询,显示教师数量,访问过的网站以及访问日期。此查询会查看过去一周的所有教师访问次数。

我想将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%'   

1 个答案:

答案 0 :(得分: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