question

souravagar avatar image
souravagar asked

Query to display unique comments based on certain conditions

TABLE DEFINITION Column Name Comments CustomerID INT SequenceNo INT Comments VARCHAR(MAX) CUSTOMER TABLE CustomerID SequenceNo Comments 1 1 ABC D 1 2 CDE 1 3 ABC 1 4 ABC D 1 5 CDE 1 6 abc 2 7 ABC DEF 2 8 2 9 ABC DEF 2 10 DEF 2 11 XYZ 123 2 12 ABC 3 13 PQ RST OUTPUT CustomerID SequenceNo Comments 1 3 ABC 1 4 ABC D 1 5 CDE 1 6 abc 2 8 2 9 ABC DEF 2 10 DEF 2 11 XYZ 123 2 12 ABC 3 13 PQ RST Records should be filtered by 1. Display only Unique Comments from Customer Table for all the customers, 2. If Comments are same then display the row which has maximum SequenceNo ![alt text][1] [1]: /storage/temp/2551-11698890-10154036087194466-3495075191431781854-o.jpg
sql-server-2008sqltsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Tom Staab avatar image
Tom Staab answered
Because you want "ABC" separate from "abc", the following will only work as long as your collation is case sensitive. If it is not, you could try adding "COLLATE SQL_Latin1_General_CP1_CS_AS" or something similar to the Comments column to make it case sensitive. SELECT CustomerID , SequenceNo = MAX(SequenceNo) , Comments FROM Customers GROUP BY CustomerID, Comments ORDER B CustomerID, SequenceNo ;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.