【问题标题】:Condense XML subsequent rows into parent Row将 XML 后续行压缩为父行
【发布时间】:2015-12-29 14:16:33
【问题描述】:

我有一个难题要解决,用 TSql。

以下是一组遍布日志文件的示例数据。

我正在研究将“有效”内容转换为列的过程。除了唯一的 XML 格式行之外,我可以将每一行放入列中。如问题部分所述,如果没有完全炸毁,当涉及到其中只有 XML 元素的行时,它就会出错。

我已经能够从解析器中分离出“无效”行,但是如果没有后续信息,数据所属的行就无关紧要了。

问题

目录中带有{DELIM} 的行需要将后续行与它连接起来,以形成单行。

示例数据:

RowID                FileID               Contents
-------------------- -------------------- ---------
3595                 1                    8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3596                 1                      <LoggedOn>true</LoggedOn>
3597                 1                      <ProfileLoaded>false</ProfileLoaded>
3598                 1                      <UsingLocalLogon>false</UsingLocalLogon>
3599                 1                      <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3600                 1                      <ErrorOccured>false</ErrorOccured>
3601                 1                      <ErrorString />
3602                 1                    </ImpersonationResultClass>
3629                 1                    8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3630                 1                      <LoggedOn>true</LoggedOn>
3631                 1                      <ProfileLoaded>false</ProfileLoaded>
3632                 1                      <UsingLocalLogon>false</UsingLocalLogon>
3633                 1                      <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3634                 1                      <ErrorOccured>false</ErrorOccured>
3635                 1                      <ErrorString />
3636                 1                    </ImpersonationResultClass>
3653                 1                    8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3654                 1                      <LoggedOn>true</LoggedOn>
3655                 1                      <ProfileLoaded>false</ProfileLoaded>
3656                 1                      <UsingLocalLogon>false</UsingLocalLogon>
3657                 1                      <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
3658                 1                      <ErrorOccured>false</ErrorOccured>
3659                 1                      <ErrorString />
3660                 1                    </ImpersonationResultClass>
5239                 1                    8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5240                 1                      <LoggedOn>true</LoggedOn>
5241                 1                      <ProfileLoaded>false</ProfileLoaded>
5242                 1                      <UsingLocalLogon>false</UsingLocalLogon>
5243                 1                      <ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>
5244                 1                      <ErrorOccured>false</ErrorOccured>
5245                 1                      <ErrorString />
5246                 1                    </ImpersonationResultClass>

问题

我能否获得一些帮助以将后续 XML 元素压缩到具有有效日期时间的单行中?

例如:

RowID                FileID               Contents
-------------------- -------------------- ---------
3595                 1                    8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3629                 1                    8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
3653                 1                    8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
5239                 1                    8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>

【问题讨论】:

    标签: sql-server xml sql-server-2012


    【解决方案1】:

    我不确定你为什么需要这个,它可能效率不高,而且可能有更好的方法。

    但是您可能没有其他选择,这里有一种方法。

    你的数据:

        declare @data table(RowID int, FileID int, Contents nvarchar(max))
        insert into @data(RowID, FileID, Contents) values
        (3595, 1, '8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
        , (3596, 1, '<LoggedOn>true</LoggedOn>')
        , (3597, 1, '<ProfileLoaded>false</ProfileLoaded>')
        , (3598, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
        , (3599, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
        , (3600, 1, '<ErrorOccured>false</ErrorOccured>')
        , (3601, 1, '<ErrorString />')
        , (3602, 1, '</ImpersonationResultClass>')
        , (3629, 1, '8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
        , (3630, 1, '<LoggedOn>true</LoggedOn>')
        , (3631, 1, '<ProfileLoaded>false</ProfileLoaded>')
        , (3632, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
        , (3633, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
        , (3634, 1, '<ErrorOccured>false</ErrorOccured>')
        , (3635, 1, '<ErrorString />')
        , (3636, 1, '</ImpersonationResultClass>')
        , (3653, 1, '8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
        , (3654, 1, '<LoggedOn>true</LoggedOn>')
        , (3655, 1, '<ProfileLoaded>false</ProfileLoaded>')
        , (3656, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
        , (3657, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
        , (3658, 1, '<ErrorOccured>false</ErrorOccured>')
        , (3659, 1, '<ErrorString />')
        , (3660, 1, '</ImpersonationResultClass>')
        , (5239, 1, '8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
        , (5240, 1, '<LoggedOn>true</LoggedOn>')
        , (5241, 1, '<ProfileLoaded>false</ProfileLoaded>')
        , (5242, 1, '<UsingLocalLogon>false</UsingLocalLogon>')
        , (5243, 1, '<ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs>')
        , (5244, 1, '<ErrorOccured>false</ErrorOccured>')
        , (5245, 1, '<ErrorString />')
        , (5246, 1, '</ImpersonationResultClass>');
    

    查询:

        with rows as(
            Select RowID, FileID, Contents
                , n = ROW_NUMBER() over(order by RowId)
            From @data 
            Where Contents like '%{DELIM}%'
        ), cat as (
            Select ID = r1.RowID, d.RowID, d.Contents From rows as r1
            Left Join rows as r2 on r1.n = r2.n - 1
            Left Join @data as d On d.RowID > r1.RowID and (d.RowID < r2.RowID or r2.RowID is null)
        )
        Select r.RowID, r.FileId
            , r.Contents+(
                Select c.Contents
                From cat as c
                Where c.ID = r.RowID 
                Order By c.RowID
                For XML PATH (''), TYPE
            ).value('.', 'varchar(max)')
        From rows r
    

    另一种选择:

        with rows as(
            Select RowID, FileID, Contents
                , n = Case When Contents like '%{DELIM}%' Then 
                        ROW_NUMBER() over(partition by case when Contents like '%{DELIM}%' then 0 else 1 end order by RowId)
                    Else  ROW_NUMBER() over(order by RowId) -
                        ROW_NUMBER() over(order by case when Contents like '%{DELIM}%' then 1 else 0 end, RowId)
                    End
            From @data 
        )
        Select r.RowID, r.FileId
            , FIRST_VALUE(Contents) Over(partition by n order by RowId) + (
                Select r2.Contents
                From rows as r2
                Where r.n = r2.n and Contents not like '%{DELIM}%'
                Order By r2.RowID
                For XML PATH (''), TYPE
            ).value('.', 'varchar(max)')
        From rows as r
        Where r.Contents like '%{DELIM}%'
        Order By RowID
    

    输出:

        RowID  FileId  Contents
        3595   1       8/30/2015 5:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
        3629   1       8/30/2015 5:00:03 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
        3653   1       8/30/2015 5:00:12 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
        5239   1       8/30/2015 7:00:01 AM{DELIM}Debug{DELIM}Impersonation result 1: <ImpersonationResultClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><LoggedOn>true</LoggedOn><ProfileLoaded>false</ProfileLoaded><UsingLocalLogon>false</UsingLocalLogon><ImpersonatedAs>NT AUTHORITY\SYSTEM</ImpersonatedAs><ErrorOccured>false</ErrorOccured><ErrorString /></ImpersonationResultClass>
    

    【讨论】:

    • 简而言之,我有一个日志要编入 SQL。不幸的是,我无法访问日志记录方法(第 3 方软件)。日志文件的 80% 是 1 行条目;在某些情况下,它具有跨多行数据的格式良好的 XML 格式条目。将 RawData 拆分为两个表,一个用于解析单条目行,另一个用于 XML 数据。
    • 我的回答对你有帮助吗?
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2017-12-25
    • 2015-12-29
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多