【发布时间】:2017-12-05 17:02:22
【问题描述】:
我有以下数据(产品及其价格和价格日期):
prodId priceDate price
======+==========+=====
1 |2017-11-24| 600
1 |2017-11-21| 600
1 |2017-11-18| 600
1 |2017-11-15| 500
1 |2017-11-05| 400
1 |2017-11-03| 400
2 |2017-11-11| 300
我需要一个指标(chg 列)来显示产品价格在过去 7 天内是否发生了变化。
prodId priceDate price chg
======+==========+=====+===
1 |2017-11-24| 600| N
1 |2017-11-21| 600| Y
1 |2017-11-18| 600| Y
1 |2017-11-15| 500| N
1 |2017-11-05| 400| N
1 |2017-11-03| 400| N
2 |2017-11-14| 300| N
这个 SQL 查询应该是什么样子?
【问题讨论】:
-
2.5k 代表有这样的问题?
-
@sentinel 到目前为止我试过这个:
select prodId,preisDatum,preis, (select preis from preise p2 where p2.prodId=p.prodId and p2.preisDatum<p.preisDatum order by preisDatum desc limit 1) from preise p但这个查询显示的是上次价格 - 如果 7 天内有变化,则不会。 -
@davidbuzatto,好吧,也许我今天工作很累,看不到这个解决方案的简单性,或者你看不到问题的复杂性,所以请帮忙并提供解决方案
-
所以@Sentinel 只是在评论,没有贡献。
-
@sbrbot 在我回复您时,其他两个用户已经提供了答案。
标签: mysql