0

My question is similar to this but the answer is not what I want:

Cannot use group by and over(partition by) in the same query?

My table is like this:

CODE_1  CODE_2  AMOUNT
A       A.1     2
A               4
A               6
B               1
B       B.1     3
B       B.1     5

I want to get the total of Code_1 and [Code_1, Code_2] like this:

CODE_1  CODE_2  Total_1 Total_2
A       A1      12      2
B       B1      9       8

Can I get the result in one SELECT? Thanks!

Community
  • 1
  • 1
abacusit
  • 53
  • 5

1 Answers1

3

Is this what you're looking for? It's hard to know how you intend to treat the CODE_2 data:

select
    code_1, 
    max(CODE_2) as CODE_2,
    sum(AMOUNT) as Total_1,
    sum(case when CODE_2 is not null then AMOUNT end) as Total_2
from T
    group by CODE_1
Hogan
  • 69,564
  • 10
  • 76
  • 117
shawnt00
  • 16,443
  • 3
  • 17
  • 22