December 11, 2010 at 6:56 am
Hi All,
if employee names in a table are equal then that particular employees salary should be add total, Here i am giving one simple example:
input table is as below:
EmpID, EmpName, Salary
123, andy, 10000
111, Maddy, 12000
121, Raman, 25000
124, Hemanth, 40000
133, krishna, 50000
122, andy, 20000
134, Raman, 60000
143, Maddy, 30000
the output should be like:
EmpID, EmpName, Salary
123, andy, 10000
122, andy, 20000
143, Maddy, 30000
111, Maddy, 12000
121, Raman, 25000
134, Raman, 60000
124, Hemanth, 40000
133, krishna, 50000
EmpName, Salary
andy, 30000
Maddy, 42000
Raman, 85000
Hemanth, 40000
krishna, 50000
December 11, 2010 at 10:03 am
Looks like homework... You might want to look into aggregation task or GROUP BY in TSQL.
December 11, 2010 at 10:15 am
Here is a GROUP BY solution
CREATE TABLE #T(EmpId INT, EmpName VARCHAR(50),Salary MONEY)
INSERT INTO #T
SELECT 123, 'andy', 10000 UNION ALL
SELECT 111, 'Maddy', 12000 UNION ALL
SELECT 121, 'Raman', 25000 UNION ALL
SELECT 124, 'Hemanth', 40000 UNION ALL
SELECT 133, 'krishna', 50000 UNION ALL
SELECT 122, 'andy', 20000 UNION ALL
SELECT 134, 'Raman', 60000 UNION ALL
SELECT 143, 'Maddy', 30000
SELECT SUM(Salary),EmpName FROM #T GROUP BY EmpName
DROP TABLE #T
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply