complex data requirement
Here is my query:
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
I am looking for the above records for every visitorID when the very first viewID (there are multiple viewID for each visitorID) is either 'product a' or 'product b'. Can someone help?
Appended below is the data and the expected results are visitorid=1, 2 & 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
V2 - is the minimum/first view for each visitor on that day. V3 - is the visitors which looked at product A or B on their first view. Then the rest of the query takes those visitors and links them to the rest of the data.
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
精彩评论