[dba-SQLServer] Crosstabs - Pivots

Ken Stoker kens.programming at verizon.net
Thu Jul 22 12:15:08 CDT 2004


Someone may have something better, but something like the following is the
way I get a crosstab-like query in SQL Server:

SELECT JOB, CASE WHEN LEDGER = '04PRA' THEN BALANCE END AS '04PRA',
	CASE WHEN LEDGER = '04PRB' THEN BALANCE END AS '04PRB'
FROM ...

This assumes that you know the number of columns that you are
'transforming'.  Hope this helps.

Ken

-----Original Message-----
From: dba-sqlserver-bounces at databaseadvisors.com
[mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Haslett,
Andrew
Sent: Thursday, July 15, 2004 12:23 AM
To: 'dba-sqlserver at databaseadvisors.com'
Subject: [dba-SQLServer] Crosstabs - Pivots

Greetings all.  The infamous crosstab question!

I've managed to get the data I require in the following format

JOB     LEDGER   BALANCE
0862	04PRA	    68444.85
0862	04PRB	    44783.0
0915	04PRA	    5092.0
0915	04PRB	    46000.0
1045	04PRA	    5400.0
1045	04PRB	    5400.0

How can I transform it to this:
JOB      04PRA        04PRB
0862     68444.85   44783.0
0915     5092.0       46000.0
1045     54000.0     5400.0


A few samples on the net show you how to do it, but they all require a
numeric value to pivot on so you can use an aggregate function.

I don't want to aggregate anything - just transform (pivot) them...

Thanks in advance

Andrew

IMPORTANT - PLEASE READ ******************** 
This email and any files transmitted with it are confidential and may 
contain information protected by law from disclosure. 
If you have received this message in error, please notify the sender 
immediately and delete this email from your system. 
No warranty is given that this email or files, if attached to this 
email, are free from computer viruses or other defects. They 
are provided on the basis the user assumes all responsibility for 
loss, damage or consequence resulting directly or indirectly from 
their use, whether caused by the negligence of the sender or not.
_______________________________________________
dba-SQLServer mailing list
dba-SQLServer at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
http://www.databaseadvisors.com





More information about the dba-SQLServer mailing list