SQL Select - identify record discrepancies

I need some help with SQL Select to identify record discrepancies.  I have a table containing a list of products with each product containing 3 feature descriptions.  I also have a group ID column that groups related sets of products together.  WIthin each product group, I need to identify the group of products that do not contain the same set of new features.    For example;

GroupID  Product ID NewFeature01 NewFeature02 NewFeature03
  1                  21                   Apple            Peach
  1                  22                   Peach            Apple
  1                  23                                          Peach               Apple
  2                  24                    Cherry
  2                  25                    Orange  
  3                  26                                          Nuts
  3                  27                     Nuts

Using the above example, only product group does not have the same set of new features.   Therefore, the expect result from this SQL Select query would:
GroupID  Product ID NewFeature01 NewFeature02 NewFeature03
  2                  24                    Cherry
  2                  25                    Orange
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.

Ryan ChongSoftware Team LeadCommented:
probably still not the best solution but something like this seems working for me.

declare @yourTable table
	GroupID int,
	[Product ID] int,
	NewFeature01 varchar(50),
	NewFeature02 varchar(50),
	NewFeature03 varchar(50)
insert into @yourTable 
(1, 21, 'Apple', 'Peach', null),
(1, 22, 'Peach', 'Apple', null),
(1, 23, null, 'Peach', 'Apple'),
(2, 24, 'Cherry', null, null),
(2, 25, 'Orange', null, null),
(3, 26, null, 'Nuts', null),
(3, 27, 'Nuts', null, null),

(4, 28, 'Apple', 'Peach', null),
(4, 29, 'Peach', 'Apple', null),
(4, 30, 'whatever', 'Peach', 'Apple');

with cte as
	select GroupID, NewFeature, count(NewFeature) Cnt,
	rank() over (partition by GroupID order by count(NewFeature)) idx
		select GroupID, NewFeature01 as NewFeature from @yourTable
		union all
		select GroupID, NewFeature02 from @yourTable
		union all
		select GroupID, NewFeature03 from @yourTable
	) a
	where NewFeature is not null
	Group by GroupID, NewFeature
), cte2 as
	select groupid
	from cte
	where cnt = 1 or idx > 1
	group by groupid
select a.*
from @yourTable a
inner join cte2 b
on a.groupid = b.groupid
order by GroupID

Open in new window

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
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.