0

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.

5
  • which server you are using? mysql/oracle/sql server?
    – sll
    Oct 31, 2011 at 19:28
  • Shouldn't it be " So in the above example I would want the row that has A, 3, Text3a" and not " So in the above example I would want the row that has A, 3, Text3a and B, 2, Text2b returned." ??
    – Ritesh
    Oct 31, 2011 at 19:36
  • no i want b, 2, Text2b returned as well
    – cdub
    Oct 31, 2011 at 19:40
  • @chris , before downvoting, you might want to clarify the question. We are here to help each other!
    – Ritesh
    Oct 31, 2011 at 21:05
  • @chris : I understood the question (you want max seqID row for each type). See if my solution works for you now.
    – Ritesh
    Oct 31, 2011 at 21:12

8 Answers 8

1
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.

0

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)
0
SELECT tbl.*
FROM
   ( SELECT type, MAX(seqID)
     FROM tbl
     GROUP BY type) maxes
WHERE
   tbl.type= maxes.type AND
   tbl.seqID= maxes.seqID
0
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
0

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
0
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
0

Try:

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

As easy as that.

-1

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
3
  • LIMIT is not Sql Server function
    – sll
    Oct 31, 2011 at 19:35
  • this won't work, it won't return the B, 2, Text2b data row only the one with seqID 3
    – cdub
    Oct 31, 2011 at 19:37
  • @sll : My answer said 'if' LIMIT is supported, then you can use it. What do you mean by 'not Sql Server' function? Is the OP talking about a particular DB server type? I didn't see it in his/her question.
    – Ritesh
    Oct 31, 2011 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.