Cross tab queries with SQL Server 2000


By:   |   Updated: 2006-06-15   |   Comments (2)   |   Related: 1 | 2 | 3 | More > Scripts

Problem

In SQL Server 2000 there is no simple way to create a cross tab query.  Generally crosstab queries are only used for reporting or if you want to display some information in a grid on an application screen.  Other then displaying or reporting on data there isn't a real need for this type of query.  But when the need arises for either one of these reasons, there is no simple way of doing this in SQL Server 2000.  This may come as a big surprise to people that have been doing this in Microsoft Access for quite some time. With Microsoft Access there is a built in option to produce crosstab queries, but not so with SQL Server 2000.

Solution

As mentioned above there is no simple way to produce a cross tab query, but several implementations have been produced and are available on the internet.  Finding all of these options, understanding them, implementing and testing takes some time, so to make your life a little easier we have gathered a list of some useful variations on how to produce cross tab queries in SQL Server 2000.

The following solution is pretty straight forward and offers a lot of flexibility.  Although it met my needs, take a look at the other options as well to see which one makes the most sense for your needs.

This is a list of various implementations and methods for creating crosstab queries for SQL Server 2000. There are probably others out there as well.

Some other options that do not require T-SQL coding include the following:

Next Steps
  • Take a look at these various methods for creating crosstab queries
  • Select a method that works best for your environment
  • Modify these methods to implement the right solution for your needs


Last Updated: 2006-06-15


get scripts

next tip button



About the author





Comments For This Article




Monday, October 30, 2017 - 2:07:56 PM - Greg Robidoux Back To Top (68976)

Hi Elaine,

what version of SQL Server are you using? If you are using SQL 2005 or later you should look at using PIVOT.  Take a look at this other tip: https://www.mssqltips.com/sqlservertip/1019/crosstab-queries-using-pivot-in-sql-server/

Thanks
Greg


Friday, October 20, 2017 - 4:46:16 PM - Elaine Filos Back To Top (68582)

 Can you help? This is the data I have and the code I am using.

Two tables with this code

Select t1. ID#, t2.CHarge_Code

from Table 1 t1

Left outer join Table 2 t2 on t2.ID = t1.ID

where t2.charge_codes in

('2512', '25006', '14226') 

This is the result:

ID # CHARGE CODE  
1602383 25012  
1602383 25006  
     
     
I want to be able to displayall of  the charge codes data on one line.  The charge code field is not an array.
     
ID # Charge code 1 charge Code 2
1602383 25012 25006
     

 



download


Recommended Reading

Crosstab queries using PIVOT in SQL Server

Script to create dynamic PIVOT queries in SQL Server

Searching and finding a string value in all columns in a SQL Server table

Run same command on all SQL Server databases without cursors

List columns and attributes for every table in a SQL Server database





get free sql tips
agree to terms


Learn more about SQL Server tools