Solved

I need help building Pivot tables and cross tab queries in SQL

Posted on 2011-03-21
5
227 Views
Last Modified: 2012-05-11
Hi Experts,
Is there a way to dynamically build cross tab queries and pivot tables in SQL?  If I build an application that will allow the user to select up to 3 fields, could this be done?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
[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
5 Comments
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35186846
0
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
ID: 35188311
Hi,
You can use one of followings:
select
ColID,
      max(Case When ColumnName='Col1' then  ColumnValue End) Col1,
      max(Case When ColumnName='Col2' then  ColumnValue End) Col2,
      max(Case When ColumnName='Col3' then  ColumnValue End) Col3
from tbl
Group by ColID

Or

SELECT *
FROM
(select ColumnName,ColumnValue,ColID from tbl
) as tab
PIVOT
 (
 max(ColumnValue)
 FOR ColumnName
 IN (ColID,ColumnName,ColumnValue)
 )
 AS p

Thanks
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35188593
you can go totally dynamic with dynamic sql....

or you could go partially dynamic if you have selection criteria that
allow for it...

what are you allowing the users to select ....

the pivoted columns (range)?
or the columns against which the data is to be displayed?

can you handle the naming of the columns on the returned result set?
0
 

Author Comment

by:mainrotor
ID: 35199094
Lowfatspread, I am allowing the users to select the columns.
0
 

Author Comment

by:mainrotor
ID: 35416951
Lowfatspread,
I am allowing the users to select the columns against which the data is to be displayed.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.

734 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