0
votes

I am trying to execute the following db2 query, but I'm getting this error:

SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-119, SQLSTATE=42803, SQLERRMC=ENTITLEMENT

The query is:

SELECT *
FROM reclaimbalance rb
    ,user_benefit_accrued_period ubap
WHERE rb.user_id = ubap.user_id
    AND rb.component_id = ubap.PAY_HEAD_ID
    AND ubap.CUSTOMER_ID = 281
    AND rb.year = '2016-2017'
    AND ubap.STATUS = 1
GROUP BY ubap.user_id
    ,ubap.PAY_HEAD_ID
HAVING sum(ubap.STD_BALANCE_ADDED_IN_PERIOD) != rb.ENTITLEMENT
2
You should post your table schema, sample data and expected results. I suspect the issue is around the group by and having clauses but it's difficult to understand what your trying to do. - sgeddes
hey @sgeddes .... thanks for reply. - user1359
I wanna fetch the records from two table on the basis of one table record's sum is not equal to other - user1359
You cannot use SELECT * with GROUP BY; the select list must contain only the columns from the GROUP BY clause and aggregate functions. - mustaccio

2 Answers

0
votes

One problem is SELECT *. I would expect the error to be a bit different from just a generic syntax error, though.

Also, you should learn to use explicit JOIN syntax. And, your HAVING clause has an unaggregated column.

I assume you want something like this:

SELECT ubap.user_id, ubap.PAY_HEAD_ID, rb.ENTITLEMENT,
       sum(ubap.STD_BALANCE_ADDED_IN_PERIOD)
FROM reclaimbalance rb JOIN
     user_benefit_accrued_period ubap
      ON rb.user_id = ubap.user_id AND rb.component_id = ubap.PAY_HEAD_ID
WHERE ubap.CUSTOMER_ID = 281 AND rb.year = '2016-2017' AND ubap.STATUS = 1
GROUP BY ubap.user_id, ubap.PAY_HEAD_ID, rb.ENTITLEMENT
HAVING sum(ubap.STD_BALANCE_ADDED_IN_PERIOD) <> rb.ENTITLEMENT;
0
votes

The problem diagnosed by the sqlcode=-119 is that the column "ENTITLEMENT" specified in the HAVING clause is neither coded on that HAVING clause within an aggregate function nor is the column "ENTITLEMENT" specified in the GROUP BY clause.
Recovery is by either removing the non-aggregate reference to the column "ENTITLEMENT" from the HAVING clause, changing the reference to the column "ENTITLEMENT" to be inside of an aggregate function, or adding the column "ENTITLEMENT" to the GROUP BY clause.

Noting however, that despite being valid recovery, the effect may not be what is required. And even after that sqlcode=-119 problem is resolved according to any one of the possible recovery actions noted above, almost surely the next problem will be for a sqlcode-=-122 suggesting that some columns or non-aggregate\non-[effective-]constant expressions included in the SELECT-list are not also named on the GROUP BY clause. FWiW: Despite allusion(s) otherwise, the SELECT * can be compatible with GROUP BY, but just a special-case; as the rules state, the selected column [implicitly selected per the asterisk] must have been specified also, on the GROUP BY.