Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

March 05, 1999 12:00 AM

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

Windows IT Pro
InstantDoc ID #14145
Rating: (0)

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


Related Content:

ARTICLE TOOLS

Comments
  • Petter Enqvist
    11 years ago
    Mar 13, 2001

    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

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.