strong>顶部子句从偏移量中获取行并获取变量。
我有一个需要导入的表,但是数据的权重太大,无法在一次操作中导入。因此,我决定将表分成4个部分,并在并行程序中导入这4个部分。每次导入时,行数都会更改。
所以我创建了一个变量,它是总行数的1/4,我需要得到正确的行数过滤器:
declare @row_to_get int
set @row_to_get = (select
*
from
[AAA].[tablename]
where
datepart(ss, [tempo]) = 0
and
datepart(mi, [tempo]) % 3 = 0
and
DateKey > @startDateKey_Extended)
/4
然后我试着这样做:
select
[Id]
,[Datekey]
,[tempo]
,max(coalesce([othercolumn], 1)) as [othercolumn]
from
[AAA].[tablename]
where
datepart(ss, [tempo]) = 0
and datepart(mi, [tempo]) % 3 = 0
and DateKey > @startDateKey_Extended
group by
[Id]
,[Datekey]
,[tempo]
OFFSET 0 ROWS FETCH NEXT @row_to_get ROWS ONLY
,第二次分割:
select
[Id]
,[Datekey]
,[tempo]
,max(coalesce([othercolumn], 1)) as [othercolumn]
from
[AAA].[tablename]
where
datepart(ss, [tempo]) = 0
and datepart(mi, [tempo]) % 3 = 0
and DateKey > @startDateKey_Extended
group by
[Id]
,[Datekey]
,[tempo]
OFFSET @row_to_get ROWS FETCH NEXT @row_to_get*2 ROWS ONLY
等等
但是我得到了这个错误:Incorrect syntax near 'OFFSET'
我怎样才能用更聪明的方式重写这个sintax ?它是强制性的,链接到一个参数:我需要的行数,而不是表中的所有行我用
来保持组目前OFFSET/FETCHAzure Synapse不支持子句。
但是,有一些变通方法。
使用<<ol>例子:
Declare @fetchrows int = 2;
Declare @offsetposition int = 2;
SELECT *
FROM (
SELECT TOP (@fetchrows) *
FROM
(SELECT TOP(0+@fetchrows) *
FROM dbo.sampledata
ORDER BY id ASC
) AS a
ORDER BY id DESC
) AS b
ORDER BY id ASC
SELECT *
FROM (
SELECT TOP (@fetchrows) *
FROM
(SELECT TOP((@offsetposition*1)+@fetchrows) *
FROM dbo.sampledata
ORDER BY id ASC
) AS a
ORDER BY id DESC
) AS b
ORDER BY id ASC
- ROW_NUMBER窗口函数获取row_number在offset和fetch变量之间的行。
参考:分页结果在Synapse SQL