使用xmlagg查找要连接的计数

时间:2018-07-10 15:08:14

标签: sql xml oracle

SELECT DISTINCT
    ent.entity_key_id AS query1kid,
    CAST(substr(rtrim(XMLAGG(xmlelement(e,alstd.relationship_desc
                                                    ||
        CASE
            WHEN(alstd.joint_flag = 'No' AND ent.anonymous_flag = 'No') THEN ''
            ELSE('('
                   ||
                CASE
                    WHEN alstd.joint_flag = 'Yes' THEN 'Joint'
                    ELSE ''
                END
                   ||
                CASE
                    WHEN ent.anonymous_flag = 'Yes' THEN ',Anon'
                    ELSE ''
                END
                   || ')')
        END
                                                    || ': '
                                                    || allocthm.allocation_description
                                                    || '('
                                                    || substr(allocthm.allocation_code,5,6) || ***count(ben.entity_key_ID)***
                                                    || ')',',').extract('//text()')
        ORDER BY
            ent.entity_key_id
    ).getclobval(),','),1,4000) AS VARCHAR(4000) ) AS displayfiled1 
FROM
                    er_datamart.allocation_theme allocthm
            left JOIN er_datamart.allocation_stewardee alstd ON (allocthm.allocation_code = alstd.allocation_code and alstd.status_code <> 'F')
            INNER JOIN er_datamart.entity_d ent ON alstd.entity_key_id = ent.entity_key_id
            LEFT OUTER JOIN ER_DATAMART.ALLOCATION_BENEFICIARY ben ON ben.allocation_code  = allocthm.allocation_code 


GROUP BY 
    ent.entity_key_id

这给了我一个错误:

  

ORA-00937:不是单组分组功能

我正在尝试找到count(ben.entity_key_ID),以便可以将其附加到已经运行的查询中。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题似乎出在count()内的xmlagg()-您有嵌套的group函数,group-by子句无法处理。

您可以使用自己的分组依据将字符串连接移动到嵌入式视图中,以获取该计数,然后从该视图执行XML聚合;像这样:

SELECT
   entity_key_id AS query1kid,
   CAST(substr(rtrim(
      XMLAGG(xmlelement(e, constructed_string, ',').extract('//text()')
         ORDER BY entity_key_id
   ).getclobval(),','),1,4000) AS VARCHAR(4000) ) AS displayfiled1 
FROM (
   SELECT ent.entity_key_id,
      alstd.relationship_desc
            ||
         CASE
            WHEN(alstd.joint_flag = 'No' AND ent.anonymous_flag = 'No') THEN ''
            ELSE('('
            ||
         CASE
            WHEN alstd.joint_flag = 'Yes' THEN 'Joint'
            ELSE ''
         END
            ||
         CASE
            WHEN ent.anonymous_flag = 'Yes' THEN ',Anon'
            ELSE ''
        END
           || ')')
        END
           || ': '
           || allocthm.allocation_description
           || '('
           || substr(allocthm.allocation_code,5,6) || count(ben.entity_key_ID)
           || ')' as constructed_string
   FROM
       allocation_theme allocthm
   LEFT JOIN allocation_stewardee alstd
   ON allocthm.allocation_code = alstd.allocation_code and alstd.status_code <> 'F'
   INNER JOIN entity_d ent
   ON alstd.entity_key_id = ent.entity_key_id
   LEFT OUTER JOIN ALLOCATION_BENEFICIARY ben
   ON ben.allocation_code  = allocthm.allocation_code 
   GROUP BY
      ent.entity_key_id,
      alstd.relationship_desc,
      alstd.joint_flag,
      ent.anonymous_flag,
      allocthm.allocation_description,
      allocthm.allocation_code
)
GROUP BY 
   entity_key_id

ELSE ''部分是多余的,因为这是默认行为,但是您可能更希望保留它们以保持清楚/明确。您的关节/肛门部分可能需要更多的工作-如果关节为否,而肛门为是,您会得到(,Anon)-我认为-这看起来有些奇怪,但可能正是您所需要的。