Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2008, 08:31 PM
Authorized User
 
Join Date: Dec 2006
Location: Texas, TX, USA.
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!
Reply With Quote
  #2 (permalink)  
Old August 10th, 2008, 05:14 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.