从SQL中的设置日期开始查找最少的访问天数?

时间:2018-02-18 16:18:44

标签: sql sqlite

这就是我所做的:in case of avg per class

select cust_classes,
         avg(InvoiceDate("2017-12-31") - (InvoiceDate)) as 
         avg_least_days 
         from tbl 
         group by cust_classes

它给了0所以我想也许它是零。因此测试如下: 要查看每位客户的最少访问天数。non avg query as the question

select cust_id,cust_classes,
             (InvoiceDate("2017-12-31") - (InvoiceDate)) as 
             avg_least_days 
             from tbl 

它为所有不正确的人返回0

问题是什么? 所需的输出:

cust_id  num_days_from_last_visit
1           3
2           12
3           9

1 个答案:

答案 0 :(得分:2)

这应该有效,假设visit_date是表格中的一列:

select t.ethnicity,
       avg(julianday('2017-12-31') - julianday(t.visit_date)) as 
avg_least_days 
from table_1 t
group by t.ethnicity;