如何按不同日期的价格差异对表格进行排序

时间:2016-03-08 14:32:25

标签: python sql sqlite

我有这个问题:

SELECT * FROM (
select t.article_number, t.date, t.company, t.size, t.price , count(*) as rnk
from price t
INNER JOIN price s
ON(t.article_number = s.article_number and t.company = s.company and t.date <= s.date)
    GROUP BY t.article_number,t.company,t.date,t.size,t.price
)

    WHERE rnk <= 2
    order by article_number, company, date

适用于此数据:

('50240-20', '2016-03-08 13:06:12.872955', '2B', '20', '645,75')
('50240-20', '2016-03-08 13:13:55.311955', '2B', '20', '645,75')
('50240-20', '2016-03-08 13:47:13.737155', '2B', '20', '635,75')
('50240-20', '2016-03-08 13:51:32.677155', '2B', '20', '645,75')
('50240-20', '2016-03-08 13:06:12.872955', 'Bio', '20', '423,20')
('50240-20', '2016-03-08 13:13:55.311955', 'Bio', '20', '423,20')
('50240-20', '2016-03-08 13:47:13.737155', 'Bio', '20', '403,20')
('50240-20', '2016-03-08 13:51:32.677155', 'Bio', '20', '423,20') ...

它产生如下输出:

('50240-20', '2016-03-08 13:47:13.737155', '2B', '20', '635,75', 2)
('50240-20', '2016-03-08 13:51:32.677155', '2B', '20', '645,75', 1)

('50240-20', '2016-03-08 13:47:13.737155', 'Bio', '20', '403,20', 2)
('50240-20', '2016-03-08 13:51:32.677155', 'Bio', '20', '423,20', 1) ...

但我想根据这两个日期之间的最大价格差异对这个输出进行排序。所以看起来应该是这样的:

('50240-20', '2016-03-08 13:47:13.737155', 'Bio', '20', '403,20', 2)
('50240-20', '2016-03-08 13:51:32.677155', 'Bio', '20', '423,20', 1)

('50240-20', '2016-03-08 13:47:13.737155', '2B', '20', '645,75', 2)
('50240-20', '2016-03-08 13:51:32.677155', '2B', '20', '635,75', 1) ...

因为顶级产品的价格变化了4,7%而第二个变化了1.6%

我希望你理解我的意思,也许可以建议一个解决方案,因为我在sql中并不是那么好。我正在使用sqlite3和python。

非常感谢!

1 个答案:

答案 0 :(得分:0)

  • 假设sqlite3使用cte,您可以使用类似的内容。
  • 我将date1用作datesize1用作size,因为我的数据库不允许我使用这些关键字。
  • price格式应更改为decimal而不是string

这是我在Oracle中使用Common Type Expression尝试的查询。如果sqlite不支持它,则必须用主查询替换每个派生表的查询。 (就像我使用tbl1时一样,您必须使用用于派生tbl1的查询,依此类推。

with tbl1 as
(select p1.article_number,p1.company,count(p1.date1) as rno,max(p1.date1) as date1,max(p1.size1) as size1,max(p1.price) as price
from price p1 inner join price p2
on p1.article_number=p2.article_number and p1.company=p2.company and p1.date1>=p2.date1
group by p1.article_number,p1.company,p1.date1
)
,tbl2 as
(select p1.article_number,p1.company,count(p1.date1)+1 as rno,max(p1.date1) as date1,max(p1.size1) as size1,max(p1.price) as price
from price p1 inner join price p2
on p1.article_number=p2.article_number and p1.company=p2.company and p1.date1>=p2.date1
group by p1.article_number,p1.company,p1.date1
)
,tbl3 as
(
select tbl1.*,
case when tbl1.price>tbl2.price then ((tbl1.price-tbl2.price)/tbl1.price)*100
else ((tbl2.price-tbl1.price)/tbl1.price)*100 end as diff_percent
from tbl1 inner join tbl2 
on tbl1.article_number=tbl2.article_number and tbl1.company=tbl2.company
and tbl1.rno=tbl2.rno
order by tbl1.article_number,tbl1.company,tbl1.rno
)
,tbl4 as(
select t1.article_number,t1.company,count(t1.diff_percent) as rnk,max(t1.date1) as date1,max(t1.price) as price,max(t1.diff_percent) as diff_percent
from tbl3 t1 inner join tbl3 t2
on t1.article_number=t2.article_number and t1.company=t2.company
and t1.diff_percent <=t2.diff_percent
group by t1.article_number,t1.company,t1.diff_percent
)
select * from tbl4 
where rnk <=2
order by article_number,company,date1