sql statement for same items where the flag is not the same across the board

so I have this table with lets say three fields

id primary key
staffid varchar
active bit

I would like to get a list of items where staffid has different active flags. You can have staffid with multiple locations, but they should have the same flag. So I want to generate a query of staffid where the "active" flag is not consistent (its not all either 0 or 1)

some staffid can occur 1 time in the list, (we want to ingnore those) some may have 2 occorunces, some 3 or even more. I would like to generate a list of those with more than 1 occurrence where the active flag is not consistent.

thanks in advance
damixaAsked:
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.

Scott PletcherSenior DBACommented:
SELECT staffid
FROM table_name
GROUP BY staffid
HAVING MIN(active) <> MAX(active)
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
<Very wild guess.  I do my own stunts too>
SELECT yt.id, yt.staffid, yt.active
FROM YourTable yt
JOIN (
   SELECT id, staffid, COUNT(DISTINCT active) as active_distinct_count
   FROM YourTable 
   GROUP BY id, staffid
   HAVING COUNT(active) > 1) ytdc ON yt.id = ytdc.id AND yt.staffid = ytdc.staffid AND yt.active_discount_count > 1

Open in new window

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
TONY TAYLORCommented:
Modified code from @jimhorn.

I think this is what you are looking for:

SELECT yt.id, yt.staffid, yt.active
FROM 
   YourTable yt
   INNER JOIN (
      SELECT staffid, COUNT(DISTINCT active) as active_distinct_count
      FROM YourTable 
      GROUP BY staffid
      HAVING COUNT(DISTINCT active) > 1
   ) ytdc ON yt.staffid = ytdc.staffid 
ORDER BY yt.staffid, yt.active

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kyle AbrahamsSenior .Net DeveloperCommented:
select distinct t1.staffId from
table t1 join 
table t2 on t1.staffId = t2.staffid and t1.active != t2.active

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
damixa, do you still need help with this question?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I think we've answered this multiple ways.
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.