简体   繁体   English

Oracle SQL:使用Xmlagg选择时出现错误,不是单组组函数

[英]Oracle SQL: error not a single-group group function in Select with Xmlagg

I have a table setup that returns multiple rows of column i.AIN for each column p.S_ID so I am trying to aggregate these to get just one row for each unique i.AIN record . 我有一个表设置, i.AIN为每一列p.S_ID返回i.AIN列的多行,因此我试图将它们汇总起来,以便为每个唯一的i.AIN记录仅获得一行

I cannot use Listagg as I am exceeding the character limit there. 我无法使用Listagg,因为我超出了那里的字符数限制。

With information from this website I created the below query but I get the following error: 利用该网站的信息,我创建了以下查询,但出现以下错误:

"ORA-00937: not a single-group group function"

Can someone tell me how I have to change my GROUP BY here to make this work or if there is anything else wrong with my query ? 有人可以告诉我如何在此处更改GROUP BY才能完成此工作,或者我的查询是否还有其他问题?

My query (shortened): 我的查询(缩短):

ALTER SESSION ENABLE PARALLEL QUERY;
SELECT
    p.S_ID AS ID
    , XMLELEMENT
    (
        "AINs", XMLAGG
        (
            XMLELEMENT
            (
                "AIN", i.AIN || 
                '-nl-Article: ' || SUBSTR(a.AIN_NAME, 1, 50) || '...' || 
                '-nl-Quantity: ' || SUM(i.UNITS) || 
                '-nl-Price: ' || TO_CHAR(i.PRICE, 'FM9,990.00') || ' + ' || TO_CHAR(i.PRICE_TAX, 'FM9,990.00') || ' USt = ' || TO_CHAR((i.PRICE + i.PRICE_TAX), 'FM9,990.00') || 
                '-nl------' || 
                '-nl-Subtotal: ' || TO_CHAR((i.PRICE * SUM(i.UNITS)), 'FM9,990.00') || ' + ' || TO_CHAR((i.PRICE_TAX * SUM(i.UNITS)), 'FM9,990.00') || ' USt = ' || TO_CHAR(((i.PRICE + i.PRICE_TAX) * SUM(i.UNITS)), 'FM9,990.00')
            )
        )
    )/*.EXTRACT('//text()')*/ AS Details
FROM 
    ITEMS i
LEFT JOIN 
    AINS a
    ON i.AIN = a.AIN
LEFT JOIN 
    PKGS p
    ON i.SHIPMENT = p.SHIPMENT
WHERE
    /*...*/
GROUP BY
    p.S_ID
    , i.AIN
    , a.AIN_NAME
    , i.UNITS
    , i.PRICE
    , i.PRICE_TAX
ORDER BY
    p.S_ID

Many thanks for any help with this - it is much appreciated, 非常感谢您对此提供的任何帮助-非常感谢,
Mike 麦克风

Without real data and tables, it is difficult to provide working an example. 没有真实的数据和表,很难提供一个可行的例子。 You have to split your aggregation in two level. 您必须将聚合分为两个级别。 First, create inline view and calculate subtotals, and in next level aggregate results into one string. 首先,创建内联视图并计算小计,然后在下一级将结果聚合为一个字符串。

  select xmlagg(....
            ) 
            from (
            select i.AIN as ain,  SUBSTR(a.AIN_NAME, 1, 50) as ain_name
                , SUM(i.UNITS) sum_units 
                , TO_CHAR(max(i.PRICE), 'FM9,990.00') || ' + ' || TO_CHAR(max(i.PRICE_TAX), 'FM9,990.00') || ' USt = ' || TO_CHAR((max(i.PRICE) + max(i.PRICE_TAX)), 'FM9,990.00') 
               ... etc
               from /*joins*/
               GROUP BY
              p.S_ID
            , i.AIN
            , a.AIN_NAME 

            ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM