Avatar of Brock
Brock
Flag for Canada asked on

Sql select problem

Hello,

The following is the data that I need to model in sql :

runcntl          OPRID      EMPLID       cntrct    DEPTID              
TEST               L             2001548      148137      8437             
TEST               L              2001548 148137      8500             
TEST             L               2001548 148139      8450             
TEST            L             2001548      148139      8460             
TEST            L               4086876      148140      8370             
TEST            L               4088876  148151      8370             
TESTS            L               4088876      148152      8370             
TEST           L              9700994 148150      8360             
TEST           L             9700994      148150      8370             
TEST           L             4086876      148152      8370             


I do not want  the values below to appear in the result

TEST   L      4088876      148151      8370
TEST   L      4088876      148152      8370
TEST   L      4086876      148152      8370


So I only want the EMPLID who have different deptids either within a single cntrct or across multiple cntrcts.

Can this be done without a CTE?

Thanks, Brock
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Brock

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
David Todd

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Olaf Doschke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Brock

ASKER
Thank you for your responses.  Working on this - this morning.

Brock
Brock

ASKER
Thank you, to Both!

Olaf - for some reason my boss does not want to see me using CTE.  We get peer reviews of code and someone complained :-(
SOLUTION
Olaf Doschke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Brock

ASKER
Thank you, Olaf.

Unfortunately,  I don't have much say in the matter :-)

Yes, I saw the multiple solutions. And I am greatful.

I  have not been here lately and wanted to reward both options...'this is my solution' was a bit confusing.

Thank you,
Brock
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes