Solved

How do I write a SQL query which aggregates FieldName/FieldValue pairs into a single column?

Posted on 2013-05-15
4
421 Views
Last Modified: 2013-05-15
Hi:

I'm trying to figure out a SQL query.

Basically, I have a View which looks like this:
Time, Event Name, FieldName, FieldValue
10:00am, Event1, FieldName1, FieldValue1
10:00am, Event1, FieldName2, FieldValue2
10:00am, Event1, FieldName3, FieldValue3
10:00am, Event1, FieldName4, FieldValue4
10:10am, Event2, FieldName1, FieldValue1
10:10am, Event2, FieldName2, FieldValue2
10:10am, Event2, FieldName3, FieldValue3
10:10am, Event2, FieldName4, FieldValue4
10:20am, Event3, FieldName1, FieldValue1
10:20am, Event3, FieldName2, FieldValue2
10:20am, Event3, FieldName3, FieldValue3
10:20am, Event3, FieldName4, FieldValue4

Open in new window


I'm looking for a query against that view which will return a result set as follows:
Time, Event Name, FieldsAndValues
10:00am, Event1, FieldName1:FieldValue1 FieldName2:FieldValue2 FieldName3:FieldValue3 FieldName4:FieldValue4
10:10am, Event2, FieldName1:FieldValue1 FieldName2:FieldValue2 FieldName3:FieldValue3 FieldName4:FieldValue4
10:20am, Event3, FieldName1:FieldValue1 FieldName2:FieldValue2 FieldName3:FieldValue3 FieldName4:FieldValue4

Open in new window



I know I'm going to do a SELECT with a Group By on Time and Event.
I also know I've got to do some sort of aggregation to collapse the related FieldName/FieldValue pairs into one (1) column.

How do I go about doing that?

Thanks,
JohnB
0
Comment
Question by:jxbma
4 Comments
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 39168557
0
 
LVL 6

Expert Comment

by:Manuel Marienne-Duchêne
ID: 39168586
0
 
LVL 7

Accepted Solution

by:
Ross Turner earned 400 total points
ID: 39168588
i created this example for you

http://sqlfiddle.com/#!3/a60f1/5

Create Table TEST 
(
[Time] varchar(30), 
[Event Name] varchar(30),
[FieldName] varchar(30), 
[FieldValue] varchar(30)
)


INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:00am',' Event1',' FieldName1',' FieldValue1');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:00am',' Event1',' FieldName2',' FieldValue2');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:00am',' Event1',' FieldName3',' FieldValue3');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:00am',' Event1',' FieldName4',' FieldValue4');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:10am',' Event2',' FieldName1',' FieldValue1');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:10am',' Event2',' FieldName2',' FieldValue2');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:10am',' Event2',' FieldName3',' FieldValue3');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:10am',' Event2',' FieldName4',' FieldValue4');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:20am',' Event3',' FieldName1',' FieldValue1');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:20am',' Event3',' FieldName2',' FieldValue2');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:20am',' Event3',' FieldName3',' FieldValue3');
INSERT INTO TEST(Time, [Event Name], FieldName, FieldValue) VALUES('10:20am',' Event3',' FieldName4',' FieldValue4');

Open in new window


select 
time,[Event Name],
 STUFF(
               (SELECT      ',' + SubTableUser.FieldName +':'+FieldValue
               FROM      TEST AS SubTableUser
               WHERE      SubTableUser.time = test.time
               FOR XML PATH('')), 1, 1, '') AS FIELDREF
from test
group by time,[Event Name]

Open in new window


sql
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 39169770
RossTurner gets extra for going the extra mile with my example and turning me on to SQL Fiddle.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now