简体   繁体   English

SQL / HIVE:如何计算购买天数

[英]SQL/HIVE : How to calculate days to purchase

SQL/Hive: I am looking to calculate # of days to purchase by a visitor. SQL / Hive:我希望计算访客购买的天数。 Here is how my data looks like 这是我的数据的样子

date    visitor orders
1-Jan   A   0  
1-Jan   B   0  
4-Jan   B   1  
5-Jan   A   0  
12-Jan  A   1

This is the result I am expecting: 这是我期望的结果:

Days to purchase    count of visitors
0   0
1   0 
2   0
3   1
4   0
5   0
.   .
.   .
.   .
11  1

Any help ? 有什么帮助吗?

if I understood you correctly: what you need to do is to find the minmum date for each combination of visitor+orders 如果我对您的理解正确:那么您需要做的就是找到每个访客+订单组合的最低日期

select visitor,orders,min(date) as min.date from table group by visitor,orders

this should give something like: 这应该给出类似:

visitor orders min.date
  A         0  1-Jan 
  B         0  1-Jan
  B         1  4-Jan
  A         1  12-Jan

this table (lets call it tbl) can be self joined to give 该表(我们称其为tbl)可以自行加入

select A.visitor,datediff(day,purchase.date,first.visit) as days.to.purchase 
from (select visitor,min.date as first.visit from tbl where orders=0) A 
inner join (select visitor,min.date as purchase.date from tbl where orders=1) B
on A.visitor=B.visitor

now, wrap this query with an outer query to count the visitors with same datediffs: 现在,将此查询与外部查询一起包装以计算具有相同datediffs的访问者:

 select days.to.purchase,count(visitors) as visitors from 
 (select A.visitor,datediff(day,purchase.date,first.visit) as days.to.purchase 
    from (select visitor,min.date as first.visit from tbl where orders=0) A 
    inner join (select visitor,min.date as purchase.date from tbl where orders=1) B
    on A.visitor=B.visitor
) joined
group by days.to.purchase order by days.to.purchase

hope I understood you correctly. 希望我正确理解你。 I'm not sure this is the right solution but you didn't give me much to start with :) 我不确定这是否是正确的解决方案,但您并没有给我太多的开始:)

the full solution could be: 完整的解决方案可能是:

 select days.to.purchase,count(visitors) as visitors from 
 (select A.visitor,datediff(day,purchase.date,first.visit) as days.to.purchase 
    from 
(select visitor,min.date as first.visit from 
(select visitor,orders,min(date) as min.date from table group by visitor,orders) tbl where orders=0) A 
    inner join 
(select visitor,min.date as purchase.date from 
(select visitor,orders,min(date) as min.date from table group by visitor,orders) tbl where orders=1) B
    on A.visitor=B.visitor
) joined
group by days.to.purchase order by days.to.purchase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM