使用标题分成列的多个值

时间:2018-07-18 10:00:16

标签: tsql

我有一列,其中包含多个用“:”分隔的值,还包括用“:”分隔的标题。 该列如下所示:

{"Id":"c7d45d60-cf90-490d-be4c-f04c9349da03","RecordType":20,"CreationTime":"2018-06-12T21:48:30","Operation":"DownloadReport","OrganizationId":"dabd5d90-87c2-44c9-93cd-783e03236e40","UserType":0,"UserKey":"10033FFF85612C0C","Workload":"PowerBI","UserId":"xxxxxx@grundfos.com","ClientIP":"xxx.3.xx.111","UserAgent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/66.0.3359.181 Safari\/537.36","Activity":"DownloadReport","ItemName":"My Salesresponsibility Final 12-06-2018","WorkSpaceName":"My Sales Dashboard testing environment","DatasetName":"My Salesresponsibility Final 12-06-2018","ReportName":"My Salesresponsibility Final 12-06-2018","CapacityId":"65CB790D-16C8-4DA6-BFA2-89854100423E","CapacityName":"Hope","WorkspaceId":"34812eed-830a-43b2-ae18-6c5c1c0c92a1","ObjectId":"My Salesresponsibility Final 12-06-2018","DatasetId":"b6184b73-fea2-4244-83ca-328e89c536af","ReportId":"36e6b6c5-ceeb-4d02-9d54-fb820634e839","IsSuccess":true,"DatapoolRefreshScheduleType":"None"}

是否可以将这些值及其标头拆分为单独的列? 因此,第一列将被命名为“ Id”,并包含值“ c7d45d60-cf90-490d-be4c-f04c9349da03”,依此类推。 我曾尝试遵循最受好评的解决方案来解决这个类似问题,但是它似乎不起作用: How to split a comma-separated value to columns

所以我想从这里开始:

enter image description here

对于这样的事情:

enter image description here

我尝试使用此代码,但它确实不能很好地工作:

SELECT [AuditData]
      ,Substring(AuditData, 1,Charindex(',', AuditData)-1) as Id
      ,Substring(AuditData, Charindex(',', AuditData)+1, LEN(AuditData)) as  RecordType
      ,Substring(AuditData, Charindex(',', AuditData)+2, LEN(AuditData)) as  CreationTime
  FROM [dbo].[Log]

4 个答案:

答案 0 :(得分:0)

如果您按照上面的评论所述修复数据,则可以使用Jeff Moden's splitter function来使用此方法

最好使用永远不会出现在数据集中的分隔符

;WITH CteSpace AS(
    SELECT *
    FROM dbo.DelimitedSplit8K(REPLACE(REPLACE(REPLACE(@j,'"', ''),'{', ''), '}', '') , ',')
)
SELECT
    [Id]    = LEFT(Item, CHARINDEX(':', Item) - 1),
    [Value] = SUBSTRING(Item, CHARINDEX(':', Item) +1, LEN(Item) - CHARINDEX(':', Item))
FROM CteSpace

提供此输出

Id                          Value
Id                          c7d45d60-cf90-490d-be4c-f04c9349da03
RecordType                  20
CreationTime                2018-06-12T21-48-30
Operation                   DownloadReport
OrganizationId              dabd5d90-87c2-44c9-93cd-783e03236e40
UserType                    0
UserKey                     10033FFF85612C0C
Workload                    PowerBI
UserId                      xxxxxx@grundfos.com
ClientIP                    xxx.3.xx.111
UserAgent                   Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML- like Gecko) Chrome\/66.0.3359.181 Safari\/537.36
Activity                    DownloadReport
ItemName                    My Salesresponsibility Final 12-06-2018
WorkSpaceName               My Sales Dashboard testing environment
DatasetName                 My Salesresponsibility Final 12-06-2018
ReportName                  My Salesresponsibility Final 12-06-2018
CapacityId                  65CB790D-16C8-4DA6-BFA2-89854100423E
CapacityName                Hope
WorkspaceId                 34812eed-830a-43b2-ae18-6c5c1c0c92a1
ObjectId                    My Salesresponsibility Final 12-06-2018
DatasetId                   b6184b73-fea2-4244-83ca-328e89c536af
ReportId                    36e6b6c5-ceeb-4d02-9d54-fb820634e839
IsSuccess                   true
DatapoolRefreshScheduleType None

