Solved

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

Posted on 2013-05-15
4
427 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
[X]
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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2014 missing dll from Bin? 3 34
Convert time stamp to date 2 57
Using datetime as triggers 2 26
Specify timing interval fro change data 2 57
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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