[英]SQL Find Previous Value for Each Row
I have a SQL table with the following structure: 我有一个具有以下结构的SQL表:
id Integer, (represents a userId)
version Integer,
attribute varchar(50)
So some sample rows are: 因此,一些示例行是:
4, 1, 'test1'
4, 2, 'test2'
4, 3, 'test3'
I need to generate output in the following format: 我需要以以下格式生成输出:
4, 1, 'test1', 'test1'
4, 2, 'test2', 'test1'
4, 3, 'test3', 'test2'
So the format of my output would be: 因此,我的输出格式为:
id Integer,
version Integer,
attribute_current varchar,
attribute_old varchar
I already tried the following: 我已经尝试了以下方法:
select versionTest.id, versionTest.version, versionTest.attribute, maxVersionTest.attribute
from versionTest
inner join
versionTest maxVersionTest
ON
versionTest.id = versionTest.id
AND
versionTest.version =
(Select max(version) currentMaxVersion
from versionTest maxRow
where maxRow.id = id);
The above query executes, but returns incorrect results. 上面的查询执行,但返回不正确的结果。 It only returns the largest version instead of returning rows for all versions. 它仅返回最大版本,而不返回所有版本的行。
Any ideas about how I should fix my query to produce the correct results? 关于如何解决查询以产生正确结果的任何想法? Thanks! 谢谢!
Note - The version numbers are not guaranteed to be sequential starting at 1. My actual database has some unusual version numbers (ie user 7 has version 3 and 15 with no versions 4, 5, 6, etc...). 注-版本号不能保证从1开始是连续的。我的实际数据库有一些不寻常的版本号(即用户7的版本为3和15,没有版本4、5、6等)。
Since you have mentioned that: 由于您已经提到:
The version numbers are not guaranteed to be sequential starting at 1. My actual database has some unusual version numbers (ie user 7 has version 3 and 15 with no versions 4, 5, 6, etc...) 版本号不能保证从1开始是连续的。我的实际数据库有一些不寻常的版本号(即用户7的版本为3和15,没有版本4、5、6等)。
MySQL doesn't support window functions like any other RDBMS does, you can still simulate on how you can create a sequential numbers and used as the linking column to get the previous rows. MySQL不像其他任何RDBMS一样支持窗口函数,您仍然可以模拟如何创建序列号并将其用作链接列以获取前一行。 Ex, 当然,
SELECT a.ID, a.Version, a.attribute attribute_new,
COALESCE(b.attribute, a.attribute) attribute_old
FROM
(
SELECT ID, version, attribute,
@r1 := @r1 + 1 rn
FROM TableName, (SELECT @r1 := 0) b
WHERE ID = 4
ORDER BY version
) a
LEFT JOIN
(
SELECT ID, version, attribute,
@r2 := @r2 + 1 rn
FROM TableName, (SELECT @r2 := 0) b
WHERE ID = 4
ORDER BY version
) b ON a.rn = b.rn + 1
SELECT a.*, COALESCE(b.attribute,a.attribute) attribute_old
FROM
( SELECT x.*
, COUNT(*) rank
FROM versiontest x
JOIN versiontest y
ON y.id = x.id
AND y.version <= x.version
GROUP
BY x.id
, x.version
) a
LEFT
JOIN
( SELECT x.*
, COUNT(*) rank
FROM versiontest x
JOIN versiontest y
ON y.id = x.id
AND y.version <= x.version
GROUP
BY x.id
, x.version
) b
ON b.id = a.id
AND b.rank = a.rank-1;
Sample output ( DEMO ): 样本输出( DEMO ):
+----+---------+-----------+------+---------------+
| id | version | attribute | rank | attribute_old |
+----+---------+-----------+------+---------------+
| 4 | 1 | test1 | 1 | test1 |
| 4 | 5 | test2 | 2 | test1 |
| 4 | 7 | test3 | 3 | test2 |
| 5 | 2 | test3 | 1 | test3 |
| 5 | 3 | test4 | 2 | test3 |
| 5 | 8 | test5 | 3 | test4 |
+----+---------+-----------+------+---------------+
If version numbers always increase by 1
, you could: 如果版本号始终增加1
,则可以:
select cur.id
, cur.version
, cur.attribute
, coalesce(prev.attribute, cur.attribute)
from versionTest
left join
versionTest prev
on prev.id = cur.id
and prev.version = cur.version + 1
You could try... 你可以尝试...
SELECT ID,
VERSION,
ATTRIBUTE,
(SELECT ATTRIBUTE
FROM VERSIONTEST V3
WHERE V3.ID = V1.ID AND
V3.VERSION = (SELECT MAX(VERSION)
FROM VERSIONTEST V2
WHERE V2.ID = V1.ID AND
V2.VERSION < V1.VERSION)) AS PREVIOUSATTRIBUTE
FROM VERSIONTEST V1;
provided the version values are in numerical order. 如果版本值按数字顺序排列。
I think the easiest way to express this is with a correlated subquery: 我认为表达这一点的最简单方法是使用相关子查询:
select id, version, attribute as attribute_current,
(select attribute
from VersionTest vt2
where vt2.id = vt.id and vt2.version < vt.version
order by vt2.version
limit 1
) as attribute_prev
from VersionTest vt
This version would put in NULL
as the prev value for the first row. 此版本会将NULL
作为第一行的上一个值。 If you really want it repeated: 如果您真的想要重复:
select id, version, attribute as attribute_current,
coalesce((select attribute
from VersionTest vt2
where vt2.id = vt.id and vt2.version < vt.version
order by vt2.version
limit 1
), vt.attribute
) as attribute_prev
from VersionTest vt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.