答案 1 :(得分:0)

这没什么奇怪的,只是JSON。 Starting with version 2016 you can use FROM OPENJSON()像这里:

DECLARE @v VARCHAR(MAX)='{"Id":"c7d45d60-cf90-490d-be4c-f04c9349da03","RecordType":20,"CreationTime":"2018-06-12T21:48:30","Operation":"DownloadReport","OrganizationId":"dabd5d90-87c2-44c9-93cd-783e03236e40","UserType":0,"UserKey":"10033FFF85612C0C","Workload":"PowerBI","UserId":"xxxxxx@grundfos.com","ClientIP":"xxx.3.xx.111","UserAgent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/66.0.3359.181 Safari\/537.36","Activity":"DownloadReport","ItemName":"My Salesresponsibility Final 12-06-2018","WorkSpaceName":"My Sales Dashboard testing environment","DatasetName":"My Salesresponsibility Final 12-06-2018","ReportName":"My Salesresponsibility Final 12-06-2018","CapacityId":"65CB790D-16C8-4DA6-BFA2-89854100423E","CapacityName":"Hope","WorkspaceId":"34812eed-830a-43b2-ae18-6c5c1c0c92a1","ObjectId":"My Salesresponsibility Final 12-06-2018","DatasetId":"b6184b73-fea2-4244-83ca-328e89c536af","ReportId":"36e6b6c5-ceeb-4d02-9d54-fb820634e839","IsSuccess":true,"DatapoolRefreshScheduleType":"None"}';
SELECT *
FROM OPENJSON(@v);

结果

key            value                                 type
Id             c7d45d60-cf90-490d-be4c-f04c9349da03  1
RecordType     20                                    2
CreationTime   2018-06-12T21:48:30                   1
[... more rows ...]

更新将其作为PIVOT列表

有两种方法:

DECLARE @v VARCHAR(MAX)='{"Id":"c7d45d60-cf90-490d-be4c-f04c9349da03","RecordType":20,"CreationTime":"2018-06-12T21:48:30","Operation":"DownloadReport","OrganizationId":"dabd5d90-87c2-44c9-93cd-783e03236e40","UserType":0,"UserKey":"10033FFF85612C0C","Workload":"PowerBI","UserId":"xxxxxx@grundfos.com","ClientIP":"xxx.3.xx.111","UserAgent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/66.0.3359.181 Safari\/537.36","Activity":"DownloadReport","ItemName":"My Salesresponsibility Final 12-06-2018","WorkSpaceName":"My Sales Dashboard testing environment","DatasetName":"My Salesresponsibility Final 12-06-2018","ReportName":"My Salesresponsibility Final 12-06-2018","CapacityId":"65CB790D-16C8-4DA6-BFA2-89854100423E","CapacityName":"Hope","WorkspaceId":"34812eed-830a-43b2-ae18-6c5c1c0c92a1","ObjectId":"My Salesresponsibility Final 12-06-2018","DatasetId":"b6184b73-fea2-4244-83ca-328e89c536af","ReportId":"36e6b6c5-ceeb-4d02-9d54-fb820634e839","IsSuccess":true,"DatapoolRefreshScheduleType":"None"}';

-PIVOT

SELECT p.*
FROM
(
    SELECT [value],[key]
    FROM OPENJSON(@v)
) t
PIVOT
(
    MAX(t.[value]) FOR t.[key] in(Id,RecordType,CreationTime)
) p;

-JSON_VALUE

SELECT JSON_VALUE(@v,'$.Id') AS Id
      ,JSON_VALUE(@v,'$.RecordType') AS RecordType
      ,JSON_VALUE(@v,'$.CreationTime') AS CreationTime

尝试一下...

更新2:从表中获取

这很简单。

DECLARE @tbl TABLE(ID INT IDENTITY, yourJSON NVARCHAR(MAX));
INSERT INTO @tbl VALUES('{"Id":"blah1","RecordType":10]}')
                      ,('{"Id":"blah2","RecordType":20]}')
                      ,('{"Id":"blah3","RecordType":30]}');
SELECT t.ID 
      ,JSON_VALUE(t.yourJSON,'$.Id') AS JsonId
      ,JSON_VALUE(t.yourJSON,'$.RecordType') AS RecordType
FROM @tbl t;

提示

下一个问题:请不要粘贴图片。像上面一样,尝试添加精简的独立运行示例so called MCVE)。其他情况下,人们必须为您输入。

答案 2 :(得分:0)

如果您没有SQL 2016并想使用JSON字符串,则可以使用Phil Factor's ParseJSON function

然后,如@Shnugo所提到的,无需担心分隔符出现在数据集中并破坏代码

DECLARE @j NVARCHAR(MAX) = '{"Id":"c7d45d60-cf90-490d-be4c-f04c9349da03","RecordType":20,"CreationTime":"2018-06-12T21-48-30","Operation":"DownloadReport","OrganizationId":"dabd5d90-87c2-44c9-93cd-783e03236e40","UserType":0,"UserKey":"10033FFF85612C0C","Workload":"PowerBI","UserId":"xxxxxx@grundfos.com","ClientIP":"xxx.3.xx.111","UserAgent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML- like Gecko) Chrome\/66.0.3359.181 Safari\/537.36","Activity":"DownloadReport","ItemName":"My Salesresponsibility Final 12-06-2018","WorkSpaceName":"My Sales Dashboard testing environment","DatasetName":"My Salesresponsibility Final 12-06-2018","ReportName":"My Salesresponsibility Final 12-06-2018","CapacityId":"65CB790D-16C8-4DA6-BFA2-89854100423E","CapacityName":"Hope","WorkspaceId":"34812eed-830a-43b2-ae18-6c5c1c0c92a1","ObjectId":"My Salesresponsibility Final 12-06-2018","DatasetId":"b6184b73-fea2-4244-83ca-328e89c536af","ReportId":"36e6b6c5-ceeb-4d02-9d54-fb820634e839","IsSuccess":true,"DatapoolRefreshScheduleType":"None"}'


SELECT name, stringvalue
FROM dbo.parseJSON(@j)

输出

name            stringvalue
Id              c7d45d60-cf90-490d-be4c-f04c9349da03
RecordType      20
CreationTime    2018-06-12T21-48-30
Operation       DownloadReport
OrganizationId  dabd5d90-87c2-44c9-93cd-783e03236e40
UserType        0
UserKey         10033FFF85612C0C

答案 3 :(得分:0)

以一种非常round回的方式,我得到了所需的答案。我通过以下方式创建了一个包含该列中JSON的新视图:

SELECT
        JSON_VALUE(AuditData,'$.Id') AS Id
        ,JSON_VALUE(AuditData,'$.RecordType') AS RecordType
        ,JSON_VALUE(AuditData,'$.CreationTime') AS CreationTime
        ,JSON_VALUE(AuditData,'$.Operation') AS Operation
        ,JSON_VALUE(AuditData,'$.OrganizationId') AS OrganizationId
        ,JSON_VALUE(AuditData,'$.UserType') AS UserType
        ,JSON_VALUE(AuditData,'$.UserKey') AS UserKey
        ,JSON_VALUE(AuditData,'$.Workload') AS Workload
        ,JSON_VALUE(AuditData,'$.UserId') AS UserId
        ,JSON_VALUE(AuditData,'$.ClientIP') AS ClientIP
        ,JSON_VALUE(AuditData,'$.UserAgent') AS UserAgent
        ,JSON_VALUE(AuditData,'$.Activity') AS Activity
        ,JSON_VALUE(AuditData,'$.ItemName') AS ItemName
        ,JSON_VALUE(AuditData,'$.WorkSpaceName') AS WorkSpaceName
        ,JSON_VALUE(AuditData,'$.DatasetName') AS DatasetName
        ,JSON_VALUE(AuditData,'$.ReportName') AS ReportName
        ,JSON_VALUE(AuditData,'$.CapacityId') AS CapacityId
        ,JSON_VALUE(AuditData,'$.CapacityName') AS CapacityName
        ,JSON_VALUE(AuditData,'$.WorkspaceId') AS WorkspaceId
        ,JSON_VALUE(AuditData,'$.ObjectId') AS ObjectId
        ,JSON_VALUE(AuditData,'$.DatasetId') AS DatasetId
        ,JSON_VALUE(AuditData,'$.ReportId') AS ReportId
        ,JSON_VALUE(AuditData,'$.IsSuccess') AS IsSuccess
        ,JSON_VALUE(AuditData,'$.DatapoolRefreshScheduleType') AS DatapoolRefreshScheduleType

FROM [dbo].[Audit_Log];

非常感谢@Shnugo为我这样的新手帮助Stackoverflow。