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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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)
Jim HornMicrosoft SQL Server Data DudeCommented:
<Very wild guess.  I do my own stunts too>
SELECT yt.id, yt.staffid, yt.active
FROM YourTable yt
   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

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

I think this is what you are looking for:

SELECT yt.id, yt.staffid, yt.active
   YourTable yt
      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

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

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