Pivot/Unpivot table 在 ABAP CDS 中

Pivot/Unpivot table in ABAP CDS

是否可以在ABAP CDS 查询中进行数据透视查询?有没有办法像我们在 SQL 服务器和 MySQL 中一直做的那样实现这一目标?

不,目前它在 ABAP CDS 和 HANA 中都不受本地支持。

您可以使用多种解决方法来模拟枢轴行为:

  1. COUNTUNION ALL 一起旋转 [sample]

    select vendor_id, sum("E1") AS "E1", SUM("E2") AS "E2", SUM("E3") AS "E3"
    FROM
    (
    select vendor_id, COUNT(NUM) as "E1", 0 AS "E2" , 0 AS "E3"
    from "ABHISHEAGRAW".
    WHERE EMP_ID = 'E1' GROUP BY vendor_id
    union all
    select vendor_id, 0 AS “E1”, COUNT(NUM) as "E2", 0 AS "E3"
    from "ABHISHEAGRAW".
    WHERE EMP_ID = 'E2' GROUP BY vendor_id
    union all
    select vendor_id, 0 AS “E1”, 0 as "E2", COUNT(NUM) AS "E3"
    from "ABHISHEAGRAW".
    WHERE EMP_ID = 'E3' GROUP BY vendor_id
    ) GROUP BY vendor_id ORDER BY vendor_id
    
  2. COUNTCASE [sample]

    select vendor_id, sum("E1") AS "E1", SUM("E2") AS "E2", SUM("E3") AS "E3"
    FROM
    (
    select vendor_id, CASE (EMP_ID) WHEN 'E1'
    THEN 1
    ELSE 0
    END as "E1",
    CASE (EMP_ID) WHEN 'E2'
    THEN 1
    ELSE 0
    END as "E2",
    CASE (EMP_ID) WHEN 'E3'
    THEN 1
    ELSE 0
    END as "E3"
    from “ABHISHEAGRAW”.
    )GROUP BY vendor_id ORDER BY vendor_id
    
  3. 使用 CASE 进行透视和分组 [sample]

给定的片段是 HANA 示例,但它们可以适用于 ABAP CDS。如果您提供 MCVE 您的案例,我们可以向您推荐一个可能的解决方案。