DB2 count(*) over(partition by fieldname) giving -

2019-08-17 08:37发布

问题:

I have slimmed down the query to remove potential complications, in addition I have verified that the fields are correct. DB2 UDB zSeries V7 is my db2 version.

SELECT 
    STDINSTRCD, 
    COUNT(*) OVER(PARTITION BY STDINSTRCD),
    CAST(STDINSTRDESC AS VARCHAR(1000)) AS INSTR,
    C.STDINSTRSEQ,
    1
FROM 
    SYST.SCC004 C
WHERE  
    C.STDINSTRCD = '098'

I have tried a subquery as well.

select 
 H2.FRSTSTDINSTRCD,
 (select count(*) from SYST.scC004 Ci where '098'=Ci.STDINSTRCD) as cnt, 
 cast(STDINSTRDESC as varchar(1000)),
 C.STDINSTRSEQ,
 1
from SYST.scE4A00 H2
 LEFT OUTER JOIN SYST.scC004 C
 ON C.STDINSTRCD = H2.FRSTSTDINSTRCD
 WHERE
  H2.CTLENTYID='MCS'
  AND H2.VCKVAL='12654'
  AND H2.POKVAL='0198617S12 000  000'

The error is receive is om.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: (;, FROM INTO sqlcode sqlstate -104 Illegal Symbol token. 42601 A character, token, or clause is invalid or missing.

Any advice? I have been unable to determine what syntax error I might me making.

回答1:

are there any weird special characters in there that might not be printing? http://www-01.ibm.com/support/docview.wss?uid=swg1IY43009 basically sounds like a weird cr/lf or special char? Any copy pasting from *nix to windows ?

Also, I'm not sure why you need partition by anyway? would a group by not accomplish your goal. (looks like your just counting number of rows that met your criteria)... something like this for your first query?

SELECT 
 STDINSTRCD, 
 count(1) ,
 CAST(STDINSTRDESC AS VARCHAR(1000)) AS INSTR,
 C.STDINSTRSEQ,
 1
FROM SYST.SCC004 C
WHERE  C.STDINSTRCD = '098'
group by 
STDINSTRCD, 
CAST(STDINSTRDESC AS VARCHAR(1000)) AS INSTR,
C.STDINSTRSEQ,
1


回答2:

Db2 Version 7 for z/OS does not support OLAP functions, or row_number(). You need to rewrite your query to avoid using such functions. They arrived in later Db2 versions. See also other people's tips on alternatives via this link.