1

我正在寻找 tsql 中的连续算法。我的数据设置如下。

IF OBJECT_ID('Tempdb..#Area_Version') IS NOT NULL 
DROP TABLE #Area_Version
CREATE TABLE #Area_Version
(
  AreaVersionId INT ,
  AreaId VARCHAR(1) ,
  VersionId VARCHAR(2) ,
  PocketGroupId INT
)

INSERT  INTO #Area_Version
    ( AreaVersionId, AreaId, VersionId, PocketGroupId )
VALUES  ( 1, 'A', 'V5', 0 ),
    ( 2, 'B', 'V2', 0 ),
    ( 3, 'C', 'V2', 0 ),
    ( 4, 'D', 'V3', 0 ),
    ( 5, 'E', 'V2', 0 ),
    ( 6, 'F', 'V1', 0 ),
    ( 7, 'F', 'V4', 0 ),
    ( 8, 'G', 'V3', 0 );   

结果:

AreaVersionId AreaId VersionId PocketGroupId
------------- ------ --------- -------------
1             A      V5        0
2             B      V2        0
3             C      V2        0
4             D      V3        0
5             E      V2        0
6             F      V1        0
7             F      V4        0
8             G      V3        0

(8 row(s) affected)

下面是数据的可视化表示。

AreaId  V1  V2  V3  V4  V5
A                        x
B            x            
C            x            
D                x        
E            x            
F        x           x  
G                x

按 AreaId 排序,我正在寻找连续的组。由于 V2 跨越 B、C 和 E 区域,因此 D 中的 V2 和 V3 不能以一个连续的顺序排列。上面的数据集可以分为两组。(V5、V2、V1 和 V3、V4)或(V5、V3、V4 和 V2、V1)。我可以根据超出问题范围的数量列来决定哪个集合优先。

根据算法的结果集,我会更新PocketGroupId,这样我就可以根据PocketGroupId进行查询,我会知道它是一个连续的组。

期望的结果应该是这样的。

AreaVersionId AreaId VersionId PocketGroupId
------------- ------ --------- -------------
1             A      V5        1
2             B      V2        1
3             C      V2        1
4             D      V3        2
5             E      V2        1
6             F      V1        1
7             F      V4        3
8             G      V3        2

任何想法如何实现这一点。提前致谢。

4

2 回答 2

1

这是一个使用游标和while循环的动态版本......以及额外的临时表。这很糟糕,但很有效。

IF OBJECT_ID('Tempdb..#Area_Version') IS NOT NULL
    DROP TABLE #Area_Version

CREATE TABLE #Area_Version
    (
      AreaVersionId INT ,
      AreaId VARCHAR(1) ,
      VersionId VARCHAR(2) ,
      PocketGroupId INT
    )

INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (1, 'A', 'V5', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (2, 'B', 'V2', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (3, 'C', 'V2', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (4, 'D', 'V3', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (5, 'E', 'V2', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (6, 'F', 'V1', 0)
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (7, 'F', 'V4', 0)    
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (8, 'G', 'V2', 0)    
INSERT INTO #Area_Version (AreaVersionId, AreaId, VersionId, PocketGroupId) VALUES (9, 'H', 'V4', 0)    

IF OBJECT_ID('Tempdb..#WorkingArea_Version') IS NOT NULL
    DROP TABLE #WorkingArea_Version

CREATE TABLE #WorkingArea_Version
    (
      AreaVersionId INT ,
      AreaId VARCHAR(1) ,
      VersionId VARCHAR(2) ,
      PocketGroupId INT
    )

INSERT INTO #WorkingArea_Version
SELECT * FROM #Area_Version

IF OBJECT_ID('Tempdb..#resultset') IS NOT NULL
    DROP TABLE #resultset

CREATE TABLE #resultset
    (
      PocketGroupId INT ,
      VersionId NVARCHAR(MAX)
    )

DECLARE @areaCount INT 
SELECT  @areaCount = COUNT(1)
FROM    #WorkingArea_Version av

 DECLARE @PocketGroupId INT
SET @PocketGroupId = 1

WHILE ( @areaCount > 0 )
    BEGIN

        DECLARE @AreaVersionId INT ,
            @AreaId VARCHAR(1) ,
            @VersionId VARCHAR(2)

        DECLARE db_cursor CURSOR
        FOR
            SELECT  av.AreaVersionId ,
                    av.AreaId ,
                    av.VersionId
            FROM    #WorkingArea_Version av
            ORDER BY av.AreaId

        OPEN db_cursor   
        FETCH NEXT FROM db_cursor INTO @AreaVersionId, @AreaId, @VersionId


        WHILE @@FETCH_STATUS = 0
            BEGIN   

                DECLARE @Versions NVARCHAR(MAX) = ''
                IF NOT EXISTS ( SELECT TOP 1
                                        1
                                FROM    #resultset r
                                WHERE   r.PocketGroupId = @PocketGroupId )
                    BEGIN

                        INSERT  INTO #resultset
                                ( PocketGroupId, VersionId )
                        VALUES  ( @PocketGroupId, @VersionId )                
                    END
                ELSE
                    BEGIN

                        DECLARE @minAreaId NVARCHAR(1)
                        DECLARE @maxAreaId NVARCHAR(1)

            --current max
                        SELECT  @maxAreaId = MAX(AreaId)
                        FROM    #resultset r
                                JOIN #WorkingArea_Version av ON av.VersionId = r.VersionId

            --potentials min
                        SELECT  @minAreaId = MIN(AreaId)
                        FROM    #WorkingArea_Version av
                        WHERE   av.VersionId = @VersionId                           

                        IF ( @minAreaId > @maxAreaId )
                            BEGIN
                                INSERT  INTO #resultset
                                        ( PocketGroupId, VersionId )
                                VALUES  ( @PocketGroupId, -- PocketGroupId - int
                                          @VersionId  -- VersionId - nvarchar(max)
                                          )
                            END     
                    END

                FETCH NEXT FROM db_cursor INTO @AreaVersionId, @AreaId,
                    @VersionId
            END
        CLOSE db_cursor   
        DEALLOCATE db_cursor

        DELETE  FROM #WorkingArea_Version
        WHERE   VersionId IN ( SELECT   r.VersionId
                               FROM     #resultset r )

        SELECT  @areaCount = COUNT(1)
        FROM    #WorkingArea_Version av
        SET @PocketGroupId = @PocketGroupId + 1
    END



    SELECT av.AreaVersionId, av.AreaId, av.VersionId, r.PocketGroupId
    FROM #Area_Version av
    JOIN #resultset r ON r.VersionId = av.VersionId
    ORDER BY av.AreaVersionId
于 2014-12-19T14:36:06.757 回答
0
IF OBJECT_ID('Tempdb..#Area_Version') IS NOT NULL 
    DROP TABLE #Area_Version
CREATE TABLE #Area_Version
    (
      AreaVersionId INT ,
      AreaId VARCHAR(1) ,
      VersionId VARCHAR(2) ,
      PocketGroupId INT
    )

INSERT  INTO #Area_Version
        ( AreaVersionId, AreaId, VersionId, PocketGroupId )
VALUES  ( 1, 'A', 'V5', 0 ),
        ( 2, 'B', 'V2', 0 ),
        ( 3, 'C', 'V2', 0 ),
        ( 4, 'D', 'V3', 0 ),
        ( 5, 'E', 'V2', 0 ),
        ( 6, 'F', 'V1', 0 ),
        ( 7, 'F', 'V4', 0 );
WITH    cte
          AS ( SELECT   AVLeft.AreaVersionId AS [LEFT] ,
                        AVLeft.AreaId ,
                        AVLeft.VersionId ,
                        AVLeft.PocketGroupId ,
                        COALESCE(MAX(AVRight.VersionId),
                                 ( SELECT   MIN(AV.VersionId)
                                   FROM     #Area_Version AS AV
                                 )) AS maxVersion ,
                        COALESCE(MIN(AVRight.VersionId),
                                 ( SELECT   MAX(AV.VersionId)
                                   FROM     #Area_Version AS AV
                                 )) AS minVersion
               FROM     #Area_Version AS AVLeft
                        LEFT JOIN #Area_Version AS AVRight ON AVLeft.AreaVersionId > AVRight.AreaVersionId
               GROUP BY AVLeft.AreaVersionId ,
                        AVLeft.AreaId ,
                        AVLeft.VersionId ,
                        AVLeft.PocketGroupId
             )
    SELECT  cte.[LEFT] ,
            cte.AreaId ,
            cte.VersionId ,
            CASE WHEN cte.VersionId > cte.minVersion
                      AND cte.VersionId < cte.maxVersion THEN 2
                 ELSE 1
            END AS PocketGroupId
    FROM    cte
于 2014-12-16T05:39:01.670 回答