To start: I am querying a DB2 server built into the IBM i System package from Excel 2007 using "From Microsoft Query" and straight-up SQL. It is a financial database.
The DB is structured such that account #s are split up into 8 integer fields for fund, division, department, activity 1, activity 2, element 1, element 2, and object. In order to make query results readable I have concatenated them with the following code:
select
right(trim(trailing from (concat('00',cast(T1."GMFUND" as Char(8))))),3)||'-'
||right(trim(trailing from (concat('00',cast(t1."GMDPT" as Char(8))))),2)
||right(trim(trailing from (concat('00',cast(T1."GMDIV" as Char(8))))),2) ||'-'
||right(trim(trailing from (concat('00',cast(T1."GMSTAB" as Char(8))))),2)
||right(trim(trailing from (concat('00',cast(T1."GMSTAS" as Char(8))))),1)||'.'
||right(trim(trailing from (concat('00',cast(T1."GMELM1" as Char(8))))),1)
||right(trim(trailing from (concat('00',cast(T1."GMELM2" as Char(8))))),1)||'-'
||right(trim(trailing from (concat('00',cast(T1."GMOBJ" as Char(8))))),2)
as AJAccount
It's a bit difficult to read, so I'll explain: For each field I am concatenating '00' to each # typecast as an 8-char string (to add leading zeroes), then trimming the trailing blank spaces (from the typecast conversion), and finally grabbing the correct # of digits for each using right(). That ends up making up a single acct # which we use day-to-day in the following format:
000-0000-000.00-00
Now here comes the more complicated part. I need to sum a calculated field for debits and credits, defined by the following:
IFNULL(T1."GMDAMT",0)-IFNULL(T1."GMCAMT",0) as Trans_Total
But that isn't summed yet. And that's where I run into my problem. When I query those two fields I get accurate results for each and every transaction in the DB, labeled w/ acct #s. But when I attempt to enclose the [debits - credits] field in a SUM() aggregate function and group by 'AJAccount' my query throws an error.
The error points to the following section of my concatenated acct # field:
right(trim (trailing from (concat('00',cast(T1."GMOBJ" as Char(8))))),2)
Specifically it highlights "GMOBJ" as the source of the error. But without the SUM() function, the code runs just fine, producing results that look like the following:
001-0000-243.00-00 | 166898.00
001-0000-244.00-00 | -166898.00
161-0000-243.00-00 | 3000.00
161-0000-244.00-00 | -3000.00
470-0000-243.00-00 | 4999.00
470-0000-244.00-00 | -4999.00
490-0000-243.00-00 | 1000.00
490-0000-244.00-00 | -1000.00
This isn't even the half of what I really need to do (because the transaction data is split off into 4 different tables by transaction type that I will eventually need to JOIN on...but that's another task). But until I get this working I am at a dead stop.
This is the whole block of code:
select
right(trim(trailing from (concat('00',cast(T1."GMFUND" as Char(8))))),3)||'-'
||right(trim(trailing from (concat('00',cast(t1."GMDPT" as Char(8))))),2)
||right(trim(trailing from (concat('00',cast(T1."GMDIV" as Char(8))))),2) ||'-'
||right(trim(trailing from (concat('00',cast(T1."GMSTAB" as Char(8))))),2)
||right(trim(trailing from (concat('00',cast(T1."GMSTAS" as Char(8))))),1)||'.'
||right(trim(trailing from (concat('00',cast(T1."GMELM1" as Char(8))))),1)
||right(trim(trailing from (concat('00',cast(T1."GMELM2" as Char(8))))),1)||'-'
||right(trim(trailing from (concat('00',cast(T1."GMOBJ" as Char(8))))),2)
as AJAccount,
SUM(IFNULL(T1."GMDAMT",0)-IFNULL(T1."GMCAMT",0)) as Trans_Total
from "HTEDTA"."GM310AP" T1
where T1."GMAPYR" = 2014
group by 'AJAccount'
I thought maybe I typo'd or something, so I did the following easier query:
select
T1."GMFUND",
SUM(IFNULL(T1."GMDAMT",0)-IFNULL(T1."GMCAMT",0)) as Trans_Total
from "HTEDTA"."GM310AP" T1
where T1."GMAPYR" = 2014 and T1."GMAPMO" between 1 and 4
group by T1."GMFUND"
order by T1."GMFUND"
and it ran just fine, producing the following results:
1 | 20090901.49
111 | 32635.15
114 | 0.00
115 | 0.00
131 | 5916.66
So I suspect I am lacking knowledge about calculated fields and SUM(). Can someone enlighten me?