tags:

views:

137

answers:

4

i have a table like this :

Alternative |Total  |Male  |Female
a           |20     |10    |10
b           |50     |20    |30 
c           |40     |10    |30 
c           |30     |15    |15

now i want to select all the rows and the "c" alternative to be grouped.

+3  A: 
SELECT Alternative, Sum(Total), Sum(Male), Sum(Female) 
FROM table1 
GROUP BY Alternative
TheTXI
This will also sum the values that are not C, I'm not sure that's what he wants. See my answer below.
Roee Adler
I'm well aware of that Rax. It has been my experience from using SO that often times a question-asker doesn't always know how to correctly phrase their question. If it turns out that yours is what he wants, that's great.
TheTXI
Got it... Thanks
Roee Adler
A: 

This should suffice

SELECT Alternative, [Total] = SUM(Total), [Male] = SUM(Male), [Female] = SUM(Female)
FROM YourTable
GROUP BY Alternative
Lieven
Heh, 33 second difference.
TheTXI
lol, you are right.
Lieven
I had to read the question several times before I understood what the OP wanted.
Lieven
Only thing I didn't bother with were the column aliases on the sums and the order by clause that the other poster put below this.
TheTXI
A: 

Use group by to determine the grouping, then use the appropriate aggregate to process the other fields. I believe in this case that you want to use sum to add the values in each group:

select
   Alternative, sum(Total) as Total, sum(Male) as Male, sum(Female) as Female
from
   TheTable
group by
   Alternative
order by
   Alternative
Guffa
A: 

The following query will select all the rows that are not "c", then group all the rows that are "c", sum their value, and add a single row representing the sum of "c". I hope that's what you mean.

SELECT *
FROM table1 
WHERE Alternative != c

UNION

SELECT Alternative, Sum(Total) as Total, Sum(Male) as Male, Sum(Female) as Female
FROM table1 
WHERE Alternative = c
GROUP BY Alternative

If you just want to group anything that has multiple "Alternative" appearances, just use:

SELECT Alternative, Sum(Total) as Total, Sum(Male) as Male, Sum(Female) as Female
FROM table1 
GROUP BY Alternative
Roee Adler
Well...that's definitely taking a literal view of the question.
TheTXI
@TheTXI: I assumed this is not a "what's the syntax of group by / how do i group items" question, but rather a more advanced one. I
Roee Adler
Accepted answer and no up-votes? That's sad...
Roee Adler