Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:23+00:00 2026-05-27T05:00:23+00:00

I have a SQL table that I cannot change the structure of. The table

  • 0

I have a SQL table that I cannot change the structure of.

The table Employee_Team has 5 columns: TeamID, Employee1, Employee2, Employee3 and Employee4. The fields of columns Employee1, Employee2, Employee3 and Employee4 contain an EmployeeID. Each Employee ID has a City in the table Employee. It’s possible to have multiple employees on the same row of Employee_Team that are living in the same town. There will always be at least 1 employee that will live in the town I’m looking for.

example http://www.atriasoft.com/table.png

Example:

I want the first employee that lives in New York. In the case of TeamID 01, it will be Employee2 (I don’t want Employee 4 even though his city is New York) and for the case of TeamID 02, it will be Employee1 (I still don’t want the second employee that lives in New York).

I currently have a code that gives me every employee that lives in New York:

SELECT * 
  FROM Employee_Team
 INNER JOIN Employees
    ON Employees.EmployeeID = Employee_Team.Employee1 OR
       Employees.EmployeeID = Employee_Team.Employee2 OR
       Employees.EmployeeID = Employee_Team.Employee3 OR
       Employees.EmployeeID = Employee_Team.Employee4
 WHERE Employees.City = ‘New York’

What I am looking for is a code that takes only the first employee living in New York.
Any hints will be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:00:23+00:00Added an answer on May 27, 2026 at 5:00 am

    You actually can safely “change” the definition of your table, virtually, by using it as the basis of a view:

     CREATE VIEW NormalTeams (TeamID, EmployeeID, TeamMemberNumber) AS
         SELECT TeamID, Employee1, 1 FROM Employee_TEAM UNION ALL
         SELECT TeamID, Employee2, 2 FROM Employee_TEAM UNION ALL
         SELECT TeamID, Employee3, 3 FROM Employee_TEAM UNION ALL
         SELECT TeamID, Employee4, 4 FROM Employee_TEAM
    

    Now, NormalTeams will let you SELECT the data the way you expect to be able to:

     SELECT TeamID, MIN(TeamMemberNumber)
        FROM NormalTeams T INNER JOIN Employees E ON T.EmployeeiD = E.EmployeeID
        WHERE E.City = 'New York'
        GROUP BY TeamID
    

    will give you the first team member “number” for New York.

     SELECT TeamID, City, MIN(TeamMemberNumber)
        FROM NormalTeams T INNER JOIN Employees E ON T.EmployeeiD = E.EmployeeID
        GROUP BY TeamID, City
    

    will give you the more general solution for every City / Team combination.

    You can join these results back to NormalTeams to get the employee ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a SQL Server table in production that has millions of rows, and
I have a sql table of payroll data that has wage rates and effective
In SQL Server 2005 I have an id field in a table that has
I have a legacy data table in SQL Server 2005 that has a PK
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a MS SQL table that I don't have any control over and
I have an ADOQuery that inserts a record to SQL Server 2005 table that
On MS SQL Server 2005, I have a table that I want to replace.
I use SQL Server 2005. I have a simple logging table that my web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.