Solved

# SELECT  Query

Posted on 2008-10-22
283 Views
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
Question by:chikku2003
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 37

Assisted Solution

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

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
``````
0

Author Closing Comment

ID: 31508765
thats exactly i was looking at
0

## Featured Post

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
###### Suggested Courses
Course of the Month7 days, 8 hours left to enroll