SQL从列中提取数据

时间:2016-09-01 19:13:47

标签: sql azure-sql-database

寻找一些SQL帮助从表中的列中选择特定数据,并使用该列创建新列。以下是AttributeDescription列中的数据示例:

AttributeDescription
----------------------------------
Color: black<br />Size: L
Color: black<br />Size: M
Color: black<br />Size: S
Colour: turquoise<br />Size: XXL
Colour: white<br />Size: M
Colour: white<br />Size: XL
Model: iPhone 5/5s
Model: iPhone 6
Size: 2X-Large
Size: Large
Size: Medium
Size: Medium / Small
Size: Small
Size: XL
Size: X-Large
Size: X-Large / Large`

我想把它变成名为“Variant_Model”,“Variant_Size”和“Variant_Color”的三列。最终结果应该是:

Variant_Model |Variant_Size | Variant_Color
--------------+-------------+-------------
              | L           | black
              | M           | black
              | S           | black
              | XXL         | turquoise
              | M           | white
              | XL          | white
iPhone 5/5s   |             |
iPhone 6      |             |
              | 2X-Large    |
              | Large       |
              | Medium      |
              | Medium / Small|
              | Small       |
              | XL          |
              | X-Large     |
              | X-Large / Large|   `

请注意,在原始列中,属性可能显示为“颜色”或“颜色”。

将缩写尺寸(例如“L”,“M”)标准化为适当的单词的加分点!

非常感谢!

1 个答案:

答案 0 :(得分:1)

这样做的一种方法就是这样

<强>查询:

SELECT Model AS Variant_Model,
       CASE
           WHEN size = 'L'
           THEN 'Large'
           WHEN size = 'M'
           THEN 'Medium'
           WHEN size = 'S'
           THEN 'Small'
           WHEN size = 'XL'
           THEN 'X-Large'
           WHEN size = 'XXL'
           THEN '2X-Large'
           ELSE size
       END AS Variant_Size,
       COALESCE(color, Colour) AS Variant_Color
FROM
(
    SELECT id,
           LEFT(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS    VARCHAR(255)), '<X>', ''), '</X>', ''), CHARINDEX(':', REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', ''))-1) AS header,
           REVERSE(LEFT(REVERSE(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', '')), CHARINDEX(':', REVERSE(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', '')))-2)) AS Value
    FROM
    (
        SELECT ROW_NUMBER() OVER(ORDER BY AttributeDescription) AS id,
               CAST(('<X>'+REPLACE(REPLACE(AttributeDescription, '<br />', ','), ',', '</X><X>')+'</X>') AS XML) AS String
        FROM #a
    ) T
    CROSS APPLY String.nodes('/X') AS T2(Loc)
) data PIVOT(MAX(value) FOR header IN(Color,
                                      Size,
                                      Model,
                                      Colour)) p;

完成工作脚本

IF OBJECT_ID('tempdb..#a') IS NOT NULL
    DROP TABLE #a;

CREATE TABLE #a(AttributeDescription VARCHAR(255));

INSERT INTO #a
VALUES('Color: black<br />Size: L');

INSERT INTO #a
VALUES('Color: black<br />Size: M');

INSERT INTO #a
VALUES('Color: black<br />Size: S');

INSERT INTO #a
VALUES('Colour: turquoise<br />Size: XXL');

INSERT INTO #a
VALUES('Colour: white<br />Size: M');

INSERT INTO #a
VALUES('Colour: white<br />Size: XL');

INSERT INTO #a
VALUES('Model: iPhone 5/5s');

INSERT INTO #a
VALUES('Model: iPhone 6');

INSERT INTO #a
VALUES('Size: 2X-Large');

INSERT INTO #a
VALUES('Size: Large');

INSERT INTO #a
VALUES('Size: Medium');

INSERT INTO #a
VALUES('Size: Medium / Small');

INSERT INTO #a
VALUES('Size: Small');

INSERT INTO #a
VALUES('Size: XL');

INSERT INTO #a
VALUES('Size: X-Large');

INSERT INTO #a
VALUES('Size: X-Large / Large');

SELECT Model AS Variant_Model,
       CASE
           WHEN size = 'L'
           THEN 'Large'
           WHEN size = 'M'
           THEN 'Medium'
           WHEN size = 'S'
           THEN 'Small'
           WHEN size = 'XL'
           THEN 'X-Large'
           WHEN size = 'XXL'
           THEN '2X-Large'
           ELSE size
       END AS Variant_Size,
       COALESCE(color, Colour) AS Variant_Color
FROM
(
    SELECT id,
           LEFT(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS    VARCHAR(255)), '<X>', ''), '</X>', ''), CHARINDEX(':', REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', ''))-1) AS header,
           REVERSE(LEFT(REVERSE(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', '')), CHARINDEX(':', REVERSE(REPLACE(REPLACE(CAST(T2.Loc.query('.') AS VARCHAR(255)), '<X>', ''), '</X>', '')))-2)) AS Value
    FROM
    (
        SELECT ROW_NUMBER() OVER(ORDER BY AttributeDescription) AS id,
               CAST(('<X>'+REPLACE(REPLACE(AttributeDescription, '<br />', ','), ',', '</X><X>')+'</X>') AS XML) AS String
        FROM #a
    ) T
    CROSS APPLY String.nodes('/X') AS T2(Loc)
) data PIVOT(MAX(value) FOR header IN(Color,
                                      Size,
                                      Model,
                                      Colour)) p;