Multiple sum's of the same column based on another column in the one query

here is the layout in a basic form:

name   code   amount


I need to write a single select statement that will add all the amounts of each distinct code and also have it grouped by name.

For example, if one Name had 2 distinct codes but in 10 entries, I would want my results to be 2 rows, with the sum of each codes amounts.




 
LVL 6
RustyZ32Asked:
Who is Participating?
 
raulggonzalezConnect With a Mentor Commented:
CODES TO SUMMARIZE can be a table like @deighton said or parameters

(@param1, @param2 ...)

SELECT name,code,sum(amount)
FROM yourTable
WHERE code IN (CODES TO SUMMARIZE)
GROUP BY code, name
UNION

SELECT name,code,amount
FROM yourTable
WHERE code NOT IN (CODES TO SUMMARIZE)  -- Better like this.....


Cheers
0
 
raulggonzalezCommented:
Hi,

Try this...

SELECT code, name, sum(amount)
FROM yourTable
GROUP BY code, name


Cheers
0
 
Muhammad Ahmad ImranDatabase DeveloperCommented:
select name, sum(amount) from table group by code,name
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RustyZ32Author Commented:
I'm sorry. I  was not clear in my question.

The main challenge is that several of the values in the code column need to stay unsummarized.

i need a way to manually specify which values are to be summed and which are to be left alone.


0
 
Muhammad Ahmad ImranDatabase DeveloperCommented:
can you post some sample data and required results
0
 
raulggonzalezCommented:
Is it going to be more than one at a time?

If not, you can use a parameter

SELECT code, name,
sum(
CASE WHEN @parameter = 1 then amount
when @parameter = 2 then anotherField
END
)
FROM yourTable
GROUP BY code, name


If not, give more info...


Cheers
0
 
dmlyo150Commented:

Is there any limitations to your code that you could not use an ORDER clause? See the following:

SELECT Name, Code, sum(Amount) FROM table GROUP BY Name, Code
ORDER BY Name, Code
0
 
raulggonzalezCommented:
Hi,

There's no incompatibility between GROUP BY and ORDER BY, so you can use them both.

'Group by' sort the results somehow, but if you need any specific order, you can use ORDER BY.


Cheers.
0
 
RustyZ32Author Commented:
yes, it will be more than one at a time. I might need 5 of the codes summed and 3 not summed.

can I do some kind of series of nested statements with individual WHERE clauses for the different codes?
0
 
deightonprogCommented:
select name,code,sum(amount)
FROM yourTable
WHERE code IN (SELECT TableHoldingCodesToSum.CODE FROM TableHoldingCodesToSum)
GROUP BY code, name
0
 
raulggonzalezCommented:
HI,

can you use dynamic SQL? you can build the query according a parameter (or some) ...

anyway, can you post the field and an example of what is your expected results?


cheers.
0
 
raulggonzalezCommented:
Sorry, I think I got it...

you say that some codes need to be summarize and some not?

SELECT name,code,sum(amount)
FROM yourTable
WHERE code IN (CODES TO SUMMARIZE)
GROUP BY code, name
UNION

SELECT name,code,amount
FROM yourTable
WHERE code IN (CODES NOT  TO SUMMARIZE)



Cheers.
0
 
dmlyo150Commented:
add the Order By clause after the Union Select.
SELECT name,code,sum(amount)
FROM yourTable
WHERE code IN (CODES TO SUMMARIZE)
GROUP BY code, name
UNION

SELECT name,code,amount
FROM yourTable
WHERE code NOT IN (CODES TO SUMMARIZE)
ORDER BY name, code
0
 
raulggonzalezCommented:
Hi @dmlyo150,

you win... but @RustyZ32 didn't mention anything about ordering....


Cheers.

0
 
RustyZ32Author Commented:
dmly0150

that worked!

however, I also have several other columns I would like to include in the select statement but when i put them in the GROUP BY clause it messes up the sums. (i get an errror if i dont put them in group by)

I know this isn't part of the original question and will accept your solution regardless.


thanks!
0
 
dmlyo150Commented:
thank you -but I can stil help.
There is a PARTITION feature as of SQL Server 2005.
can you provide me (all) the field names and an idea of how you want it to apear with the addtional fields?
0
 
RustyZ32Author Commented:
dmlyo150, I figured it out. In the first select statement i left out any columns that I knew had unique data. the ones that didn't (i.e. an employees office) i was able to include without messing up the grouping.

i then specified the rest in the second statement after the union.


all worked well, thanks for your help.
0
 
dmlyo150Commented:
Glad I can help -and thank you -every point helps
0
 
raulggonzalezCommented:
Hi,

I have to add that the solution posted by @dmlyo150 is a copy/paste of my solution, so the accepted answer should be mine.

I want to open this question to be reviewed by a moderator.


Cheers
0
 
deightonprogCommented:
raulg - I'm with you on that - it looks like a copy of your post with little further amplification

I respect the fact that you mentioned that your explanation added to my previous answer BTW, that's the way it should be, it was hard to see what was needed here.
0
 
raulggonzalezCommented:
Hi @deighton

Thanks for your support. And apart, if you read the whole thread, he was asking me whether it was possible to use GROUP BY and ORDER BY together...

Cheers.
0
 
dmlyo150Commented:

Administrative Moderator,
I accept your position, and thank you for imparting the Experts Exchange etiquette. I urge the moderator to review the initial post of this thread. I answered the Question! Correctly! Later the question was expanded requiring use of a UNION; this provided RustyZ32 with all the data (he) was looking for –which was NOT in the original question –but it still wasn’t summarized as he indicated it needed to be in his original question. Once again –I indicated using the Order by Clause –which he must have felt was pretty crucial because he awarded me the points.
 
---
raulggonzalez:
Hi @dmlyo150,

you win... but @RustyZ32 didn't mention anything about ordering....

Cheers.
---
RustyZ32 did say he wanted it order when he wrote… “a single select statement that will add all the amounts of each distinct code and also have it grouped by name”
If RustyZ32 did not put an Order By clause on this –it would have been grouped –first by code then by name. My Order By clause returned it grouped by name then code –as his original question asked.
_alias99 wrote that the best practice would have been to post - (for example, with a comment to the effect of "adding 'xyz' to the code posted by 'ExpertA' would be beneficial because ...").
My post read exactly that –my copy/paste was proceeded by -- add the Order By clause after the Union Select.
In all fairness I think RustyZ32 should at least have the opportunity to distribute the points to more than one contributor.
0
 
raulggonzalezCommented:
Hi @dmlyo150,

When I said 'you win' was an irony.

There's nothing in @RustyZ32 original or 2nd question about ordering, that's why I gave up, because you where saying the same like I did + ORDER BY  TWO TIMES, which as long as I read, wasn't necessary.

And your ORDER BY is indeed mine as well, because you were asking me whether you can use both, and I said yes.

I don't want to get deeper and the moderator explained what should be pretty well. I'm not in this website since long, but I got how it works.

Best regards.

 
0
 
dmlyo150Commented:
"And your ORDER BY is indeed mine as well, because you were asking me whether you can use both, and I said yes."

I don't know why you would think I was asking -another contributor -questions about code. I was asking RustyZ32 if there was a limitation to this code that he could NOT use an ORDER BY clause.
for example –It is possible an author may not give you the entire scenario –for instance if he was going to be using this Single Select as a subquery in his final solution –he would be unable to use an Order By clause. I am not naive enough to think that this one and only Select statement is going to complete his entire project.

Sincere Wishes
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.