SQLTeam.com | Weblogs | Forums

Not sure how to do this select

I have 2 tables
id, employeeID

id, timekeeper, Alt1Timekeeper, Alt2Timekeeper

The id in employee in the key in all timekeeper fields


1, 3456
2, 5689


1, 1, 23,45
2,33,1, 66
3, 67, 55, 1
4, 57, 77, 88, 54
5, 1, 62, 74,99

I have the employee ID.
Notice that employee 3456 is either a timekeeper or alt in either orgs 1,2,3,5

I want to do a select statement that pulls back all the org record ids that employee 1 is in????

Can you provide readily consumable DDL and sample data? It's confusing with what you have provided. Is the employee ID in the TimeKeeper, Alt1Timekeeper or Alt2TTimekeeper columns? You may have to unpivot the organization table first, but unclear with what is provided. Also, your data example has 5 columns for the last 2 rows in Organization

Is that your actual table design for Organization or is that the final result you want?
Empid, org1id,org2id,org3id

DECLARE @id_to_find int
SET @id_to_find = 1

FROM Organization
WHERE @id_to_find IN (timekeeper, Alt1Timekeeper, Alt2Timekeeper)