使用distinct和max的SQL查询

发布于 2024-12-13 06:05:20 字数 284 浏览 0 评论 0原文

我有一个像这样的数据集:

 type    seqID    text     
 A       1        Text1a
 A       2        Text2a
 A       3        Text3a
 B       1        Text1b
 B       2        Text2b

如何按类型返回具有按类型分组的最高 seqID 的行?因此,在上面的示例中,我希望返回包含 A, 3, Text3a 和 B, 2, Text2b 的行。

I have a dataset like:

 type    seqID    text     
 A       1        Text1a
 A       2        Text2a
 A       3        Text3a
 B       1        Text1b
 B       2        Text2b

How do I get the row back by type with the highest seqID grouped by type? So in the above example I would want the row that has A, 3, Text3a and B, 2, Text2b returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

木落 2024-12-20 06:05:20
SELECT *  
FROM tmp t1  
WHERE NOT EXISTS  
(SELECT 1 FROM tmp t2 WHERE t1.type = t2.type AND t2.seqID > t1.seqID)  

它不应该存在具有相同类型和更高 seqID 的任何其他行。

SELECT *  
FROM tmp t1  
WHERE NOT EXISTS  
(SELECT 1 FROM tmp t2 WHERE t1.type = t2.type AND t2.seqID > t1.seqID)  

It shouldn't exists any other row with the same type and higher seqID.

别念他 2024-12-20 06:05:20

您需要一个 ID,但由于“文本”对于此示例来说似乎是唯一的

CREATE TABLE #TMP 
 (type VARCHAR(3),   seqID  INT,   [text] varchar(256))
insert #TMP values ('A'   ,    1   ,     'Text1a')
insert #TMP values ('A'   ,    2   ,     'Text2a')
 insert #TMP values ('A'   ,    3   ,     'Text3a')
 insert #TMP values ('B'   ,    1   ,     'Text1b')
 insert #TMP values ('B'   ,    2   ,     'Text2b')


SELECT * FROM #TMP T
where [text] IN
    (SELECT TOP 1 [text] FROM #TMP t2 WHERE t.type = t2.type ORDER BY t2.seqID DESC)

You kind of need an ID, but since "Text" seems unique for this example

CREATE TABLE #TMP 
 (type VARCHAR(3),   seqID  INT,   [text] varchar(256))
insert #TMP values ('A'   ,    1   ,     'Text1a')
insert #TMP values ('A'   ,    2   ,     'Text2a')
 insert #TMP values ('A'   ,    3   ,     'Text3a')
 insert #TMP values ('B'   ,    1   ,     'Text1b')
 insert #TMP values ('B'   ,    2   ,     'Text2b')


SELECT * FROM #TMP T
where [text] IN
    (SELECT TOP 1 [text] FROM #TMP t2 WHERE t.type = t2.type ORDER BY t2.seqID DESC)
无语# 2024-12-20 06:05:20
SELECT tbl.*
FROM
   ( SELECT type, MAX(seqID)
     FROM tbl
     GROUP BY type) maxes
WHERE
   tbl.type= maxes.type AND
   tbl.seqID= maxes.seqID
SELECT tbl.*
FROM
   ( SELECT type, MAX(seqID)
     FROM tbl
     GROUP BY type) maxes
WHERE
   tbl.type= maxes.type AND
   tbl.seqID= maxes.seqID
Smile简单爱 2024-12-20 06:05:20
SELECT t.* FROM
(
   SELECT type, MAX(seqID) as maxId 
   FROM Table
   GROUP BY type
) m
INNER JOIN Table t ON m.maxId = t.seqId 

使用热膨胀系数

;WITH maxIds(maxId)
AS
(
   SELECT type, MAX(seqID) as maxId 
   FROM Table
   GROUP BY type
)
SELECT t.* FROM
Table t
INNER JOIN maxIds m ON m.maxId = t.seqID
SELECT t.* FROM
(
   SELECT type, MAX(seqID) as maxId 
   FROM Table
   GROUP BY type
) m
INNER JOIN Table t ON m.maxId = t.seqId 

Using CTE

;WITH maxIds(maxId)
AS
(
   SELECT type, MAX(seqID) as maxId 
   FROM Table
   GROUP BY type
)
SELECT t.* FROM
Table t
INNER JOIN maxIds m ON m.maxId = t.seqID
‖放下 2024-12-20 06:05:20

如果您使用的是 SQL Server 2005+,则可以使用 排名函数(更具体地说,ROW_NUMBER()):

SELECT
  type,
  seqID,
  text
FROM (
  SELECT
    *,
    rnk = ROW_NUMBER() OVER (PARTITION BY type ORDER BY seqID DESC)
  FROM atable
) s
WHERE rnk = 1

If you are on SQL Server 2005+, you could use a ranking function (more specifically, ROW_NUMBER()):

SELECT
  type,
  seqID,
  text
FROM (
  SELECT
    *,
    rnk = ROW_NUMBER() OVER (PARTITION BY type ORDER BY seqID DESC)
  FROM atable
) s
WHERE rnk = 1
create table #tlb1(
[type] VARCHAR(3), seqID  INT, [text] varchar(max)
)

declare @type varchar(3), @text varchar(max);
declare @seqID int;
declare seq_cursor cursor for 
select [type], max(seqID) from tbl group by [type]
open seq_cursor
fetch next from seq_cursor into @type,@seqID
while(@@fetch_status=0)
begin
    set @text= (select [text] from tbl where [type]=@type and seqID=@seqid);
    insert into #tlb1 values (@type, @seqID,@text);
    fetch next from seq_cursor into @type,@seqID    
end
select * from #tlb1
close seq_cursor
deallocate seq_cursor
truncate table #tlb1
create table #tlb1(
[type] VARCHAR(3), seqID  INT, [text] varchar(max)
)

declare @type varchar(3), @text varchar(max);
declare @seqID int;
declare seq_cursor cursor for 
select [type], max(seqID) from tbl group by [type]
open seq_cursor
fetch next from seq_cursor into @type,@seqID
while(@@fetch_status=0)
begin
    set @text= (select [text] from tbl where [type]=@type and seqID=@seqid);
    insert into #tlb1 values (@type, @seqID,@text);
    fetch next from seq_cursor into @type,@seqID    
end
select * from #tlb1
close seq_cursor
deallocate seq_cursor
truncate table #tlb1
萧瑟寒风 2024-12-20 06:05:20

尝试:

SELECT type, max(seqID),text 
FROM 'db'
GROUP BY type

就这么简单。

Try:

SELECT type, max(seqID),text 
FROM 'db'
GROUP BY type

As easy as that.

尸血腥色 2024-12-20 06:05:20

编辑解决方案。将此视为伪代码(因为我不熟悉 SQL Server 语法):

SELECT a.type, a.seqID, a.text FROM table a
 JOIN
(SELECT type, max(seqID) seqID FROM table GROUP BY type) b 
ON a.seqID = b.seqID AND a.type=b.type

EDITED solution. Consider this a psuedo-code (since I am not familiar with SQL server syntax):

SELECT a.type, a.seqID, a.text FROM table a
 JOIN
(SELECT type, max(seqID) seqID FROM table GROUP BY type) b 
ON a.seqID = b.seqID AND a.type=b.type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文