复杂的数据要求

时间:2011-03-08 00:00:55

标签: sql sql-server tsql

这是我的问题:

select Views.VisitorID,
       Views.ViewID,
       Views.ViewDateTime,
       Views.VisitDate,
       Visits.Views,
       ProductID.Product,
       PageID.Page,
       hostID.host
from   Views
       left join Product
         on Views.ViewID = Product.ViewID
       left join ProductID
         on Product.ProductID = ProductID.ProductID
       left join host
         on Views.ViewID = host.ViewID
       left join hostID
         on host.hostID = hostID.hostID
       inner join PageID
         on Views.PageID = PageID.PageID
       inner join Visits
         on Views.VisitID = Visits.VisitID
where  Visits.Views <= 50
       and visits.firstvisit = 1
       and visitdate = '07 March, 2011'
       and ProductID.Product in ( 'product a', 'product b' )
group  by Views.VisitorID,
          Views.ViewID,
          Views.ViewDateTime,
          Views.VisitDate,
          Visits.Views,
          ProductID.Product,
          PageID.Page,
          hostID.host
order  by Views.VisitorID,Views.ViewID, Views.ViewDateTime  

当第一个viewID(每个visitorID有多个viewID)是'product a'或'product b'时,我正在寻找每个visitorID的上述记录。有人可以帮忙吗?

下面附有数据,预期结果为visitorid = 1,2&amp; 7。

VisitorID        ViewID                ViewDateTime        VisitDate        Views     Product    Page   host  
1                5874194128        1/31/2011 0:00        31-Jan                1        A        X        Y  
2                5874194131        1/31/2011 0:00        31-Jan                6        B        X        Y  
2                5874209907        1/31/2011 0:08        31-Jan                6        B        X        Y  
2                5874210697        1/31/2011 0:08        31-Jan                6        B        X        Y  
2                5874213486        1/31/2011 0:10        31-Jan                6        B        X        Y  
2                5874220373        1/31/2011 0:13        31-Jan                6        D        X        Y  
2                5874223494        1/31/2011 0:14        31-Jan                6        D        X        Y  
3                5874194139        1/31/2011 0:00        31-Jan                2        E        X        Y  
3                5874227680        1/31/2011 0:16        31-Jan                2        A        X        Y  
4                5874194149        1/31/2011 0:00        31-Jan                1        F        X        Y  
4                6082113181        2/22/2011 4:39        22-Feb                1        B        X        Y  
5                5874194150        1/31/2011 0:00        31-Jan                1        F        X        Y  
5                5909359616        2/3/2011 8:35          3-Feb                1        A        X        Y  
5                6042085229        2/17/2011 23:59       17-Feb                2        A        X        Y  
5                6042086534        2/17/2011 23:59       17-Feb                2        A        X        Y  
6                5874194154        1/31/2011 0:00        31-Jan                1        D        X        Y  
6                5936833964        2/6/2011 2:37          6-Feb                1        A        X        Y  
6                5959528921        2/9/2011 0:33          9-Feb                1        B        X        Y  
7                5874194157        1/31/2011 0:00        31-Jan                1        A        X        Y  
7                5937521299        2/6/2011 20:58         6-Feb                4        C        X        Y  
7                5937542326        2/6/2011 21:20         6-Feb                4        C        X        Y  
7                5937542560        2/6/2011 21:20         6-Feb                4        C        X        Y  
7                5937552378        2/6/2011 21:31         6-Feb                4        C        X        Y  

1 个答案:

答案 0 :(得分:1)

V2 - 是当天每位访客的最低/第一个视图。 V3 - 是在第一个视图中查看产品A或B的访问者。 然后,查询的其余部分将访问这些访问者并将其链接到其余数据。

Select V.VisitorID,
   V.ViewID,
   V.ViewDateTime,
   V.VisitDate,
   VS.Views,
   PID.Product,
   PGID.Page,
   HID.host
from Views V
inner join (
    Select V2.VisitorID, V2.VisitDate
    from (
        select VisitorID, VisitDate, Min(ViewID) as FirstViewID
        from Views
        where visitdate = '07 March, 2011'
        group by VisitorID, VisitDate
    ) V2 on V.VisitorID = V2.VisitorID and V.ViewID = V2.FirstViewID
    inner join Product P on V.ViewID = P.ViewID
    inner join ProductID PID on P.ProductID = PID.ProductID
    where PID.Product in ('A', 'B')
) V3 on V3.VisitorID = V.VisitorID and V3.VisitDate = V.VisitDate
inner join Product P on V.ViewID = P.ViewID
inner join ProductID PID on P.ProductID = PID.ProductID
inner join Host H on V.ViewID = H.ViewID
inner join HostID HID on H.hostID = HID.hostID
inner join PageID PGID on V.PageID = PGID.PageID
inner join Visits VS on V.VisitID = VS.VisitID
where VS.Views <= 50
   and VS.firstvisit = 1