Solved

Crosstab query in SQL

Posted on 2003-11-19
5
2,453 Views
Last Modified: 2007-12-19
Can anyone tell me how to create a crosstab query in SQL? I am connecting to an Oracle view through an ODBC connection that contains the information I want to display in a crosstab table.
0
Comment
Question by:meghu
[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
5 Comments
 
LVL 15

Accepted Solution

by:
namasi_navaretnam earned 250 total points
ID: 9783120
You will need to use case statement to create crosstab queries in sql server.
Example

Select col1,  ( Case Col1 WHEN 'A' THEN 1 ELSE 0 END),
                  ( Case Col1 WHEN 'B' THEN 1 ELSE 0 END),
                  ( Case Col1 WHEN 'C' THEN 1 ELSE 0 END)
from MyTable

Also look at WITH CUBE, WITH ROLLUP keywords with group statement

Example,

Select Col1, Sum(Col2)
FROM MyTable
Group By Col1 With CUBE

HTH
0
 
LVL 34

Expert Comment

by:arbert
ID: 9793335
Can you post more details.  If you have a fixed amount of values, namasi's suggestion will work fine.  If you get quite a few values, it becomes cumbersome and you might want a more dynamic solution.
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9946487
Would you need further help? If not, why not close the issue? :)

Regards.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

626 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