从表中选择最新版本(MS SQL Server,SQLite)

时间:2015-12-04 10:47:43

标签: sql 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 个答案:

答案 0 :(得分:2)

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)

yourubeString子查询:

var youtubeString = $scope.youtubeTrailer.join()

答案 1 :(得分: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]
;