我想计算每天的联系人数量/订单数量
我的样本是这样的:
orderID orderdate ContactID contact date
1 11-May A 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
例:我第12天的比率是2/3(1,2,3和A,B)
不需要任何连接就可以了。
你能帮我做一个SQL或Hive查询吗
最佳答案
这应该会给你你想要的比率:
Select isnull(orderdate, contactdate), concat(cast(isnull(contacts,0) as char),'/',cast(isnull(orders,0) as char)) as Ratio
from
(Select orderdate, count(orderdate) orders
from tbl
where orderdate is not null
Group by orderdate) o
left join
(Select contactdate, count(contactdate) contacts
from tbl
where contactdate is not null
Group by contactdate) c on o.orderdate = c.contactdate
UNION
Select isnull(orderdate, contactdate), cast(isnull(contacts,0) as varchar) + '/' + cast(isnull(orders,0) as varchar)
from
(Select orderdate, count(orderdate) orders
from @foobar
where orderdate is not null
Group by orderdate) o
right join
(Select contactdate, count(contactdate) contacts
from @foobar
where contactdate is not null
Group by contactdate) c on o.orderdate = c.contactdate
我在这里添加了一个Union,以便复制一个完整的外部Join,以防联系人日期不在order date中
关于mysql - SQL中基于不同日期字段的比率计算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30254425/