Special SQL query on SQL Server 2005 with sum()

I have a table T1, says with column C1,C2 and C3
I need the sum of C2
But first there is a condition on C3 : easy : select ... where C3 = 'MyData'
But in fact I only need the sum of maximum values C2 regarding value of C1
I explain with a sample table T1

C1   C2       C3
A       5       MyData
A     10       MyData
A     15       MyData
B    100       MyData
B     10       MyData

The sum here show be the one for C1 = 'A' third line 15 which is the max with C1 = 'A' + the one for C2 = 'B' where the max value is 100

So here, my select should return 115

Thanks
LVL 1
LeTayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
I believe that this will get you what you need.

select sum(c2) from
(
select c1, max(c2) as c2
from t1
where c3 = 'MyData'
) as a

Open in new window

0
auke_tCommented:
create table #table
(
      C1 char(1),
         C2 integer,
      C3 varchar(200)
)


insert into #table values ('A', 5, 'MyData')
insert into #table values ('A', 15, 'MyData')
insert into #table values ('B', 100, 'MyData')
insert into #table values ('B', 10, 'MyData')

select
      sum(maxc2) as total
from
(
      select
            max(c2) as maxc2
      from
            #table
      group by
            c1
) subset

DROP TABLE #table
0
Bhavesh ShahLead AnalysistCommented:
hi,

just little modify to sjwales query


select sum(c2) from
(
select t1.c1, max(c2) as c2
from t1
where c3 = 'MyData'
GROUP BY t1.c1
) as a
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
>>The sum here show be the one for C1 = 'A' third line 15 which is the max with C1 = 'A' + the one for C2 = 'B' where the max value is 100<<
Did you mean the one where C1='B' and the max value is 100? If so, you can also use analytics.
select sum(x.c2) as sumc2 from
(select c2, row_number() over (partition by c1 order by c2 desc) rn
 from t1
 where c3 = 'MyData') as x
where x.rn = 1;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.