Solved

SELECT  Query

Posted on 2008-10-22
3
258 Views
Last Modified: 2013-12-07
I have column FY and Sales for a employee.
I wouldl like to get all the employees sales for FY and FY- 1 in one row. PLease see the snapshot attached for more clarity
query.bmp
0
Comment
Question by:chikku2003
3 Comments
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 200 total points
ID: 22777150
select empId, sum(case when fy='2008' then sales else 0 end) as Sale-Fy2008,
sum(case when fy='2007' then sales else 0 end) as Sale-Fy2007,
sum(case when fy='2006' then sales else 0 end) as Sale-Fy2006
from your_table
group by empid
0
 
LVL 6

Accepted Solution

by:
openshac earned 300 total points
ID: 22777220

CREATE TABLE #temp
( ID int, empID int, FY int, Sales int)
 
INSERT INTO #temp VALUES (1, 1, 2008, 10000)
INSERT INTO #temp VALUES (2, 1, 2007, 15000)
INSERT INTO #temp VALUES (3, 1, 2006, 20000)
INSERT INTO #temp VALUES (4, 2, 2008, 7500)
INSERT INTO #temp VALUES (5, 2, 2007, 2500)
INSERT INTO #temp VALUES (6, 2, 2006, 12500)
INSERT INTO #temp VALUES (7, 3, 2008, 1000)
INSERT INTO #temp VALUES (8, 3, 2007, 2000)
INSERT INTO #temp VALUES (9, 3, 2006, 3000)
 
SELECT empID, 
SUM(CASE WHEN FY = 2008 THEN Sales ELSE 0 END),
SUM(CASE WHEN FY = 2007 THEN Sales ELSE 0 END),
SUM(CASE WHEN FY = 2006 THEN Sales ELSE 0 END)
FROM #temp
GROUP BY empID
 
DROP TABLE #temp

Open in new window

0
 

Author Closing Comment

by:chikku2003
ID: 31508765
thats exactly i was looking at
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create Index on a Materialized View 5 22
sql server insert 12 30
T-SQL: "HAVING CASE" Clause 1 25
MS SQL Server COnditional Where statement 7 44
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question