DB2函数是否返回错误SQL CODE 4743?

ymdaylpp  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(317)

我已经尝试解决这个问题有一段时间了。我有一个名为Students的表,如下所示:

ID     |Classes    |Priority
----------------------------
3       A51         1
3       B51         2
3       K5B         2
3       M5A         2
4       XN5         1
5       XN5         1
5       A51         2
9       BX1         1
9       BX2         2
9       AK3         2

我现在正在使用DBVisualizer来执行我的语句,但我尝试将名为LISTAGG()的函数作为DB2函数:

SELECT
    ID,
    LISTAGG(classes, ',') within GROUP (ORDER BY Priority) AS GROUPED_CLASSES
FROM
    Students
GROUP BY
    ID;

但是,每次我尝试运行此程序时,都会收到以下错误:

1) [Code: -4743, SQL State: 56038]  ATTEMPT TO USE A FUNCTION WHEN THE APPLICATION COMPATIBILITY SETTING IS SET FOR A PREVIOUS LEVEL. SQLCODE=-4743, SQLSTATE=56038, DRIVER=4.22.29
2) [Code: -514, SQL State: 26501]  THE CURSOR SQL_CURLH200C1 IS NOT IN A PREPARED STATE. SQLCODE=-514, SQLSTATE=26501, DRIVER=4.22.29

我不知道如何修复它,也不知道为什么会这样。我试着研究这个问题,有人建议这样做:SET CURRENT APPLICATION COMPATIBILITY = 'V11R1',但也没有运气与此,同样的错误,即使它运行。
我还尝试查看我的DB2版本与SELECT GETVARIABLE('SYSIBM.VERSION') FROM SYSIBM.SYSDUMMY1,这将返回DSN12015(不知道这意味着什么)。
我在拼命寻找答案或帮助,非常感谢。目标是让结果看起来像这样:

ID    |Grouped_Classes     |
-----------------------------
3      A51, B51, K5B, M5A
4      XN5
5      XN5, A51
9      BX1, BX2, AK3
pb3s4cty

pb3s4cty1#

试试看:

/*
WITH STUDENTS (ID, Classes, Priority) AS 
(
          SELECT 3, 'A51', 1 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 3, 'M5A', 2 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 4, 'XN5', 1 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 5, 'XN5', 1 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 3, 'B51', 2 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 3, 'K5B', 2 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 5, 'A51', 2 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 9, 'BX1', 1 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 9, 'BX2', 2 FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 9, 'AK3', 2 FROM SYSIBM.SYSDUMMY1
)

* /

SELECT 
  ID
, SUBSTR (XMLSERIALIZE (XMLAGG (XMLTEXT (',' || CLASSES) ORDER BY PRIORITY) AS CLOB (100)), 2)
  AS GROUPED_CLASSES
FROM STUDENTS
GROUP BY ID

| 识别码|分组类|
| - -|- -|
| 三个|A51、K5B、M5A、B51|
| 四个|XN5|
| 五个|XN5、A51|
| 九个|BX1、BX2、AK3|

相关问题