在单独的列中显示聚合值细分?

问题描述 投票:0回答:1

我想问一下,当在 SQL 中使用 sum agg 时,如何查看聚合过程中聚合了哪些值。

EG

产品
A 5
A 10

考虑到这一点,我想聚合 Quan 并细分所使用的值。 结果应该如下所示:

产品 Agg_Quan 详情
A 15 5, 10

目前我有这个 abap 类执行一些选择查询。 我想找到一种方法,要么直接执行 cds 视图,要么使用 abap 类。

CLASS zcl_flights_demo_cds IMPLEMENTATION.
METHOD flights_connections
    BY DATABASE FUNCTION
    FOR HDB
    LANGUAGE SQLSCRIPT
    OPTIONS READ-ONLY
    USING ZWODPROBLEM.

        itab_cities =
            SELECT
                ZWODPROBLEM.mandt as client,
                ZWODPROBLEM.product as airline_code,
                CAST(ZWODPROBLEM.phyquan AS INT) as total_phyquan,
                ZWODPROBLEM.Origin2 as city_to,
                SUM(ZWODPROBLEM.phyquan) OVER (PARTITION BY ZWODPROBLEM.product) as total_phyquan_agg
            FROM ZWODPROBLEM;

       RETURN
            SELECT client,
                    airline_code,
                    total_phyquan_agg as total_phyquan,
                    STRING_AGG(CONCAT(city_to, CONCAT('-', total_phyquan)), ', ') as cities_to
             FROM :itab_cities
             GROUP BY client, airline_code, total_phyquan_agg, city_to, total_phyquan;
ENDMETHOD.

末班。

谢谢!

sql aggregate abap cds string-agg
1个回答
1
投票

啊我解决了。

> METHOD flights_connections
>         BY DATABASE FUNCTION
>         FOR HDB
>         LANGUAGE SQLSCRIPT
>         OPTIONS READ-ONLY
>         USING ZWODPROBLEM.
> 
>             itab_coo =
>                 SELECT
>                     ZWODPROBLEM.mandt as client,
>                     ZWODPROBLEM.docNr as docNr,
>                     ZWODPROBLEM.Country_of_Origin as restriction,
>                     cast(ZWODPROBLEM.quantity as INT) as ODQuantity,
>                     ZWODPROBLEM.product as airline_code,
>                     sum(ZWODPROBLEM.phyquan) as total_phyquan,
>                     STRING_AGG(concat(ZWODPROBLEM.coo_item, concat('-',cast(ZWODPROBLEM.phyquan as INT))), ', ') as city_to
>                 FROM ZWODPROBLEM
>                 GROUP BY ZWODPROBLEM.mandt,ZWODPROBLEM.docNr,ZWODPROBLEM.Country_of_Origin,ZWODPROBLEM.quantity,ZWODPROBLEM.product;
>            RETURN
>                 SELECT client,
>                         docNr,
>                         restriction,
>                         ODQuantity,
>                         airline_code,
>                         total_phyquan as total_phyquan,
>                         city_to as cities_to
>                  FROM :itab_coo
>                  GROUP BY client,airline_code,restriction, ODQuantity, docNr,total_phyquan,city_to;
>     ENDMETHOD.

现在我在一列中得到按原点分隔的聚合值:

总数量 ---- 合计

200 ---- -50、TR-2、TR-2、TR-3、TR-3、TR-85、IT-10、AT-10、-23、-2、-10

© www.soinside.com 2019 - 2024. All rights reserved.