Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine fields?

Status
Not open for further replies.

mrman2289

Technical User
Jun 21, 2008
1
0
0
GB
Hi there, not sure exactly how to explain this but here goes.

I have a table containing a student ID and multiple tasks for each student as task1subject, task1, task2subject, task2, task3subject, task3.

I want to know if its possible to build a query which will group the tasks under one name on multiple rows as it were. Giving me an ouput where I can sort by the subject, or if there are any other easy ways of getting the same result, many thanks =)
 
Assuming that task1subject, task1, task2subject, task2, task3subject, task3, etc. are separate fields, you usually do this with a union query
Code:
Select StudentID, Task1Subject As TaskSubject, Task1 As Task
From TheTable

UNION

Select StudentID, Task2Subject, Task2
From TheTable

UNION

Select StudentID, Task3Subject, Task3
From TheTable

[blue]etc.[/blue]

Yiu can include IS NOT NULL conditions to eliminate records where there is no data for a Task or TaskSubject.
 
I'd replace UNION with UNION ALL, just to make sure no rows "disappeares".

(Will probably not happen in this specific case, but may in similar cases.)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top