Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 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