Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Crosstab query in SQL

Posted on 2003-11-19
5
Medium Priority
?
2,458 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 1000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.

715 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