mysql - Ratio Calculation in SQL based on different date fields -
i want calculate number of contacts made/number of orders placed per day how sample looks like:
orderid orderdate contactid contact date 1 11-may 12-may 2 12-may b 12-may 3 12-may null null 4 12-may c 13-may 5 13-may d 14-may
ex: ratio 12thmay 2/3(1,2,3 , a,b) need without joins. can please me sql or hive query this
this should give ratio you're looking for:
select isnull(orderdate, contactdate), concat(cast(isnull(contacts,0) char),'/',cast(isnull(orders,0) char)) ratio (select orderdate, count(orderdate) orders tbl orderdate not null group orderdate) o left join (select contactdate, count(contactdate) contacts tbl contactdate not null group contactdate) c on o.orderdate = c.contactdate union select isnull(orderdate, contactdate), cast(isnull(contacts,0) varchar) + '/' + cast(isnull(orders,0) varchar) (select orderdate, count(orderdate) orders @foobar orderdate not null group orderdate) o right join (select contactdate, count(contactdate) contacts @foobar contactdate not null group contactdate) c on o.orderdate = c.contactdate
i've added union in order replicate full outer join in case there date in contact date not in order date
Comments
Post a Comment