2015-12-04 85 views
0

比方说,我有如下表:选择从表(MS SQL Server中,SQLite的)最新版本

ID | UID | Version | Content 
---+-----+---------+----------------------------- 
1 | 1 |  1 | Something 
2 | 1 |  2 | Something (changed) 
3 | 2 |  1 | Alice has a cat 
4 | 2 |  2 | Alice has a cat and a dog 
5 | 2 |  3 | Alice has a cat and a canary 

我需要创建查询,这将返回所有对象,但只有最新版本,所以在这种情况下:

ID | UID | Version | Content 
---+-----+---------+----------------------------- 
2 | 1 |  2 | Something (changed) 
5 | 2 |  3 | Alice has a cat and a canary 

由于SQL方言不同,我会运行MS SQL Server 2008上的这个查询的SQLite 3

我怎样才能做到这一点?

回答

2

NOT EXISTS查询:

select * 
from tablename t1 
where not exists (select 1 from tablename t2 
        where t2.uid = t1.uid 
        and t2.version > t1.version) 

JOIN查询:

select t1.* 
from tablename t1 
    join (select uid, max(version) as version from tablename group by uid) t2 
    on t2.uid = t1.uid and t2.version = t1.version 

相关子查询:

select t1.* 
from tablename t1 
where t1.version = (select max(version) from tablename t2 
        where t2.uid = t1.uid) 

IN子查询:

select * 
from tablename 
where (uid, version) IN (select uid, max(version) from tablename 
         group by uid) 
0

有几种不同的方法可以解决这个问题。但是每种方法都遵循相同的原则。

查询的一部分将标识每个UID的最新版本。这将用于过滤记录集。

在下面的例子中,我发现每个UID的当前版本都使用了子查询,然后我用它来过滤主记录集。

Example

/* This table gives us some sample records 
* to experiment with. 
*/ 
DECLARE @Example TABLE 
    (
     ID   INT    PRIMARY KEY, 
     [UID]  INT    NOT NULL, 
     [Version] INT    NOT NULL, 
     Content  VARCHAR(255) NOT NULL 
    ) 
; 

/* Populate the sample data. 
*/ 
INSERT INTO @Example 
    (
     ID, 
     [UID], 
     [Version], 
     Content 
    ) 
VALUES 
    (1, 1, 1, 'Something'), 
    (2, 1, 2, 'Something (changed)'), 
    (3, 2, 1, 'Alice has a cat'), 
    (4, 2, 2, 'Alice has a cat and a dog'), 
    (5, 2, 3, 'Alice has a cat and a canary') 
; 

/* Return the most recent version of each UID. 
* This is done by using a sub query to calcualte the max of each UIDs version. 
*/ 
SELECT 
    e.* 
FROM 
    @Example AS e 
     INNER JOIN 
      ( 
       /* This sub query finds the max version of 
       * each UID. 
       */ 
       SELECT 
        [UID], 
        MAX([Version]) AS Current_Version 
       FROM 
        @Example  
       GROUP BY 
        [UID] 
      ) AS mv 
     ON mv.[UID]   = e.[UID] 
     AND mv.Current_Version = e.[Version] 
;