11

我有一张表格,列出了已安装的软件版本:

id  | userid | version | datetime
----+--------+---------+------------------------
111 | 75     | 10075   | 2013-03-12 13:40:58.770
112 | 75     | 10079   | 2013-03-12 13:41:01.583
113 | 78     | 10065   | 2013-03-12 14:18:24.463
114 | 78     | 10079   | 2013-03-12 14:22:20.437
115 | 78     | 10079   | 2013-03-12 14:24:01.830
116 | 78     | 10080   | 2013-03-12 14:24:06.893
117 | 74     | 10080   | 2013-03-12 15:31:42.797
118 | 75     | 10079   | 2013-03-13 07:03:56.157
119 | 75     | 10080   | 2013-03-13 07:05:23.137
120 | 65     | 10080   | 2013-03-13 07:24:33.323
121 | 68     | 10080   | 2013-03-13 08:03:24.247
122 | 71     | 10080   | 2013-03-13 08:20:16.173
123 | 78     | 10080   | 2013-03-13 08:28:25.487
124 | 56     | 10080   | 2013-03-13 08:49:44.503

我想显示每个记录的所有字段,userid但只显示最高版本(版本也是 a varchar)。

4

7 回答 7

8

如果您使用 SQL-Server(最低 2005 年),您可以使用 a CTEwithROW_NUMBER函数。您可以使用CASTfor version 来获得正确的顺序:

WITH cte 
     AS (SELECT id, 
                userid, 
                version, 
                datetime, 
                Row_number() 
                  OVER ( 
                    partition BY userid 
                    ORDER BY Cast(version AS INT) DESC) rn 
         FROM   [dbo].[table]) 
SELECT id, 
       userid, 
       version, 
       datetime 
FROM   cte 
WHERE  rn = 1 
ORDER BY userid

演示

ROW_NUMBER即使有多个用户使用相同(顶级)版本,也始终返回一条记录。如果要返回所有“顶级版本用户记录”,则必须替换ROW_NUMBERDENSE_RANK.

于 2013-03-13T09:04:22.760 回答
7

您没有指定要如何处理关系,但如果您希望显示重复项,则可以这样做;

SELECT a.* FROM MyTable a
LEFT JOIN MyTable b
  ON a.userid=b.userid
 AND CAST(a.version AS INT) < CAST(b.version AS INT)
WHERE b.version IS NULL

一个用于测试的 SQLfiddle

如果你想消除重复并且如果它们存在选择最新的,你将不得不稍微扩展查询;

WITH cte AS (SELECT *, CAST(version AS INT) num_version FROM MyTable)
SELECT a.id, a.userid, a.version, a.datetime 
FROM cte a LEFT JOIN cte b
  ON a.userid=b.userid
 AND (a.num_version < b.num_version OR 
     (a.num_version = b.num_version AND a.[datetime]<b.[datetime]))
WHERE b.version IS NULL

另一个 SQLfiddle

于 2013-03-13T09:09:15.893 回答
6
WITH records
AS
(
    SELECT  id, userid, version, datetime,
            ROW_NUMBER() OVER (PARTITION BY userID
                                ORDER BY version DESC) rn
    FROM    tableName
)
SELECT id, userid, version, datetime
FROM    records
WHERE   RN =1 
于 2013-03-13T08:58:53.383 回答
1

我认为这可能会解决您的问题:

 SELECT id,
       userid,
       Version,
       datetime FROM (
           SELECT id,
                  userid,
                  Version,
                  datetime , 
                  DENSE_Rank() over (Partition BY id order by datetime asc) AS Rankk
           FROM [dbo].[table]) RS 
WHERE Rankk<2

我使用 RANK 函数来满足您的要求....

于 2013-03-13T09:17:33.033 回答
0
select l.* from the_table l
left outer join the_table r
on l.userid = r.userid and l.version < r.version
where r.version is null
于 2013-03-13T09:07:12.930 回答
0

以下代码将显示您想要的内容,并且非常适合性能!

select * from the_table t where cast([version] as int) = 
(select max(cast([version] as int)) from the_table where userid = t.userid)
于 2014-11-19T14:40:21.707 回答
0

如果我的调优经验教会了我什么,那么笼统是很糟糕的。

但是,如果您获得的表格Top X很大(即数十万或数百万)。CROSS APPLY几乎是普遍最好的。事实上,如果你对它进行基准测试,交叉应用在较小的规模(数以万计)上也表现一致且令人钦佩,并且永远涵盖潜在的需求。

就像是:

select
    id
    ,userid
    ,version
    ,datetime
from
    TheTable t
cross apply
(
    select top 1 --with ties
        id
    from
        TheTable
    where
        userid = t.userid
    order by
        datetime desc
)
于 2017-06-01T17:31:39.003 回答