使用PIVOT函数的帮助

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

Assistance with PIVOT function

问题

DECLARE @T TABLE (ID INT, Col VARCHAR(100), Description VARCHAR(50))
INSERT INTO @T VALUES (1, 'MV-RUC-DEBT-ASSESS', 'MV Debt Assessment');

WITH cte AS (
  SELECT  a.ID,
          REPLACE(a.Col, '-', ' ') AS "Col",
          a.Description
  FROM @T a
),

cte2 AS (
  SELECT  a.ID,
          n.r.value('.', 'varchar(50)') AS "Value",
          a.Description
  FROM cte a
  CROSS APPLY (SELECT CAST('<r>'+REPLACE(REPLACE(Col, '&', '&amp;'), ' ', '</r><r>')+'</r>' AS XML)) AS S(XMLCol)
  CROSS APPLY S.XMLCol.nodes('r') AS n(r)
)

SELECT *
FROM cte2 a
PIVOT 
(MAX(Value) FOR a.ID IN ([1], [2], [3], [4])) AS "Pivot";
英文:

I have a reference table that's currently delimited with '-' that I'm needing to split the values out into multiple columns. Our version of SQL DB doesn't support the string_split function.

The first part of the script (multiple CTE) is returning the results into multiple rows, which I'm then wanting to pivot into columns.

Is someone able to please assist with the PIVOT portion (or even a new statement if it achieves the same result). Am looking to have the results returned per final table format?

Thanks

Original Data:

ID Value Description
1 MV-RUC-DEBT-ASSESS MV Debt Assessment
declare @T table (ID int, Col varchar(100), description varchar(50))
insert into @T values (1, &#39;MV-RUC-DEBT-ASSESS&#39;, &#39;MV Debt Assessment&#39;)
;

  with cte as (
  select	a.ID 
			,replace(a.Col,&#39;-&#39;, &#39; &#39;) as &quot;Col&quot;
			, a.description
		from @T a
  ),

  cte2 as (
  select	a.ID
			, n.r.value(&#39;.&#39;, &#39;varchar(50)&#39;) &quot;Value&quot;
			, a.description
			  from cte a
  cross apply (select cast(&#39;&lt;r&gt;&#39;+replace(replace(Col,&#39;&amp;&#39;,&#39;&amp;amp;&#39;), &#39; &#39;, &#39;&lt;/r&gt;&lt;r&gt;&#39;)+&#39;&lt;/r&gt;&#39; as xml)) as S(XMLCol)
  cross apply S.XMLCol.nodes(&#39;r&#39;) as n(r)
  
  )

  select *
  from cte2 a
  pivot 
 (max(value) for a.ID in ([1], [2], [3], [4])) as &quot;Pivot&quot;

I'm expecting the results to look like

ID Description 1 2 3 4
1 MV Debt Assessment MV RUC DEBT ASSESS

答案1

得分: 1

当您在使用a.ID作为旋转的一部分时,它只有一个,因此您只会获得值的最大值。

添加一个行号将为您提供所需的结果。

declare @T table (ID int, Col varchar(100), description varchar(50))
insert into @T values (1, 'MV-RUC-DEBT-ASSESS', 'MV Debt Assessment')
;

  with cte as (
  select    a.ID 
            ,replace(a.Col,'-', ' ') as "Col"
            , a.description
        from @T a
  ),

  cte2 as (
  select    a.ID
            , n.r.value('.', 'varchar(50)') "Value"
    , row_number() OVER(PARTITION BY a.ID ORDER BY a.ID) rn
            , a.description
              from cte a
  cross apply (select cast('<r>'+replace(replace(Col,'&','&amp;'), ' ', '</r><r>')+'</r>' as xml)) as S(XMLCol)
  cross apply S.XMLCol.nodes('r') as n(r)
  
  )

  select *
  from cte2 a
  pivot 
 (max(value) for a.rn in ([1], [2], [3], [4])) as "Pivot";


ID description 1 2 3 4
1 MV Debt Assessment MV RUC DEBT ASSESS

fiddle

英文:

When you use a.ID as part of the pivot, it has only ones so that you only would get the max value of value.

adding a row_number would give you the wanted result

declare @T table (ID int, Col varchar(100), description varchar(50))
insert into @T values (1, &#39;MV-RUC-DEBT-ASSESS&#39;, &#39;MV Debt Assessment&#39;)
;

  with cte as (
  select    a.ID 
            ,replace(a.Col,&#39;-&#39;, &#39; &#39;) as &quot;Col&quot;
            , a.description
        from @T a
  ),

  cte2 as (
  select    a.ID
            , n.r.value(&#39;.&#39;, &#39;varchar(50)&#39;) &quot;Value&quot;
    , row_number() OVER(PARTITION BY a.ID ORDER BY a.ID) rn
            , a.description
              from cte a
  cross apply (select cast(&#39;&lt;r&gt;&#39;+replace(replace(Col,&#39;&amp;&#39;,&#39;&amp;&#39;), &#39; &#39;, &#39;&lt;/r&gt;&lt;r&gt;&#39;)+&#39;&lt;/r&gt;&#39; as xml)) as S(XMLCol)
  cross apply S.XMLCol.nodes(&#39;r&#39;) as n(r)
  
  )

  select *
  from cte2 a
  pivot 
 (max(value) for a.rn in ([1], [2], [3], [4])) as &quot;Pivot&quot;


ID description 1 2 3 4
1 MV Debt Assessment MV RUC DEBT ASSESS

fiddle

huangapple
  • 本文由 发表于 2023年2月7日 05:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366658.html
匿名

发表评论

匿名网友

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

确定