Click to See Complete Forum and Search --> : Display records from a table based on checking each column value


mike09
05-29-2009, 05:28 AM
My query is
"Take each visittime of a user(st_emailtracking -Table) then
check that this user has purchased(op_order - Table) within 30 days,
if he has no purchase then
take his next visittime and check if there is any purchased within 30 days from this visittime and go on...
When the user purchased within 30 days from any visittime ,
then to check his purchase date is between 'Nov.1-2008' & 'Nov.30-2008' and also check some of the conditions(u can see in my code) then
display all records based on that."
I wrote one query,as follows
1)<code>
select st.userid,(up.firstname + ' ' + up.lastname) as username,st.visittime,op.datepurchased,st.visittime
from alamyStatistics..st_emailTracking st
join alamyusers..up_visitor up on st.userid=up.userid
join alamyorders..op_order op on op.userid=st.userid
where ((op.datepurchased between '11-01-08' and '11-30-08')
and (order_total!=0) and (orderstatusid=100) and (up.isdistrib=0)
AND (st.emailcode like '_%LB') or (st.emailcode like '_%NB')
AND (op.datepurchased BETWEEN st.visittime and dateadd(dd,30,st.visittime)))
</code>
I know this is not getting correctly,That is needed looping statements,
ie Take first visittime1 then
(check) if datepurchased within 30 days from visittime1 then
(check) if(datepurchased between '11-01-08' and '11-30-08'
and (order_total!=0) and (orderstatusid=100) and
(up.isdistrib=0))
Display all the purchase details
else
don't need his data
else Take 2nd visittime2 then
(check) proceeds above steps.

I think u got my qn.
Please can u help me this .
I would be grateful for ur help and answer.