?
Solved

Pivot, Crosstab with character field

Posted on 2003-02-26
5
Medium Priority
?
550 Views
Last Modified: 2012-06-21
I would like to pivot table from:

Guest    Question Value
------   -------- --------
Guest1   Gendar   Male
Guest1   Age      18 - 25
Guest1   Smoker   Yes
Guest2   Gendar   Female
Guest2   Age      26 - 35
Guest2   Smoker   No

To:

Guest   Gendar   Age      Smoker
------  ------   -------  ------
Guest1  Male     18 - 25  Yes
Guest2  Female   26 - 35  No


My problem is that the value field is not numeric, so I cannot use Aggregate Function, but If I put 'Value' column into 'Group By' clause, I get below result.

Guest   Gendar   Age      Smoker
------  -------  -------- -------
Guest1  Male     NULL     NULL
Geust1  NULL     18 - 25  NULL
Guest1  NULL     NULL     Yes
Guest2  Female   NULL     NULL
Guest2  NULL     26 - 35  NULL
Guest2  NULL     NULL     No

Value field is character (nvarchar(50)) type.

How can I accomplish this??

Is it even possible?

I would like to just run query instead of using Stored Procedure to create temporary table if I can.

One more twist to this is that Question may vary by guest.
So Guest1 may or may not have a record for Gendar, in which case I can just show nothing.

But if that's the only way, please put the actual stored procedure on how to put in temp table and all that.

Thanx in advance.
0
Comment
Question by:yas022100
[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
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:xxg4813
ID: 8030473
Hi,

U can count on UDF to help u implement your mission if u are running 2000.

---------------------------
create table question(Guest varchar(10), Question varchar(10), Value varchar(10))

insert into question values('Guest1', 'Gendar', 'Male')
insert into question values('Guest1', 'Age', '18-25')

CREATE FUNCTION udf_Value (
@Guest varchar(10),
@Question varchar(10))
RETURNS varchar(10)
AS
BEGIN
     return (select value from question where guest=@guest and question = @question)
END
GO

select guest, dbo.udf_Value(guest, 'Gendar') Gendar,
              dbo.udf_Value(guest,'Age') Age,
              dbo.udf_Value(guest,'Smoke') Smoke
from question group by guest

-----------------


Good luck!
0
 
LVL 18

Accepted Solution

by:
nigelrivett earned 1200 total points
ID: 8030495
If you just have these three entries then it's easy - if you have an indeterminate number then it's still easy but you need dynamic sql.

select Guest ,
Gendar = (select value from tbl t1 where t1.Question = 'Gendar' and t1.Guest = t.Guest) ,
Age = (select value from tbl t1 where t1.Question = 'Age' and t1.Guest = t.Guest) ,
Smoker = (select value from tbl t1 where t1.Question = 'Smoker' and t1.Guest = t.Guest)
from tbl t
group by Guest

0
 
LVL 3

Author Comment

by:yas022100
ID: 8030975
Exactly what I was looking for!
Thanx bunch!

Yas
0
 
LVL 4

Expert Comment

by:xxg4813
ID: 8034826
Hi,

Nigelrivett's code works fine with small set of records. U will find out the execution plan difference between my code and his.

Good luck!
0
 
LVL 3

Author Comment

by:yas022100
ID: 8035249
xxg4813,
I have not been exposed much with UDF, and I spent some time reviewing yours and it seems to work pretty well.  I don't have a big dataset to compare against, so I cannot compare the actual execution speed, but yours does make a bit cleaner Query String because I am creating query string on the fly.

So, I will go ahead and post a question to give you points also!

Thanx for help both of you!

0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

762 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