0

I'm kinda new to this and I have been stuck on this for a while now.

Example:

Col1 Col2 Col3
 A  | H  | 1
 A  | I  | 2
 A  | J  | 3
 B  | J  | 4 
 B  | K  | 5
 C  | L  | 6

How can I sum 'Col3' but only for particular values. For example sum up the values in 'Col3' where the letters in 'Col1' are in the same row as 'Col3'. So A = 6 (1+2+3) and B = 9 (4+5) and C = 6

So you get this:

Col1 Col2 Col3
 A  | H  | 6   
 A  | I  | 6
 A  | J  | 6
 B  | J  | 9 
 B  | K  | 9
 C  | L  | 6

This is what I had so far:

SELECT Col1, Col2, SUM(Col3)
FROM Table1
GROUP BY Col1, Col2;

Thanks

2

3 Answers 3

4

Just to elaborate on my comment.

You can use the window function sum() over()

Example

Declare @YourTable Table ([Col1] varchar(50),[Col2] varchar(50),[Col3] int)  Insert Into @YourTable Values 
 ('A','H',1)
,('A','I',2)
,('A','J',3)
,('B','J',4)
,('B','K',5)
,('C','L',6)

Select Col1
      ,Col2
      ,Col3 = sum(Col3) over (partition by Col1)
 From @YourTable

Returns

Col1    Col2    Col3
A       H       6
A       I       6
A       J       6
B       J       9
B       K       9
C       L       6
Sign up to request clarification or add additional context in comments.

Comments

1

Just as another way you can do this way also using join and SUM (Transact-SQL) function.

create table TestTable (Col1 varchar(5)
     , Col2 varchar(5)
     , Col3 int)

insert into TestTable Values
 ('A', 'H', 1),
 ('A', 'I', 2),
 ('A', 'J', 3),
 ('B', 'J', 4),
 ('B', 'K', 5),
 ('C', 'L', 6)

SELECT tblA.Col1
    ,tblA.Col2
    ,tblB.Col3
FROM (
    SELECT Col1
        ,Col2
    FROM TestTable
    ) tblA
INNER JOIN (
    SELECT Col1
        ,sum(Col3) AS Col3
    FROM TestTable
    GROUP BY Col1
    ) tblB ON tblA.Col1 = tblB.Col1

Live Demo

Comments

0

There are a number of ways to write data aggregation queries like this. Which to use depends on what your final results need to look like. Just to go over some basics, I’ll go over several methods here.

The simplest is to use a WHERE clause:

SELECT Col1, sum(Col3)
 from MyTable
 where Col1 = 'A'

This will produce a single row of data:

Col1 Col3
 A  | 6   

To produce sums for all of the distinct values in ColA, you would use GROUP BY:

SELECT Col1, sum(Col3)
 from MyTable
 group by Col1

This will produce three rows of data:

Col1 Col3
 A  | 6   
 B  | 9
 C  | 6

The above samples are pretty straightforward and basic SQL examples. It is actually a bit difficult to produce the result set from your example, where you include Col2 and show the summation, because Col2 is not part of the data aggregation. Several ways to do this:

Using a subquery:

SELECT
   mt.Col1
  ,mt.Col2
  ,sub.SumCol3  Col3
 from MyTable  mt
  inner join (select
                 Col1
                ,sum(Col3)  SumCol3
               from MyTable
               group by Col1)  sub
   on sub.Col1 = mt.Col1

Using a common table expression:

WITH cteSub
 as (select
        Col1
       ,sum(Col3)  SumCol3
      from MyTable
      group by Col1)
select
   mt.Col1
  ,mt.Col2
  ,cteSub.SumCol3  Col3
 from MyTable  mt
  inner join cteSub
   on ctesub.Col1 = mt.Col1

And, perhaps the most obscure and obtuse, using aggregation fucntions with partitioning:

SELECT
   Col1
  ,Col2
  ,sum(Col3) over (partition by Col1)  Col3
from MyTable

Thorough and complete discussions of all the above tactics (better than anything I'd write) can be found online, by searching for "SQL" plus the appropriate term (aggregation, subquery, CTE, paritioning functions). Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.