Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 February 15th, 2005, 07:16 AM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to raamts
Default getting a query rows as columns

I have a table with 3 columns as follows.

EmpNo MonthNo Sal
 1 1 1500
 1 2 2500
like this..

can I get out put as follows...

           1 2 3 4 5(Months)
Empno 1500 2500 3500 4500 5500

like that.. can i get this output with a query.
There is no posibility to
change my database design. can any one of u help me


regards
--raam
 
Old February 24th, 2005, 04:06 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Using the CASE syntax and aggregate function SUM in combination, you can write a query as below to get the desired result.

SELECT EmpNo,
    Sum(Case MonthNo WHEN 1 then Sal else 0 END) Month_01,
    Sum(Case MonthNo WHEN 2 then Sal else 0 END) Month_02,
    Sum(Case MonthNo WHEN 3 then Sal else 0 END) Month_03,
    Sum(Case MonthNo WHEN 4 then Sal else 0 END) Month_04,
    Sum(Case MonthNo WHEN 5 then Sal else 0 END) Month_05,
    Sum(Case MonthNo WHEN 6 then Sal else 0 END) Month_06,
    Sum(Case MonthNo WHEN 7 then Sal else 0 END) Month_07,
    Sum(Case MonthNo WHEN 8 then Sal else 0 END) Month_08,
    Sum(Case MonthNo WHEN 9 then Sal else 0 END) Month_09,
    Sum(Case MonthNo WHEN 10 then Sal else 0 END) Month_10,
    Sum(Case MonthNo WHEN 11 then Sal else 0 END) Month_11,
    Sum(Case MonthNo WHEN 12 then Sal else 0 END) Month_12
FROM EmpSalary
group by EmpNo



Cheers,
Pooja Falor





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
Query to change rows into columns and vice versa miamikk SQL Language 0 November 13th, 2007 06:41 PM
SQL Query to convert Columns into Rows....again phix SQL Server 2000 11 November 11th, 2007 09:59 AM
Formatting sql query rows as columns with stack sastian PHP Databases 0 March 25th, 2005 04:51 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.