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?