Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2008, 08:31 PM
Authorized User
 
Join Date: Dec 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to devendar
Default SQL 2005 Query

Hi,

  I need a Query for following table.

CREATE TABLE Test_Imp
(ID int,
Type1 int,
n1 char)

INSERT Test_Imp values(1,1,'a')
INSERT Test_Imp values(1,2,'b')
INSERT Test_Imp values(2,2,'c')
INSERT Test_Imp values(3,1,'d')

I need Output like

ID N1 N2
1 a b
2 Null c
3 d Null


Thanks in advance!
 
Old August 10th, 2008, 05:14 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
DECLARE    @Sample TABLE (ID INT, Type1 INT, n1 CHAR)

INSERT    @Sample
SELECT    1, 1, 'a' UNION ALL
SELECT    1, 2, 'b' UNION ALL
SELECT    2, 2, 'c' UNION ALL
SELECT    3, 1, 'd'

-- SQL 2000
SELECT        ID,
        MAX(CASE WHEN Type1 = 1 THEN n1 ELSE NULL END) AS N1,
        MAX(CASE WHEN Type1 = 2 THEN n1 ELSE NULL END) AS N2
FROM        @Sample
GROUP BY    ID
ORDER BY    ID

-- SQL 2005
SELECT    p.ID,
    p.[1] AS N1,
    p.[2] AS N2
FROM    @Sample AS s
PIVOT    (
        MAX(n1)
        FOR Type1 IN ([1], [2])
    ) AS p





Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Sql Server 2005, geographical data itHighway SQL Language 0 June 3rd, 2008 11:25 PM
SQL Server 2005 : Query problem wkm1925 SQL Language 1 May 11th, 2008 08:30 AM
SQL Server 2005 query for last of file edx SQL Language 10 July 12th, 2007 07:50 AM
sql query in vb.net 2005 PankajGarg10 General .NET 2 May 3rd, 2007 06:23 AM
SQL query in vb.net 2005 Amy18 Visual Basic 2005 Basics 3 May 2nd, 2007 11:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.