Solved

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

Posted on 2013-05-15
4
425 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 70

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

810 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