SQL Server CTE

I need to find using a CTE all Providers that belong to Multiple provider groups and I'm having a difficult time doing so.

Here's how I'd like for this to work. Find a provider, if the provider belongs to one group, show it. If the provider belongs to multiple groups, then update all of the provider groups for this provider to null.

WITH theResultSet AS
(
    SELECT DISTINCT(ProviderKey) AS col1 FROM ##tmpProviderGrp_RAMember
)
SELECT a.ProviderGroupKey
  FROM ##tmpProviderGrp_RAMember AS a
  JOIN ##tmpProviderGrp_RAMember as b ON a.ProviderKey = b.ProviderKey
)
update cte
set IDA_ProviderGroupKey= NULL
WHERE cnt > 1;
SharonBernalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
--Is this what you mean and need?

WITH cte_ResultSet AS
(
SELECT count(DISTINCT ProviderKey) as num, ProviderKey FROM ##tmpProviderGrp_RAMember
group by ProviderKey
)

SELECT a.ProviderGroupKey, case when cte.num > 1 then null else cte.num end as num, cte.ProviderKey
FROM ##tmpProviderGrp_RAMember a
   INNER JOIN cte_ResultSet cte ON a.ProviderKey = cte.ProviderKey
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SharonBernalAuthor Commented:
This is close, I've looked at it. It does not set the ProviderGroupKey to null in the table. There needs to be an update. The results look like below, the ProviderKey is in two groups, so the ProviderGroupKey needs to be set to null.

ProviderGroupKey      num      ProviderKey
xxxxx90004      NULL      CCC72237
xxxxx90004      NULL      CCC72237
xxxxx90004      NULL      CCC72237
xxxxx80004      NULL      CCC72237
xxxxx80004      NULL      CCC72237
xxxxx80004      NULL      CCC72237
0
SharonBernalAuthor Commented:
Also, on the example above the num should be 2. There are two groups with the same provider.
0
lcohanDatabase AnalystCommented:
Argh...I thought you had that just in the CTE for whatever reason so you can get the record set:
...
 update cte
 set IDA_ProviderGroupKey= NULL
 WHERE cnt > 1;
...

well in that case you can use the SELECT as a source to either populate a #TEMP table and then update the permanent table from it matching by the ProviderKey or just in a subquery
0
John_VidmarCommented:
UPDATE	a
SET	IDA_ProviderGroupKey = NULL
FROM	##tmpProviderGrp_RAMember	a
JOIN	(	SELECT	ProviderKey
		FROM	##tmpProviderGrp_RAMember
		GROUP
		BY	ProviderKey
		HAVING	COUNT(DISTINCT ProviderGroupKey) > 1
	) b	ON	a.ProviderKey = b.ProviderKey

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.