1

I am in need of some assistance as I am pretty new to SQL. I have been reading through multiple posts on SO like Using group by on multiple columns and im just not able to figure out how to accomplish what I need to do. Right now i have 4 columns from the same table that I need to group by just one of those columns and have the sum display at the end of each of the groups. Currently, I have this as my string:

SELECT
    uic5a5 as "Processor",
    acctno as "Account Number",
    orgamt AS "Loan Amount",
    date(digits(decimal(orgdt7 + 0.090000, 7, 0))) as "Origination_Date"
    FROM cncttp08.jhadat842.lnmast lnmast
    where uic5a5 is not null
    order by uic5a5 desc

Which gives me the expected Result of:

Processor | Account Number | Loan Amount | Origination Date
----------------------------------------------------------
ZJE           XXXXXXX         85000.00         2018-05-15
ZJE           XXXXXXX         150000.00        2018-12-05
CJK           XXXXXXX         75000.00         2018-06-29

What I am trying to do is get the result like this:

Processor | Account Number | Loan Amount | Origination Date | Total
-------------------------------------------------------------------
ZJE           XXXXXXX         85000.00         2018-05-15
              XXXXXXX         150000.00        2018-12-05    
                                                              235000.00
CJK           XXXXXXX         75000.00         2018-06-29
                                                              75000.00

I am hoping someone can point me in the right direction to accomplish this task.

Zack E
  • 696
  • 7
  • 23

1 Answers1

1

DB2 should support grouping sets. If so, you can do:

select uic5a5 as "Processor", acctno as "Account Number",
       sum(orgamt) AS "Loan Amount",
       date(digits(decimal(orgdt7 + 0.090000, 7, 0))) as "Origination_Date"
from cncttp08.jhadat842.lnmast lnmast
where uic5a5 is not null
group by grouping sets ( (uic5a5, acctno, date(digits(decimal(orgdt7 + 0.090000, 7, 0)))), (uic5a5) );
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786