sql server partition by using the partition function does not solve the problem of continuous digital access

sql server in a column of data table is not necessarily consecutive numbers, but in a continuous demand requirements to segment digital display, such as: 1,2,3,4,5,6,10,11,12,13,

Such displays will require: 1 to 6,10 to 13. Here's how.

Man of few words said, directly on the instance.

--创建测试表
CREATE TABLE PartitionTest
(
ID INT IDENTITY(1,1) PRIMARY KEY,
KeyID INT ,--标识ID
Num INT --号码
)
--插入数据,KeyID不同
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8000 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8001 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8002 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8003 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8004 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8005 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8006 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 1, 8007 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9000 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9001 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9002 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9003 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9004 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9005 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9006 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 2, 9007 )

- KeyID under different circumstances, it is easy to find out such data
SELECT KeyID, CONVERT (VARCHAR (10 ), MIN (Num)) + '~' + CONVERT (VARCHAR (10), MAX (Num)) AS PartNum FROM dbo.PartitionTest WHERE KeyID IN ( 1,2) GROUP BY KeyID

 

 

--插入数据,KeyID相同
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1000 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1001 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1002 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1003 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1004 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1005 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1006 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 1007 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4000 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4001 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4002 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4003 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4004 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4005 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4006 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 4007 )
INSERT INTO dbo.PartitionTest(KeyID, Num ) VALUES ( 3, 5007 )

Under the same --KeyID case, the above query can not find out the needs of the desired data
SELECT KeyID, CONVERT (VARCHAR (10 ), MIN (Num)) + '~' + CONVERT (VARCHAR (10), MAX ( Num)) AS PartNum FROM dbo.PartitionTest WHERE KeyID IN (3) GROUP BY KeyID

 

 

- can be isolated using Partition BY each period of continuous packet number may be utilized GroupNum grouping
the SELECT
the KeyID,
the Num,
the ROW_NUMBER () over (the KeyID the ORDER BY Partition BY NUM) newNum,
the Num-the ROW_NUMBER () over (the KeyID the ORDER Partition BY NUM BY) GroupNum
the FROM dbo.PartitionTest the WHERE the KeyID the IN (. 3)

 

 

- write, you can achieve the requirements demand
the SELECT t.KeyID,
the CASE the WHEN COUNT (newNum)> 1THEN the CONVERT (VARCHAR (10), MIN (t.Num)) + '~' + the CONVERT (VARCHAR (10), MAX (t.Num)) the ELSE the CONVERT (VARCHAR (10), MIN (t.Num)) the END PartNum could
the FROM (
the SELECT
the KeyID,
the Num,
the Num-the ROW_NUMBER () over (the Partition the KeyID the ORDER BY BY NUM) newNum
the FROM dbo.PartitionTest the KeyID the IN the WHERE (. 3)
) T t.KeyID the GROUP BY, t.NewNum

 

 

 

 

 

The above code can execute it directly to the database to test Oh, like the point of a praise it! ! !

 

Guess you like

Origin www.cnblogs.com/nimayax/p/11586591.html