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 7857759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:08:23+00:00 2026-06-02T21:08:23+00:00

I have a staff database table containing staff members, with user_no and user_name columns.

  • 0

I have a staff database table containing staff members, with user_no and user_name columns. I have another, department, table containing the departments which staff can be members of, with dept_no and dept_name as columns.

Because staff can be members of multiple departments, I have a third, staff_dept, table with a user_no column and a dept_no column, which are the primary keys of those other two tables. This table shows which departments each member of staff belongs to and contains one row for each user/department intersection.

I would like to have an output in the form of a spreadsheet (CSV file, whatever; I’ll be fine mangling the results into a usable form after I’ve got them) with one column for each department, and one row for each user, with an X appearing at each intersection, as defined in staff_dept.

Can I write a single SQL query which will achieve this result? or will I have to do some “real” programming (because it’s not a “real” program until you’ve nested three or four for loops, obviously) to collect and format this data?

  • 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-06-02T21:08:25+00:00Added an answer on June 2, 2026 at 9:08 pm

    This can be done with a PIVOT table (using SQL Server):

    SELECT user_name, [dept1name], [dept2name], [dept3name], ...
    FROM
        (SELECT s.user_name, d.dept_name,
         case when sd.user_no is not null then 'X' else '' end as matches
         from staff s
         cross join department d
         left join staff_dept sd on s.user_no = sd.user_no and d.dept_no = sd.dept_no
        ) AS s
    PIVOT
    (
        min(matches)
        FOR dept_name IN ([dept1name], [dept2name], [dept3name], ...)
    ) AS pvt
    order by user_name
    

    Demo: http://www.sqlfiddle.com/#!3/c136d/5

    Edit: To generate the PIVOT query dynamically from the list of departments in the table, you would make use of dynamic SQL, i.e., generate the code into a variable and use sp_executesql helper stored procedure. Here’s an example: http://www.sqlfiddle.com/#!3/c136d/14

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

Sidebar

Related Questions

I have the following table which holds data on customers and staff. Would it
I have a table of Users (tblUsers) which contains details of University staff. I
I have a database (SQL) with the table Staff with two records in it.
I have a table in the database containing files. I don't know their filetype.
I have a database which stores the work my staff do. One particular query
I have a database table with N records, each of which needs to be
I have been given a staff list which is supposed to be up to
I have a database that is similar to the following: create table Store(storeId) create
I have a database table and a corresponding entity class (POCO with change tracking
I have one field in database table with following lines show_email_icon= show_hits= feed_summary= page_title=

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.