Cross apply openjson / pivot – multiple entries 跨应用 openjson / 旋转 – 多个条目

huangapple go评论59阅读模式
英文:

Cross apply openjson / pivot - multiple entries

问题

以下是您要翻译的代码部分:

WITH request
as
(
    SELECT requestId,
           property1191,
           '['+replace(replace(property1191, '[', ''), ']', '')+']' as json
    from capex_management_requests
)
SELECT *
FROM
(
    SELECT
      P.requestId,
      AttsData.[Id],
      AttsData.[data],
      ROW_NUMBER() OVER (PARTITION BY P.requestId, AttsData.[Id]
                     ORDER BY CAST(arr.[key] AS int)) AS row_id
    FROM request P
    CROSS APPLY OPENJSON (P.json) AS arr
    CROSS APPLY OPENJSON (arr.value)
      WITH 
      (
        Id VARCHAR(200) N'$.metaId',
        data VARCHAR(200)
      ) AS AttsData
) DS
PIVOT 
(
     MAX(data) FOR Id IN ([690], [1192])
 ) piv;

请注意,这是您提供的代码的翻译版本。

英文:

The following code, written by Charlieface in this answer, converts and opens a json content and pivots it in the end. It could be the case that multpiple entries can occur. How does the code need to be adapted to cover that as well?

>
> WITH request
> as
> (
> SELECT requestId,
> property1191,
> '['+replace(replace(property1191, '[', ''), ']', '')+']' as json
> from capex_management_requests
> )
> SELECT *
> FROM
> (
> SELECT
> P.requestId,
> AttsData.[Id],
> AttsData.[data],
> ROW_NUMBER() OVER (PARTITION BY P.requestId, AttsData.[Id]
> ORDER BY CAST(arr.[key] AS int)) AS row_id
> FROM request P
> CROSS APPLY OPENJSON (P.json) AS arr
> CROSS APPLY OPENJSON (arr.value)
> WITH
> (
> Id VARCHAR(200) N'$.metaId',
> data VARCHAR(200)
> ) AS AttsData
> ) DS
> PIVOT
> (
> MAX(data) FOR Id IN ([690], [1192])
> ) piv;
>

Current outcome - only "MAX" value for each ID [690] value

requestId row_id 690 1192
1 x 1 4352
1 x 2 3887
1 x 3 4372
1 x 4 3749
1 x 51 3693
1 x 89 4228

Target - multiple entries instead of "MAX" for each ID [690] value

requestId row_id 690 1192
1 x 1 4100
1 x 1 4352
1 x 2 3887
1 x 3 4200
1 x 3 4300
1 x 3 4372
1 x 4 3749
1 x 51 3693
1 x 51 3712
1 x 89 4228

I want to be able to also cover multiple entries correctly!

答案1

得分: 1

以下是代码的部分翻译:


WITH request
as
(
    SELECT requestId,
           property1191,
           '['+replace(replace(property1191, '[', ''), ']', '')+']' as json
    from capex_management_requests
), 
ds AS (
    SELECT
      P.requestId,
      AttsData.[Id],
      AttsData.[data],
      ROW_NUMBER() OVER (PARTITION BY P.requestId, AttsData.[Id]
                     ORDER BY CAST(arr.[key] AS int)) AS row_id
    FROM request P
    CROSS APPLY OPENJSON (P.json) AS arr
    CROSS APPLY OPENJSON (arr.value)
      WITH 
      (
        Id VARCHAR(200) N'$.metaId',
        data VARCHAR(200)
      ) AS AttsData
)
SELECT 
    requestId, 
    data,
    row_id,
    CASE WHEN Id = '690' THEN data ELSE NULL END AS [690],
    CASE WHEN Id = '1192' THEN data ELSE NULL END AS [1192]
FROM ds

希望这对你有所帮助。

英文:

If I understand the question, I believe you would avoid the PIVOT to avoid the MAX aggregation. You can use CASE statements to pick out the values that the pivot would have. I put the subquery as another CTE to perhaps make it clearer what's going on.


WITH request
as
(
    SELECT requestId,
           property1191,
           '['+replace(replace(property1191, '[', ''), ']', '')+']' as json
    from capex_management_requests
), 
ds AS (
    SELECT
      P.requestId,
      AttsData.[Id],
      AttsData.[data],
      ROW_NUMBER() OVER (PARTITION BY P.requestId, AttsData.[Id]
                     ORDER BY CAST(arr.[key] AS int)) AS row_id
    FROM request P
    CROSS APPLY OPENJSON (P.json) AS arr
    CROSS APPLY OPENJSON (arr.value)
      WITH 
      (
        Id VARCHAR(200) N'$.metaId',
        data VARCHAR(200)
      ) AS AttsData
)
SELECT 
    requestId, 
    data,
    row_id,
    CASE WHEN Id = '690' THEN data ELSE NULL END AS [690],
    CASE WHEN Id = '1192' THEN data ELSE NULL END AS [1192]
FROM ds

huangapple
  • 本文由 发表于 2023年4月6日 19:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定