gpt4 book ai didi

SQL 查询 - 从特定行过滤值到结果

转载 作者:行者123 更新时间:2023-12-04 14:33:04 27 4
gpt4 key购买 nike

我有一张表,用于存储乏燃料的发票和汽车加油的公里数,其结构如下:

enter image description here

我的目标是获得如下所示的结果,因此我可以计算发票之间的已用公里数。

enter image description here

关于如何构建查询以获得所需结果的任何建议?

最佳答案

SELECT Date,
(SELECT MAX(Kms) FROM invoices i2 WHERE i2.Kms < i1.Kms) AS StartKm,
Kms AS FinishKm
FROM invoices i1
ORDER BY Kms

参见:SQL Fiddle Demo .

关于SQL 查询 - 从特定行过滤值到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15431033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com