Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 



How can I do a crosstab function using standard TSQL in SQL Server?

RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

A. It's obviously easier to use a product that has this sort of functionality built-in - e.g. Excel, but it is possible to do it in standard SQL, though there the query has to be hard-coded to the number of columns/values required.

Take the following table

Product_Code Criteria_Code Value
------------ ------------- -----
100011             1         A
100011             2         B
100011             3         C
100011             4         D
100012             1         E
100012             2         B
100012             3         F
100012             4         D

Which you want to view as follows

Product_Code Criteria_1 Criteria_2 Criteria_3 Criteria_4
------------ ---------- ---------- ---------- ----------
100011            A          B          C          D
100012            E          B          F          D

If you don't have a CASE statement (e.g. pre SQL 6.0) then use the following :-

SELECT Product_Code,
    Criteria_1=MAX(substring(Value, 1, datalength(Value) * (1 -
abs(sign(Criteria_Code - 1))))),
    Criteria_2=MAX(substring(Value, 1, datalength(Value) * (1 -
abs(sign(Criteria_Code - 2))))),
    Criteria_3=MAX(substring(Value, 1, datalength(Value) * (1 -
abs(sign(Criteria_Code - 3))))),
    Criteria_4=MAX(substring(Value, 1, datalength(Value) * (1 -
abs(sign(Criteria_Code - 4)))))
FROM <tbl>
GROUP BY Product_Code

If you do have the CASE statement available then use :-

SELECT Product_Code,
    Criteria_1=MAX(substring(Value, 1, datalength(Value) * (CASE
Criteria_Code WHEN 1 THEN 1 ELSE 0 END))),
    Criteria_2=MAX(substring(Value, 1, datalength(Value) * (CASE
Criteria_Code WHEN 1 THEN 2 ELSE 0 END))),
    Criteria_3=MAX(substring(Value, 1, datalength(Value) * (CASE
Criteria_Code WHEN 1 THEN 3 ELSE 0 END))),
    Criteria_4=MAX(substring(Value, 1, datalength(Value) * (CASE
Criteria_Code WHEN 1 THEN 4 ELSE 0 END)))
FROM <tbl>
GROUP BY Product_Code


End of Article



Reader Comments
Thanks for the tip! I've been killing myself over this problem...

Btw, I think there's an error in the CASE version of the solution. What now reads
'...CASE Criteria_Code WHEN 1 THEN 2 ELSE 0 END...'
should in fact be
'...CASE Criteria_Code WHEN 2 THEN 1 ELSE 0 END...'
The same error is present on the other rows (except the first) as well.

Thank you! Petter Enqvist

Petter Enqvist March 13, 2001


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Events SQL Server Unleashed EMEA

Deep Dive into Windows Server 2008 R2 presented by John Savill

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement