我有一个表,它使用ID和GUID进行版本控制。 ID增加,但GUID是静态的,允许版本控制。
例如:
ID GUID Data
01 ASD Something
02 ASD Something Changed
03 ASD Something Changed Again
04 DSA Something Else
05 DSA Something Else Changed
所以问题是,你如何得到最新的数据,所以最终得到:
03 ASD Something Changed Again
05 DSA Something Else Changed
答案 0 :(得分:1)
试试这个:
SELECT t1.id, t1.guid, t1.Data
FROM table t1
JOIN ( SELECT MAX(id) AS id, guid
FROM table GROUP BY guid ) t2
ON t1.id = t2.id AND t1.guid = t2.guid
答案 1 :(得分:1)
您也可以尝试
DECLARE @Table TABLE(
ID INT,
GUID VARCHAR(10),
Data VARCHAR(100)
)
INSERT INTO @Table SELECT 01,'ASD','Something'
INSERT INTO @Table SELECT 02,'ASD','Something Changed'
INSERT INTO @Table SELECT 03,'ASD','Something Changed Again'
INSERT INTO @Table SELECT 04,'DSA','Something Else'
INSERT INTO @Table SELECT 05,'DSA','Something Else Changed'
SELECT *
FROM @Table t
WHERE t.ID = ( SELECT MAX(ID) ID
FROM @Table
WHERE GUID = t.GUID GROUP BY GUID)
您应该尝试对两种解决方案(例如@Rubens Farias提供的解决方案)进行性能检查,并根据您拥有的索引查看最适合您的解决方案。