使用循环更新临时表

时间:2014-08-04 19:44:04

标签: sql sql-server loops sql-server-2012

我正在尝试创建一种自动方式来查找某些数据点的Min-Max范围。问题在于,当按顺序排列HUB数据点(SLIC)时,许多HUB SLIC流被其他HUB的SLIC中断。这提供了一个问题,因为许多HUB的范围重叠。因此,我需要停止HUB名称1的范围,为HUB名称2启动第二个范围,然后再次为HUB 1启动另一个新范围。以视觉形式出现:

HUB 1: 0101-1490
HUB 2: 1500-1501
HUB 1: 1510-1513

等,

+-------------------+------+
|       HUB         | SLIC |
+-------------------+------+
| MEADOWLANDS_NJ_D  | 1471 |
| MEADOWLANDS_NJ_D  | 1480 |
| MEADOWLANDS_NJ_D  | 1481 |
| MEADOWLANDS_NJ_D  | 1482 |
| MEADOWLANDS_NJ_D  | 1490 |
| WILLOW GROVE_PA_D | 1500 |
| WILLOW GROVE_PA_D | 1501 |
| MEADOWLANDS_NJ_D  | 1510 |
| MEADOWLANDS_NJ_D  | 1512 |
| MEADOWLANDS_NJ_D  | 1513 |
| WILLOW GROVE_PA_D | 1520 |
| MEADOWLANDS_NJ_D  | 1522 |
+-------------------+------+

我之前从未尝试过循环,所以我一直在尝试,但我相信我陷入了无限循环。我正在尝试创建两个临时表,第一个包含原始数据,用于插入和更新到第二个表中的HUB名称以及该范围的MIN和MAX SLIC。

我希望结果如此:

+-------------------+------+------+
|        HUB        | MIN  | MAX  |
+-------------------+------+------+
| MEADOWLANDS_NJ_D  | 0101 | 1490 |
| WILLOW GROVE_PA_D | 1500 | 1501 |
| MEADOWLANDS_NJ_D  | 1510 | 1513 |
| WILLOW GROVE_PA_D | 1520 | 1520 |
| MEADOWLANDS_NJ_D  | 1522 | 1522 |
| WILLOW GROVE_PA_D | 1530 | 1540 |
+-------------------+------+------+

我的查询:

DROP TABLE SLIC5
CREATE TABLE SLIC5
(UPS_HUB nvarchar (255) NOT NULL,
SLIC nvarchar (255) NOT NULL,
PK_SLIC int NOT NULL)

INSERT INTO SLIC5
    SELECT TOP (50) UPS_HUB, SLIC, RANK () OVER (ORDER BY SLIC) PK_SLIC
    FROM SLIC

ALTER TABLE SLIC5
ADD PRIMARY KEY (PK_SLIC)

DROP TABLE #tempRAW
CREATE TABLE #tempRAW
(UPS_HUB varchar(50), SLIC nvarchar(10))

INSERT INTO #tempRAW (UPS_HUB, SLIC)
    SELECT UPS_HUB, SLIC
    FROM dbo.SLIC5
    ORDER BY PK_SLIC ASC

DROP TABLE #RANGE_TABLE
CREATE TABLE #RANGE_TABLE
(UPS_HUB varchar (50), [MIN] nvarchar (10), [MAX] nvarchar (10))

INSERT INTO #RANGE_TABLE (UPS_HUB,[MIN])
SELECT TOP (1) UPS_HUB, SLIC
FROM #tempRAW

DECLARE @currentHUB varchar, @currentMinSLIC nvarchar, @tempTableRecCount int
SELECT @currentHUB=UPS_HUB, @currentMinSLIC=[MIN] from #RANGE_TABLE
SELECT @tempTableRecCount= (SELECT count(*) from #tempRAW)

WHILE (@tempTableRecCount > 0)
BEGIN
DECLARE @loopingSLIC nvarchar, @loopingHUB varchar
SELECT @loopingSLIC=SLIC, @loopingHUB=UPS_HUB from #tempRAW
    IF (@currentHub=@loopingHUB)
        BEGIN
            IF (@currentMinSLIC < @loopingSLIC)
                BEGIN
                    UPDATE #RANGE_TABLE
                    SET [MAX]=@loopingSLIC
                    WHERE #RANGE_TABLE.UPS_HUB=@currentHUB
                    END
            END
    IF (@currentHUB <> @loopingHUB)
        BEGIN
            INSERT INTO #RANGE_TABLE (UPS_HUB,[MIN])
                VALUES (@loopingHUB,@currentMinSLIC)
    END

DELETE FROM #tempRAW
WHERE UPS_HUB=@loopingHUB and SLIC=@loopingSLIC

SELECT @currentHUB=UPS_HUB from #tempRAW
SELECT @currentMinSLIC= SLIC from #tempRAW

SELECT @tempTableRecCount = count (*) from #tempRAW
END

SELECT * FROM #RANGE_TABLE
SELECT * FROM #tempRAW

1 个答案:

答案 0 :(得分:0)

create table #demo (hub nvarchar(100), slic int)

insert #demo  values 
('MEADOWLANDS_NJ_D',1471),
('MEADOWLANDS_NJ_D',1480),
('MEADOWLANDS_NJ_D',1481),
('MEADOWLANDS_NJ_D',1482),
('MEADOWLANDS_NJ_D',1490),
('WILLOW GROVE_PA_D',1500),
('WILLOW GROVE_PA_D',1501),
('MEADOWLANDS_NJ_D',1510),
('MEADOWLANDS_NJ_D',1512),
('MEADOWLANDS_NJ_D',1513),
('WILLOW GROVE_PA_D',1520),
('MEADOWLANDS_NJ_D',1522)

编辑:

    WITH CTE AS (
    SELECT hub
        , slic
        , ROW_NUMBER() OVER (
            ORDER BY slic
            ) - RANK() OVER (
            PARTITION BY hub ORDER BY slic
            ) diff
    FROM #demo
    )
SELECT hub
    , MIN(slic) min_slic
    , MAX(slic) max_slic
FROM cte
GROUP BY hub
    , diff
ORDER BY MIN(slic)

结果:

hub min_slic    max_slic
MEADOWLANDS_NJ_D    1471    1490
WILLOW GROVE_PA_D   1500    1501
MEADOWLANDS_NJ_D    1510    1513
WILLOW GROVE_PA_D   1520    1520
MEADOWLANDS_NJ_D    1522    1522