2017-04-22 115 views
0

我有一个要求做销售分析周报 出认沽想下面sql每日,每周,不同产品的年度报告?

年:

product_name  week1_date   week2_date  week3_date  week4_date '''' 
    name1   8     40    0    0 
    name2   2     42    2    0 
    name3   5     47    1    0 

我的架构是

产品

id 
sys_sku 
desc 
selling_price 
cost_price 
supplier_id 

销售

id 
product_id 
sold_to 
date 
quantity 
desc 

我想按日期不同的产品

做总和(数量)组的帮助将很可观

+0

SQLite没有DateTime类型,也没有在转动内置,所以它不会完全直线前进 –

+0

我们可以做到这一点到任何其他数据库,postgres –

回答

0

我会回答COS评论有点长,但它不仅仅是一个答案,而是一个暗示。

这有点取决于究竟是你的要求是什么。你可以使用单个日期参数进行查询,并使用Case..When声明查找下一个x周/月等。

假设你使用朱利安类型存储日期时间,那么你可以使用整天,你的sql看起来有点像这样的(伪SQL)

Select product_id, case when date between inputDate and inputDate + 7 then quantity end as week1, case when date between inputDate + 7 and inputDate + 14 then quantity end as week2 from sale where date between inputDate and inputDate + x 

然后,您可以和或组计算销售相应