我有这段代码可以找到两个数字之间的差距:
;WITH
cte AS (
SELECT
BSEG_BELNR,
RowNum = ROW_NUMBER() OVER (ORDER BY BSEG_BELNR)
FROM dbo.QLIK_JOURNAL GROUP BY BSEG_BELNR),
cte2 AS (
SELECT *, DENSE_RANK() OVER (ORDER BY BSEG_BELNR - RowNum) AS Series
FROM cte),
cte3 AS (
SELECT *, COUNT(*) OVER (PARTITION BY Series) AS SCount
FROM cte2),
cte4 AS (
SELECT
MinID = MIN(BSEG_BELNR),
MaxID = MAX(BSEG_BELNR),
Series
FROM cte3
GROUP BY Series)
SELECT a.MaxID,b.MinID
FROM cte4 a
INNER JOIN cte4 b
ON a.Series+1 = b.Series
ORDER BY a.MaxID
并且此代码提供了2列,但我需要打印不同的我意味着差距不是开始和结束。
MaxID MinID
-----------------------
0000015010 0000015012
0000015018 0000015020
0000015020 0000015022
0000015041 0000015043
0000015062 0000015065
........ ........
我希望在
之间打印数字 Gap
---------
0000015011
0000015019
0000015021
0000015042
0000015063
0000015064
我在SQL SERVER中如何做到这一点
答案 0 :(得分:1)
您可以使用CTE并转换为int
:
;WITH cte AS (
SELECT MIN(CAST(MaxID as int)) as Mi,
MAX(CAST(MinID as int)) as Ma
FROM YourResultSet c
UNION ALL
SELECT Mi + 1,
Ma
FROM cte
WHERE Mi < Ma
)
SELECT STUFF(c.Mi,1,0,'00000')
FROM cte c
INNER JOIN YourResultSet y
ON c.Mi > CAST(MaxID as int) and c.Mi < CAST(MinID as int)
输出:
0000015011
0000015019
0000015021
0000015042
0000015063
0000015064
答案 1 :(得分:0)
我想要返回MaxID和MinID列之间的值之间的差距,请尝试:
why the data not divided into same 4 block of 50MB in order to save them each one of them in 64MB of block size.