0

我使用下面的代码在某个字符或数字出现后提取分隔字符串中如此多的数据,我希望在提取每个这些值之前提取另一条数据。尽管我确实了解其中的元素,但我并不完全了解代码的工作原理。我只是希望重复拉取的过程并将其整合到查询结果中。

USE RUG_Data 

IF OBJECT_ID('tempdb..#splitoutdata','U') IS NOT NULL DROP TABLE #SPLITOUTDATA;
CREATE TABLE #SPLITOUTDATA (
    INDEX1 INT,
    ROWNUM INT,
    BITOFDATA VARCHAR(max));


IF OBJECT_ID(N'dbo.Split_XML', N'TF') IS NOT NULL DROP FUNCTION dbo.Split_XML
GO
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
CREATE FUNCTION dbo.Split_XML
    (
     @Parameter VARCHAR(MAX)
    ,@Delimiter VARCHAR(1)
    )
RETURNS @Result TABLE
    (
     ItemNumber INT
    ,ItemValue VARCHAR(MAX)
    )
AS 
    BEGIN
        DECLARE @XML XML ;
        SET @Parameter = ( SELECT   @Parameter
                         FOR XML PATH('')
                         ) ;
        SELECT  @XML = '<r>' + REPLACE(@Parameter, @Delimiter, '</r><r>') + '</r>' ;

        INSERT  INTO @Result
                (
                 ItemNumber
                ,ItemValue
                )
                SELECT  ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL) ) AS ItemNumber
                ,       Item.value('text()[1]', 'VARCHAR(MAX)') AS ItemValue
                FROM    @XML.nodes('//r') R ( Item ) ;
        RETURN ;
    END ;
GO


;WITH
REFORMATTEDDATA AS
(
SELECT  
    row_number()over(order by (select null)) as INDEX1,

    REPLACE(REPLACE(CAST(DATA AS VARCHAR(MAX)),CHAR(13),''),CHAR(10),'')AS RAWCLOB2
FROM
    RUG_CLOB
WHERE
    CAST(DATA AS VARCHAR(MAX)) LIKE 'ZHV|FS0000%%%|D0003001%'
)



INSERT INTO #SPLITOUTDATA 
SELECT 
      INDEX1, 
      ROW_NUMBER()OVER(PARTITION BY INDEX1 ORDER BY split.ItemNumber) AS ROWNUM,
      split.ItemValue AS BITOFDATA 
 FROM REFORMATTEDDATA
 CROSS APPLY dbo.Split_XML(reformatteddata.RAWCLOB2,'|') SPLIT


CREATE CLUSTERED INDEX idx1 ON #SPLITOUTDATA (INDEX1,ROWNUM)


SELECT

    [Date], [MPAN],
    [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],
    [20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50] 

FROM
( 
    SELECT 
    INDEX1, 
    (SELECT BITOFDATA FROM #SPLITOUTDATA so2 WHERE so2.INDEX1 = so1.INDEX1 and so2.ROWNUM = 8) AS [Date] ,
    (SELECT BITOFDATA FROM #SPLITOUTDATA so2 WHERE so2.INDEX1 = so1.INDEX1 and so2.ROWNUM = 14) AS [MPAN] ,

    ROW_NUMBER()OVER(PARTITION BY INDEX1 ORDER BY ROWNUM) AS ROWNUM, 




    (SELECT BITOFDATA FROM #SPLITOUTDATA so4 WHERE so4.INDEX1 = so1.INDEX1 AND so4.ROWNUM = so1.ROWNUM +3) AS BITOFDATA 
    FROM #SPLITOUTDATA so1 
    WHERE BITOFDATA = '012' 


        --  AND 
        --(SELECT BITOFDATA FROM #SPLITOUTDATA so5 WHERE so5.DC_INDEX_FK = so1.DC_INDEX_FK AND so5.ROWNUM = 10) NOT IN ('TR01')
    ) p 
    PIVOT 
        (MAX (BITOFDATA) 
    FOR ROWNUM IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]) 
) AS PVT



IF OBJECT_ID('tempdb..#splitoutdata','U') IS NOT NULL DROP TABLE #SPLITOUTDATA;
IF OBJECT_ID(N'dbo.Split_XML', N'TF') IS NOT NULL DROP FUNCTION dbo.Split_XML

目前我的查询结果显示为——</p>

[Date], [MPAN],
[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],
[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50] 

我希望拥有

[Date], [MPAN],
[E/A],[1], [E/A], [2], [E/A], [3], [E/A], [4], [E/A], [5], [E/A], [6], [E/A], [7], [E/A], [8], [E/A], [9], [E/A], [10], [E/A], [11], [E/A], [12], [E/A], [13], [E/A], [14], [E/A], [15], [E/A], [16], [E/A], [17], [E/A], [18], [E/A], [19], [E/A],[20], [E/A], [21], [E/A], [22], [E/A], [23], [E/A], [24], [E/A], [25], [E/A], [26], [E/A], [27], [E/A],[28], [E/A], [29], [E/A], [30], [E/A], [31], [E/A], [32], [E/A], [33], [E/A], [34], [E/A],[35], [E/A],[36], [E/A], [37], [E/A], [38], [E/A], [39], [E/A], [40], [E/A], [41], [E/A], [42], [E/A], [43], [E/A], [44], [E/A], [45], [E/A], [46], [E/A], [47], [E/A], [48], [E/A],[49], [E/A], [50] 

E/A 值也在分隔字符串内,并且在我每次拉取的值之前都有一个部分,所以我假设我会以某种方式使用这段代码来拉取它:

(SELECT BITOFDATA FROM #SPLITOUTDATA so4 WHERE so4.INDEX1 = so1.INDEX1 AND so4.ROWNUM = so1.ROWNUM +2) AS BITOFDATA 
FROM #SPLITOUTDATA so1 
WHERE BITOFDATA = '012' 

由于某种原因,我只是无法让它工作,因为我对此很陌生,但我认为它相当简单。

对不起,如果我没有很好地解释它。

4

1 回答 1

0

没有样本数据很难判断,但是您应该能够对 PIVOT 的结果进行第二次 PIVOT 以获得您所追求的结果

于 2012-09-03T13:50:37.870 回答