子串的动态枢轴列

时间:2016-08-20 19:54:21

标签: sql sql-server dynamic-pivot

我有这张桌子:

itemid                  attributename   value
6829556334057807840       Part Notes    Drilling Required: No,Install Time: 30 Minutes,Fascia Trimming: Yes (minor),Visibility: Exposed

我希望能够将Value列中的数据转换为KeyValue对

Example:

    ItemId                  Key                  Value
6829556334057807840       Drilling Required       No
6829556334057807840       Install Time            30
6829556334057807840       Fascia Trimming         Yes (minor)

但是,数据透视必须是动态的,因为“值”列可以具有多个不同的键值,具体取决于列中的内容。

2 个答案:

答案 0 :(得分:2)

考虑数据库的规范化。如果您有未知数据,请使用xml(自SQL Server 2005以来支持)或JSON(自SQL Server 2016以来支持)。使用此查询作为起点:

WITH Src AS
(
    SELECT * FROM (VALUES
        ('6829556334057807840', 'Part Notes', 'Drilling Required: No,Install Time: 30 Minutes,Fascia Trimming: Yes (minor),Visibility: Exposed')
    ) T(itemid, attributename, value)
), Recurse AS
(
    SELECT
        itemid,
        SUBSTRING(value, 1, CASE WHEN CHARINDEX(',', value, 1)=0 THEN LEN(value) ELSE CHARINDEX(',', value, 1)-1 END) Item,
        SUBSTRING(value, CASE WHEN CHARINDEX(',', value, 1)=0 THEN LEN(value) ELSE CHARINDEX(',', value, 1)+1 END, LEN(value)) Rest
    FROM Src
    UNION ALL
    SELECT
        itemid,
        CASE WHEN CHARINDEX(',', Rest, 1)=0 THEN Rest ELSE SUBSTRING(Rest, 1, CHARINDEX(',', Rest)-1) END Item,
        CASE WHEN CHARINDEX(',', Rest, 1)=0 THEN NULL ELSE SUBSTRING(Rest, CHARINDEX(',', Rest)+1, LEN(Rest)) END Rest
    FROM Recurse
    WHERE Rest IS NOT NULL
)
SELECT
    itemid,
    SUBSTRING(Item, 1, CHARINDEX(':', Item)-1) [Key],
    SUBSTRING(Item, CHARINDEX(':', Item)+1, LEN(Item)) [Value]
FROM Recurse

它返回:

itemid               Key                   Value
-------------------  --------------------  -------------
6829556334057807840  Drilling Required     No
6829556334057807840  Install Time          30 Minutes
6829556334057807840  Fascia Trimming       Yes (minor)
6829556334057807840  Visibility            Exposed

答案 1 :(得分:0)

XML的另一种方式:

<script>
var app = angular.module("MyApp",[]);
    app.controller("sController",function ($scope,$http) {
        var url = "http://www.example.com/data.txt";

        $http.get(url).success( function(response) {
        alert('success');
           $scope.students = response;
        }).error(function (response) {
    alert("error");

    return status;
    });
    });
</script> 

对于您提供的表格样本,输出将为:

DECLARE @xml xml

SELECT @xml = (
    SELECT CAST('<item id="' + CAST(itemid as nvarchar(100)) +'"><row><key>' +
            REPLACE(REPLACE([value],',','</value></row><row><key>'),': ','</key><value>')+'</value></row></item>' as xml)
    FROM YourTable
    FOR XML PATH('')
)

SELECT  t.v.value('../@id','bigint') ItemId,                                    
        t.v.value('key[1]','nvarchar(max)') [Key],
        t.v.value('value[1]','nvarchar(max)') [Value]
FROM @xml.nodes('/item/row') as t(v)

这是使用ItemId Key Value 6829556334057807840 Drilling Required No 6829556334057807840 Install Time 30 Minutes 6829556334057807840 Fascia Trimming Yes (minor) 6829556334057807840 Visibility Exposed 转换表格后获得的XML数据:

XML PATH