需要解决这个问题
这是针对REST API的,我需要对每一行进行计数,以便可以将其用于分页。
SELECT *
FROM (SELECT ROW_NUMBER()
OVER (
ORDER BY sc.LEGACY_PARTY_ID) AS rownum,
*
FROM (SELECT *,
ROW_NUMBER()
OVER (
ORDER BY sc.LEGACY_PARTY_ID) AS rownuminner
FROM (SELECT *
FROM (SELECT sc.legacy_party_id,
sc.practice,
sr.risk_level
FROM dbo.supplier sc
INNER JOIN dbo.risk sr
ON sc.LEGACY_PARTY_ID = sr.LEGACY_PARTY_ID)AS z) AS a)AS c
WHERE rownuminner <= ?)c
WHERE rownum > ?;
需要每行具有row_number的表元素,但会出现此错误:
Msg 4104,第16级,状态1,第21行
无法绑定多部分标识符“ sc.LEGACY_PARTY_ID”。Msg 4104,第16级,状态1,第20行
无法绑定多部分标识符“ sc.LEGACY_PARTY_ID”。
答案 0 :(得分:4)
您正试图通过子查询中的别名引用对象。您需要使用正确的别名来引用它:
SELECT *
FROM (SELECT ROW_NUMBER() OVER (ORDER BY c.LEGACY_PARTY_ID) AS rownum,
*
FROM (SELECT *,
ROW_NUMBER() OVER (ORDER BY a.LEGACY_PARTY_ID) AS rownuminner
FROM (SELECT *
FROM (SELECT sc.legacy_party_id,
si.supplier_name,
si.supplier_description,
sc.practice,
sc.category,
sc.subcategory,
sui.industry,
sr.risk_level
FROM mip.supplier_classification AS sc
INNER JOIN mip.supplier_info AS si ON si.legacy_party_id = sc.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_INDUSTRY AS sui ON sc.LEGACY_PARTY_ID = sui.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_RISK AS sr ON sc.LEGACY_PARTY_ID = sr.LEGACY_PARTY_ID) AS z ) AS a ) AS c
WHERE rownuminner <= 10) AS c
WHERE rownum > 5;
这似乎仍然太复杂了。看起来,它可能写得更简单:
WITH CTE AS
(SELECT sc.legacy_party_id,
si.supplier_name,
si.supplier_description,
sc.practice,
sc.category,
sc.subcategory,
sui.industry,
sr.risk_level,
ROW_NUMBER() OVER (ORDER BY sc.legacy_party_id) AS RN
FROM mip.supplier_classification AS sc
INNER JOIN mip.supplier_info AS si ON si.legacy_party_id = sc.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_INDUSTRY AS sui ON sc.LEGACY_PARTY_ID = sui.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_RISK AS sr ON sc.LEGACY_PARTY_ID = sr.LEGACY_PARTY_ID)
SELECT *
FROM CTE
WHERE RN <= ?
AND RN > ?;
答案 1 :(得分:0)
SELECT *
FROM (SELECT ROW_NUMBER()
OVER (
ORDER BY c.LEGACY_PARTY_ID) AS rownum,
*
FROM (SELECT *,
ROW_NUMBER()
OVER (
ORDER BY c.LEGACY_PARTY_ID) AS rownuminner
FROM (SELECT *
FROM (SELECT sc.legacy_party_id,
si.supplier_name,
si.supplier_description,
sc.practice,
sc.category,
sc.subcategory,
sui.industry,
sr.risk_level
FROM mip.supplier_classification sc
INNER JOIN mip.supplier_info si
ON si.legacy_party_id = sc.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_INDUSTRY sui
ON sc.LEGACY_PARTY_ID = sui.LEGACY_PARTY_ID
INNER JOIN mip.SUPPLIER_RISK sr
ON sc.LEGACY_PARTY_ID = sr.LEGACY_PARTY_ID)AS z) AS a)AS c
WHERE rownuminner <= ?)c
WHERE rownum > ?;
您已经为外部查询和使用sc定义了c别名,所以它给出了错误