Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 November 10th, 2007, 08:09 AM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by phix
 This gets me close, but it lists all 24 different region/Tier
combinations.

select [PType],
    (case when ([region] = 'R1' and [tier] = '1') then [NewRate] end) As R1,
    (case when ([region] = 'R2' and [tier] = '1') then [NewRate] end) As R2,
    (case when ([region] = 'R3' and [tier] = '1') then [NewRate] end) As R3,
    (case when ([region] = 'R4' and [tier] = '1') then [NewRate] end) As R4,
    (case when ([region] = 'R5' and [tier] = '1') then [NewRate] end) As R5,
    (case when ([region] = 'R6' and [tier] = '1') then [NewRate] end) As R6
from tblFY06FY07Compare
group by [region], [PType], [Tier], [NewRate]
order by [PType]



I get this...
RT01 45.90 NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL 47.20 NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL 52.50 NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL 57.05 NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL 45.90 NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL 52.20
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT01 NULL NULL NULL NULL NULL NULL
RT02 43.70 NULL NULL NULL NULL NULL
RT02 NULL NULL NULL NULL NULL NULL
RT02 NULL NULL NULL NULL NULL NULL
Hi!
Did you manage to sort this problem? Well its quite sometime i know but i hope i would share this experience with you as i landed into the same task and came up with an interesting solution. Just add a MAX in your CASE and null will be eliminated by aggregate!!!!

Your new select should look like this now

select [PType],
     MAX(case when ([region] = 'R1' and [tier] = '1') then [NewRate] end) As R1,
     MAX(case when ([region] = 'R2' and [tier] = '1') then [NewRate] end) As R2,
     MAX(case when ([region] = 'R3' and [tier] = '1') then [NewRate] end) As R3,
     MAX(case when ([region] = 'R4' and [tier] = '1') then [NewRate] end) As R4,
     MAX(case when ([region] = 'R5' and [tier] = '1') then [NewRate] end) As R5,
     MAX(case when ([region] = 'R6' and [tier] = '1') then [NewRate] end) As R6
from tblFY06FY07Compare
group by [region], [PType], [Tier], [NewRate]
order by [PType]

 
Old November 11th, 2007, 09:59 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

No... he was looking for a SUM... Peso's addition to my code does it...

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query to Convert Columns into Rows Niaz Oracle 3 February 5th, 2010 07:08 AM
convert columns to rows in sql hoangmv0101 SQL Language 0 January 2nd, 2007 02:04 AM
Formatting sql query rows as columns with stack sastian PHP Databases 0 March 25th, 2005 04:51 AM
getting a query rows as columns raamts SQL Language 1 February 24th, 2005 04:06 AM
SQL Query to Convert Columns into Rows Niaz SQL Server 2000 2 April 20th, 2004 01:36 AM





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