Solved

Crosstab query in SQL

Posted on 2003-11-19
5
2,446 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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 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