Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Sum function with other columns

Author  Topic 

poornimajayan
Starting Member

3 Posts

Posted - 2008-09-10 : 02:23:48
Hi all,
I need a query like
Name Age MGR salary
A 10 A 10
B 20 B 20
A 10 A 5
s 30 D 4


I want the output as follows.Since A is
duplicating, I want only one record for A and
the salary for it should be the sum of salary for all
records of A.

A 10 A 15
B 20 B 20
S 30 D 4


Can u give me a query?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-10 : 02:26:32
[code]
select Name, Age, MGR, sum(salary)
from yourtable
Group by Name, Age, MGR
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

poornimajayan
Starting Member

3 Posts

Posted - 2008-09-10 : 05:39:07
Thanks Khtan...But My actual requirement is like this:
I have 10 columns A,B,..J.
I want to select all columns from this table and (if value of column E=%CCT% and if duplicates are there(based on keys A,B,C) then I need to display one record only;but its column H should be a sum of all the records).Hope you got.
Then I cannot use this as I need to keep all 10 records in group by,which won't make row as distinct
Thanks,
Poornima
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-10 : 05:40:47
Post your table DDL, sample data and expected result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

poornimajayan
Starting Member

3 Posts

Posted - 2008-09-10 : 21:02:19
Suppose if I have 10 columns in a table, I want to list all 10 columns grouped by first 3 columns.
So I got it done as
select Name, Age,MGR,max(X),max(Y),max(Z), sum(salary)
from yourtable
Group by Name, Age, MGR
Go to Top of Page
   

- Advertisement -