SQL Query using distinct and max?

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

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

SELECT tbl. * FROM ( SELECT type, MAX(seqID) FROM tbl GROUP BY type) maxes WHERE tbl. Type= maxes.

Type AND tbl. SeqID= maxes.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.

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.

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) be ON a. SeqID = b.

SeqID AND a. Type=b.type.

LIMIT is not Sql Server function – sll Oct 31 at 19:35 this won't work, it won't return the B, 2, Text2b data row only the one with seqID 3 – chris Oct 31 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. – allrite Oct 31 at 21:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions