How to select individual records by name?

My table Tagshas these entries

Application   ID                                      TagName
/Blogs        A75FB4D9-B0A2-45B1-A58D-9CC4E7FC1482    TagA
/News         E1BDEF9D-4285-464F-88DC-00495B59D2AE    TagA
/News         997F1721-335B-477A-9943-B91F0C21DE74    TagB
/Blogs        BB1CEE87-AF8A-44D6-8A4B-EAB138BBEF10    TagB

I want to return only the first match TagAand TagB, but the returned table should look exactly the same as above (Application, ID, TagName), so I expect this to be:

/Blogs        A75FB4D9-B0A2-45B1-A58D-9CC4E7FC1482    TagA
/News         997F1721-335B-477A-9943-B91F0C21DE74    TagB

How can i do this?

+3
source share
3 answers

SQL Server 2005 +

 SELECT Application, Id, TagName
 FROM
 (SELECT Application, Id, TagName,
  ROW_NUMBER()  OVER (partition by TagName order by TagName) rn
  FROM Table
 ) x 
 WHERE rn =1
+7
source

This can do the trick ...

SELECT MIN(Application), MIN(ID), TagName FROM Table GROUP BY TagName

In this example, you will get an identifier that is not predictable ...

+1
source

, , sql

SELECT Application, Id, TagName
FROM Tags t
JOIN (
    SELECT Application, TagName, MAX(InsertOrder) LatestInsert
    FROM Tags
    GROUP BY Application, TagName
) l ON l.Application = t.Application AND l.TagName = t.TagName AND l.LatestInsert = t.InsertOrder

, "" . : , ,

0

Source: https://habr.com/ru/post/1760748/


All Articles