mysql - Return only rows whose max value is less than specified -


hi quite tricky 1 (for me). have 3 tables.

one contains mobile numbers , unique id (tbldealermobiles)

fcs - mobile number 1234 - 07464648787 1234 - 07565465465 3566 - 07878989989 7899 - 07464646466 7899 - 07565465464 9654 - 07589898998  

one contains purchase dates , unique id , other details (tblhistory)

fcs - purchase date - purchased 1234 - 22/04/2013 - gloves 1234 - 14/03/2013 - hat 1234 - 01/03/2013 - coat 3566 - 20/04/2013 - gloves 3566 - 19/04/2012 - hat 7899 - 14/03/2013 - shoes 9654 - 24/05/2013 - hat 9654 - 19/04/2013 - shoes 

one contains customer type , unique id , other details. (tblalldealers)

fcs - cust type - name 1234 - virtual - jim 3566 - outbound - jon 7899 - virtual - jack 9654 - outbound - susan 

my problem comes when want display customers have bought more 30 days ago if they're 'outbound' , more 60 days ago if they're virtual.

i want return mobile numbers jon , jack because others have purchased since dates specified customer type.

i'm using inner join link 3 tables on unique id(fcs), i'm using max return values who's max value less date have no clue how add criteria specify 2 different dates.

here query have far -

select * tbldealermobiles   inner join tblhistory on tbldealermobiles.fcs = tblhistory.fcs   inner join tblalldealers on tbldealermobiles.fcs = tblalldealers.fcs (tblalldealers.custgroup = 'virtual' ,        tblhistory.purchasedate < date('2013-03-22'))     or        (tblalldealers.custgroup = 'outbound' ,         tblhistory.purchasedate < date('2013-04-21'))  group tbldealermobiles.mobilenumber  having max(tblhistory.purchasedate) < date('2013-04-21')  order tblhistory.purchasedate desc 

the problem fcs have purchase date earlier date specified want return mobile number hasn't got purchase date after date specified depending on customer group.

thanks in advance help.

edit: formatting code dukeling.

you want pull condition join having clause. join only looking @ records before dates, don't know if happens afterwards.

select * tbldealermobiles   inner join tblhistory on tbldealermobiles.fcs = tblhistory.fcs   inner join tblalldealers on tbldealermobiles.fcs = tblalldealers.fcs tblalldealers.custgroup in ('virtual', 'outbound') group tbldealermobiles.mobilenumber  having max(tblhistory.purchasedate) <             max(case when tblalldealers.custgroup = 'virtual' date('2013-03-22')                      when tblalldealers.custgroup = 'outbound' date('2013-04-21')                 end) order tblhistory.purchasedate desc 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -