Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return data containing the same value within a string?

I have been asked to gather data where all strings within the defaultmessage column are similar, the issue is the message is unknown and so is the string count value, I have no information to search for in the WHERE clause. I have tried to run a SELF JOIN however, this did not work as it just returned all values, the table contains over 10000 rows of data, where the strings vary but are sometimes similar to each other, could someone please explain how I could go about to return this?

Table: This is just an example table of what I am trying to gather

TravellerID Traveller DefaultMessage
1 J: 182 I travelled to US
2 J: 192 I travelled to IND
3 K: 901 I travelled to GBR
4 M: 531 I travelled to US
5 N: 231 I travelled to AUS

So basically, I need to gather data where the default message is the same across different ids, please let me know if you require further information, tried to explain this the best I could.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please note again, I have no idea what the default message actually specifies, I just need to find entries of data that have the same message within the column

Output:

TravellerID Traveller DefaultMessage
1 J: 182 I travelled to US
4 M: 531 I travelled to US

>Solution :

On SQL Server, we could use COUNT() as a window function here:

WITH cte AS (
    SELECT *, COUNT(*) OVER (PARTITION BY DefaultMessage) cnt
    FROM yourTable
)

SELECT TravellerID, Traveller, DefaultMessage
FROM cte
WHERE cnt > 1
ORDER BY DefaultMessage, TravellerID;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading