获取每个ID和状态的前3个值

时间:2017-09-02 09:00:32

标签: sql-server sql-server-2008 sql-server-2008-r2 ssms

我有类似这样的数据,

ID        Time        Status
---       ----        ------
1          10           B
1          20           B
1          30           C
1          70           C
1         100           B
1         490           D

期望的结果应该是,

ID      Time       Status
1        490         D
1        100         B
1         70         C

这就是我应该获得前3个时间值的ID和不同状态。 为此,我试过: -

    ;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY id ORDER BY TIME DESC) AS rn
   FROM MyTable
)
SELECT  id,TIME,Status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
FROM cte
where rn<=3

但它不符合我的要求iam gettng top 3重复staus值,我该如何解决这个问题。帮助!

5 个答案:

答案 0 :(得分:1)

... #ifdef HAVEWIN #define HOMEENV "USERPROFILE" #define PATHFMT "%s\\%s" #elif HAVEUNIX #define HOMEENV "HOME" #define PATHFMT "%s/%s" #endif int main (void) { char *home = getenv (HOMEENV); char filename[PATH_MAX] = ""; char buf[BUFSIZ] = ""; FILE *fp; if (!home) { fprintf (stderr, "user home environment not found.\n"); return 1; } sprintf (filename, PATHFMT, home, FILENAME); if (!(fp = fopen (filename, "r"))) { fprintf (stderr, "error: file not found '%s'\n", filename); return 1; } if (fgets (buf, sizeof buf, fp)) puts (buf); return 0; } 分区:

status

答案 1 :(得分:0)

top函数的with ties参数将返回与顶部值匹配的所有行:

select top (3) with ties id, Time, Status from table1 order by Time desc

或者,如果您只想返回3个值,但要确保它们总是相同的3个值,那么您将需要使用其他东西作为打破平局。在这种情况下,您的id列看起来可能是唯一的。

select top (3) id, Time, Status from table1 order by Time desc, id

答案 2 :(得分:0)

试试这个:

select distinct id,max(time) over (partition by id,status) as time ,status
from mytable t order by time desc

输出 -

id  time    status
1   490     D
1   100     B
1   70      C

修改

select distinct TOP 3 id,max(time) over (partition by id,status) as time,status 
from mytable t order by time desc

答案 3 :(得分:0)

试试这个:

SELECT TOP 3 * FROM [MyTable] WHERE [Id] = 1 ORDER BY [Time] DESC

这将为您提供ID = 1的前三个记录。对于任何其他ID,只需更改WHERE子句中的数字。

此外,您可以为UNION每个ID的所有前三个记录创建一些存储过程 - 这可以通过循环遍历表中的所有不同ID来完成:)

答案 4 :(得分:0)

尝试使用RANK。 您可以使用以下查询来获得所需的结果。

select * from 
(select *, RANK() over(partition by status order by time desc) as rn from myTable)T
where rn = 1

FIDDLE