Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2460
  • Last Modified:

Crosstab query in SQL

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
meghu
Asked:
meghu
  • 2
1 Solution
 
namasi_navaretnamCommented:
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
 
arbertCommented:
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
 
namasi_navaretnamCommented:
Would you need further help? If not, why not close the issue? :)

Regards.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now