Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to merge rows of records into single row

Author  Topic 

Sunny_175
Starting Member

2 Posts

Posted - 2007-05-20 : 11:52:21
Dear All,
I would like to ask a question about how to extract rows of records into one record with numbers of new columns.

I have two tables, ([Score] and [Scoring Items]):
[Score]
Score ID ¡V Auto Number
Staff Name ¡V String
Date ¡V Date/Time
Scoring Item ¡V Number
Score ¡V number
Time Modified ¡V Date/Time

[Scoring Items]
Item ID ¡V Auto Number
Scoring Item ¡V String

Here are the sample data:
[Score]
Score ID Staff Name Date Scoring Item Score Time Modified
1 Ken 19/5/2007 1 5 9/5/2007 18:39:15
2 Ken 19/5/2007 2 10 9/5/2007 18:39:15
3 Ken 19/5/2007 3 2 9/5/2007 18:39:15
4 Mary 20/5/2007 1 6 9/5/2007 18:41:05
5 Mary 20/5/2007 2 4 9/5/2007 18:41:05
6 Mary 20/5/2007 3 10 9/5/2007 18:41:05
7 Jack 16/5/2007 1 5 9/5/2007 18:43:00
8 Jack 16/5/2007 2 2 9/5/2007 18:43:00
9 Jack 16/5/2007 3 10 9/5/2007 18:43:00

[Scoring Items]
Item ID Scoring Item
1 Customer Service
2 Working Method
3 Performance

Now, I want to make a query to show the name of scoring item as a column and merge several rows into one row as follow:
Staff Name Date Customer Service Working Method Performance Time Modified
Ken 19/5/2007 5 10 2 9/5/2007 18:39:15
Mary 20/5/2007 6 4 10 9/5/2007 18:41:05
Jack 16/5/2007 5 2 10 9/5/2007 18:43:00

I don¡¦t know how to use SQL to make this output, is this possible using SQL to add columns upon records from table? And how can I write a SQL statement to make the above output?

Anyone can help me? Thanks in advance.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-20 : 12:00:34
look for pivoting around here.

and look here:
http://weblogs.sqlteam.com/jeffs/archive/0001/01/01/4842.aspx

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Sunny_175
Starting Member

2 Posts

Posted - 2007-05-21 : 10:35:44
Thanks a lot. I'll study the article.
Go to Top of Page
   

- Advertisement -