如何在DB2 SQL中使用FETCH FIRST (n) ROWS ONLY
返回变量(n)行数?这适用于DB2 / 400 v7r3。
documentation for FETCH
明确表示无法做到,但我无法想象另一种方式......
... fetch-row-count不能包含标量全查询,列引用,表引用,用户定义的函数引用或内置标量函数......
...我认为它必须是一个常数,如"10"
,并且不能是变量或列名称,不幸的是,这正是我想要做的。
这有效:
SELECT PREFILTER.*
FROM PREFILTER
INNER JOIN GT1 ON FILTERED.GTAMT=GT1.LOSTAMT
ORDER BY GTDATE DESC
FETCH FIRST 10 ROWS ONLY
这不起作用:
SELECT PREFILTER.*
FROM PREFILTER
INNER JOIN GT1 ON PREFILTER.GTAMT=GT1.LOSTAMT
ORDER BY GTDATE DESC
FETCH FIRST (GT1.LOSTAMT) ROWS ONLY <=== changed here
收到以下错误:
SQL Error [428H7]: [SQ20467] Expression containing LOSTAMT must calculate a constant value.
此查询旨在查找在较大的表中孤立或键入错误的金融交易记录,这些记录具有相当松散的规范化和参照完整性(或缺乏)。
PREFILTER
是一个返回主表子集的查询,GT1
是另一个查询,它计算这些记录的较小和更复杂的子集。然后JOIN
它们,并按照(n)
列的指定,按降序日期顺序返回LOSTQTY
行。所以它应该只返回(n)最近的记录。
请注意,我确实认识到我的FETCH
的位置是不正确的,并且它(或者它变形的任何东西)可能不得不进入CTE查询之一,例如GT1
。
此外,在这个早期阶段,我似乎得到了一些笛卡尔结果,但一旦FETCH
问题得到解决,这可能会得到解决。
作为参考,这是项目的完整SQL:
WITH --SET THE INITIAL ACCOUNT & DATE RANGE
PREFILTER AS ( SELECT *
FROM GLTRANT
WHERE GTDATE > 20170000
AND GTACCT=112068
),
--CREATE LIST OF ALL POSITIVE VALUES
POSVALS AS ( SELECT GTAMT AS POSAMT, COUNT(GTAMT) AS POSC
FROM PREFILTER
WHERE GTAMT > 0
GROUP BY GTAMT
),
--CREATE LIST OF ALL NEGATIVE VALUES, WITH SIGN DROPPED
NEGVALS AS ( SELECT ABS(GTAMT) AS NEGAMT, COUNT(GTAMT) AS NEGC
FROM PREFILTER
WHERE GTAMT < 0
GROUP BY ABS(GTAMT)
),
--CALCULATE DISCREPANCIES BETWEEN THE TWO LISTS. SUBTRACT THE TWO AND MULTIPLY THE SIGN BY THE ABSOLUTE VALUE
--OF THE DIFFERENCE. THEN TO RESTORE THE SIGN, MULTIPLY THE AMOUNT BY THE SIGN OF THE DIFFERENCE.
FOJ AS ( SELECT SIGN(COALESCE(POSC,0)-COALESCE(NEGC,0))*COALESCE(POSAMT,NEGAMT) AS LOSTAMT,
ABS (COALESCE(POSC,0)-COALESCE(NEGC,0)) AS LOSTQTY
FROM POSVALS
FULL OUTER JOIN NEGVALS ON POSAMT=NEGAMT
WHERE COALESCE(POSC,0)-COALESCE(NEGC,0) <> 0
),
--GET DISCREPANCIES WITH COUNT >1
GT1 AS ( SELECT *
FROM FOJ
WHERE LOSTQTY>1
)
--SEARCH PREFILTER FOR EACH AMOUNT (LOSTAMT) IN GT1 AND RETURN THE MOST RECENT (LOSTQTY) RECORDS
SELECT PREFILTER.*
FROM PREFILTER
INNER JOIN GT1 ON PREFILTER.GTAMT=GT1.LOSTAMT
ORDER BY GTDATE DESC
FETCH FIRST (GT1.LOSTQTY) ROWS ONLY --DOES NOT WORK
你应该可以使用window function ROW_NUMBER
:
WITH --SET THE INITIAL ACCOUNT & DATE RANGE
PREFILTER AS (
SELECT T.*,
ROW_NUMBER() OVER (ORDER BY T.GTDATE DESC) AS RN
FROM (
SELECT *
FROM GLTRANT
WHERE GTDATE > 20170000
AND GTACCT=112068
) T
),
...
--SEARCH PREFILTER FOR EACH AMOUNT (LOSTAMT) IN GT1 AND RETURN THE MOST RECENT (LOSTQTY) RECORDS
SELECT PREFILTER.*
FROM PREFILTER
INNER JOIN GT1 ON PREFILTER.GTAMT=GT1.LOSTAMT
WHERE RN <= GT1.LOSTQTY
ORDER BY GTDATE DESC
它按照PREFILTER
的降序为GTDATE
中的每一行分配连续的数字,稍后您可以使用它来限制结果集。
我不确定你能在一份声明中做你想做的事。但是,您应该能够将可以执行此操作的存储过程或SQL函数放在一起。在存储过程中,将查询放在一起,然后用光标打开它。然后,游标可以获取所需的行数,将其放入结果集中,并从过程中返回该结果集。来自IBM的当前文档就是:https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/sqlp/rbafyresultsets.htm
好的,在吸收了Mustaccio之前的回答并且整天敲打IBM文档后,我能够解决这个问题。非常感谢Mustaccio指出我正确的方向。
两个重要的部分:ROW_NUMBER()的位置必须进一步向下移动;它不适用于它的位置。此外,它需要添加PARTITION BY
参数,以便编号重新开始每个新的值组。所以它就像1-2-1-2-1-2-3而不是1-2-3-4-5-6-7。这对于WHERE RN <= LOSTQTY
的工作至关重要。
这是完整的结果:
WITH --SET THE INITIAL ACCOUNT & DATE RANGE
PREFILTER AS ( SELECT GTCOMP, GTACCT, GTDATE, GTSRCE, GTREF#, GTENT#, GTAMT, GTDESC, "GTPO#",
"GTCHK#", "GTINV#", GTCKAC, GT1099, GTXXX1, GTAFLG, GTVEND, "GTBAT#"
FROM F_CERTOB.GLTRANT
WHERE GTDATE > 20180000
AND GTACCT=112068
),
--CREATE LIST OF ALL POSITIVE VALUES
POSVALS AS ( SELECT GTAMT AS POSAMT, COUNT(GTAMT) AS POSC
FROM PREFILTER
WHERE GTAMT > 0
GROUP BY GTAMT
),
--CREATE LIST OF ALL NEGATIVE VALUES, WITH SIGN DROPPED
NEGVALS AS ( SELECT ABS(GTAMT) AS NEGAMT, COUNT(GTAMT) AS NEGC
FROM PREFILTER
WHERE GTAMT < 0
GROUP BY ABS(GTAMT)
),
--CALCULATE DISCREPANCIES BETWEEN THE TWO LISTS. SUBTRACT THE "DE-SIGNED" NEGATIVE FROM THE POSITIVE AND
--MULTIPLY THE SIGN BY THE ABSOLUTE VALUE OF THE DIFFERENCE. THEN TO RESTORE THE SIGN, MULTIPLY THE AMOUNT
--BY THE SIGN OF THE DIFFERENCE. THIS IS A FULL OUTER JOIN, SO NULLS ARE A GIVEN, AND COALESCE() IS USED
--TO FILL IN THE HOLES.
FOJ AS ( SELECT SIGN(COALESCE(POSC,0)-COALESCE(NEGC,0))*COALESCE(POSAMT,NEGAMT) AS LOSTAMT,
ABS (COALESCE(POSC,0)-COALESCE(NEGC,0)) AS LOSTQTY
FROM POSVALS
FULL OUTER JOIN NEGVALS ON POSAMT=NEGAMT
WHERE COALESCE(POSC,0)-COALESCE(NEGC,0) <> 0
),
--THIS IS AN EXTRA KNOB TO CONTROL THE NUMBER OF RESULTS DURING DEVELOPMENT. IF SET TO >1, IT WILL SHOW
--ONLY THE DATA THAT WOULD RETURN TWO OR MORE ROWS. USEFUL WHEN 99% OF THE DATA WOULD BE ONE ROW. THIS
--WAS NEEDED TO DEVELEOP & TEST "ROW_NUMBER() OVER (PARTITION BY GTAMT ORDER BY GTDATE DESC) AS RN" AND
--IF THE NUMBERING WAS RESTARTING PROPERLY FOR EACH GROUP OF VALUES.
GT AS ( SELECT *
FROM FOJ
WHERE LOSTQTY>0
),
--RETRIEVE THE ITEMS, RANK THEM BY AMT & DATE. USE PARTITON-BY TO RESTART ROW NUMBERING FOR EACH GROUP
--OF AMOUNTS. USE ORDER-BY TO NUMBER BY DATE IN DESCENDING ORDER.
GROUPED AS ( SELECT PREFILTER.*, LOSTQTY,
ROW_NUMBER() OVER (PARTITION BY GTAMT ORDER BY GTDATE DESC) AS RN
FROM PREFILTER
INNER JOIN GT ON GTAMT=LOSTAMT
),
--NARROW IT DOWN TO ONLY THE TOP (n) ITEMS
RECENT AS ( SELECT *
FROM GROUPED
WHERE RN <= LOSTQTY
)
SELECT *
FROM RECENT
ORDER BY GTAMT
参考页面确实表示您不能像您尝试的那样使用列引用作为要返回的行数的表达式。
它允许的是表达。有关于它所说的不允许的规则,但这些规则并不排除使用主机变量。
FETCH FIRST :xqty ROWS ONLY