samra
samra

Reputation: 180

How to combine multiple SUM in one Query?

I have query like this:

SELECT *, SUM(ss.rank) as group_sum 
FROM Standard s, s.SubStandard ss 
GROUP BY s.id

The problem is I want to have SUM(s.rank) as total_rank in this query, how is this possible?

Upvotes: 2

Views: 2935

Answers (3)

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

Not sure what you're looking for but this might help:

select s.id, s.title, sum(ss.rank) as group_sum from standard s
join subStandard ss
on s.id = ss.standard_id
group by s.id, s.title

Upvotes: 1

user319198
user319198

Reputation:

Just Try:

SELECT *, SUM(ss.rank) as group_sum ,SUM(s.rank) as total_rank
FROM Standard s, s.SubStandard ss on s.id=ss.sid  // => replace with actual column
GROUPBY s.id

if different group by then please use union.

Upvotes: 1

SenorAmor
SenorAmor

Reputation: 3345

List your individual columns (instead of *).

SELECT SUM(s.rank) as total_rank, s.id, s.foo, s.bar, SUM(ss.rank) as group_sum FROM standard s...

I would suggest altering your JOIN, too.

... LEFT JOIN substandard ss ON s.id = ss.standard

or something similar.

Upvotes: 1

Related Questions