mysql - Join multiple models' subqueries with rails -
i'm trying write below query in rails cleanest , simplest way;
select * (select date(logtime) date, sum(case when duration/60 <= 2 1 else 0 end) d_0_2, sum(case when duration/60 > 2 , duration/60 <=5 1 else 0 end) d_2_5, sum(case when duration/60 > 5 , duration/60 <= 10 1 else 0 end) d_5_10, sum(case when duration/60 > 10 , duration/60 <= 15 1 else 0 end) d_10_15, sum(case when duration/60 > 15 1 else 0 end) d_15_9999 session_logs date(logtime) > "2014-04-1" , isclosed = 1 group date(logtime)) l1 left join (select logdate date, potential, conversion, bounce, newcustomers, repeatcustomers, averagetime metrics client_id = 1 , logdate between "2014-01-01" , "2015-05-04" group logdate) l2 on l1.date = l2.date
so have 2 models, sessionlog , metric. wrote 2 different queries each of model;
metric = metric.select("potential,conversion,logdate,bounce,newcustomers,repeatcustomers,averagetime"). where("client_id = ? , logdate between ? , ?",client_id,startdate,enddate)
,
dwell = sessionlog.select("date(logtime) logdate, sum(case when duration/60 <= 2 1 else 0 end) d_0_2, sum(case when duration/60 > 2 , duration/60 <=5 1 else 0 end) d_2_5, sum(case when duration/60 > 5 , duration/60 <= 10 1 else 0 end) d_5_10, sum(case when duration/60 > 10 , duration/60 <= 15 1 else 0 end) d_10_15, sum(case when duration/60 > 15 1 else 0 end) d_15_9999"). where("client_id = ? , isclosed = 1 , date(logtime) between ? , ?", client_id, startdate, enddate)
but couldn't figure out how should join sets date.
what best way that?
thanks!
Comments
Post a Comment