Returns the sum of elements depending on the type

I have one field that I need to summarize, we can say that the named elements However, this field can be part of group a or b. In the end, I need to have all the elements summed for group a and group b

when I say grouped, I mean that there is a LEFT OUTER JOIN in another table, in the previous table there is a type for the elements, and the one that is connected has a group assigned to this element type

Sorry guys I'm a little new to sql. I am going to try what you gave me to get back to you.

Okay, I feel that we are getting closer, not everything yet, I can get them to disperse, but the problem is that I need both groups to sum in the same line, which is difficult because I also have several LEFTs OUTER JOIN

Tyler looks like it might work, so I'm trying to make a hash, which is very fast

Alain seems to be coming, but I need to make friends with him a little.

+3
source share
4 answers

Perhaps I do not understand the complexity of what you are asking, but ... shouldn't it be?

SELECT groupname, SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
GROUP BY groupname

And if you don't care which of the elements or b belongs to this element, this will do:

SELECT SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
+4
source

Do you want something like

SELECT column,SUM( column ) FROM table GROUP BY column
0
source

( ), , :

SELECT sum(item), groupingField FROM someTable GROUP BY groupingField

:

SELECT count(*), item FROM someTable GROUP BY item

:

sum(item)    |     groupingField
-------------+-----------------------
 71          |          A
 82          |          B

, , , , ?

0

:

SELECT B.[Group], COUNT(*) AS GroupCount
FROM Table1 A
LEFT JOIN Table2 B ON B.ItemType=A.ItemType
GROUP BY B.[Group]
0

All Articles