Forum Discussion

KrishnaGceb's avatar
KrishnaGceb
Copper Contributor
Feb 07, 2025

Need help with an SQL query without using a cursor

Hi there to all SQL gurus

So, here is the scenario. I have a #temp table in one of my SQL stored procedures which has only 2 columns, say Customer ID and Profile ID, and it has the below data

Customer ID          Profile ID

100001                  ABCD001

100001                  ABCD002

100002                  ABCD001

100002                  ABCD002

100003                  ABCD001

I need to write a query which selects only the Profile ID which is mapped to all the Customer IDs. In this case Customer ID 100001 and 100002 have both ABCD001 and ABCD002, but Customer ID 100003 has only Profile ID ABCD001, so, the output of the SQL should have only ABCD001. How do I do this without using a CURSOR? Would a CTE help? I am not very familiar with CTE, so if the solution is using a CTE, please give your suggestions in more detail. Thanks in advance

Resources