Row_Number 其中行 = 1 不工作

Row_Number where row = 1 not working

我有这个 select 声明:

SELECT MODATA.CITEM, MODATA.QTREQ, MODATA.UNMSR, MODATA.CDESC, MODATA.USRSQ, SLQNTY.LLOCN, SLQNTY.LQNTY, ROW_NUMBER() OVER (PARTITION BY CITEM ORDER By CITEM) as tr
FROM MODATA
LEFT OUTER JOIN SLQNTY ON SLQNTY.ITNBR = MODATA.CITEM
WHERE MODATA.ORDNO = 'M123456' AND SLQNTY.LQNTY >= '0'
ORDER BY MODATA.USRSQ, MODATA.CITEM

哪个拉这个:

我只想显示 = 1 的 TR...我尝试将其添加到我的 Where 子句 TR = '1' 或 TR = 1...它说找不到该列?为什么会这样,我该如何解决?

您需要使用子查询:

SELECT MS.*
FROM (SELECT MODATA.CITEM, MODATA.QTREQ, MODATA.UNMSR, MODATA.CDESC, MODATA.USRSQ, SLQNTY.LLOCN, SLQNTY.LQNTY,
             ROW_NUMBER() OVER (PARTITION BY CITEM ORDER By CITEM) as tr
      FROM MODATA JOIN
           SLQNTY
           ON SLQNTY.ITNBR = MODATA.CITEM
      WHERE MODATA.ORDNO = 'M123456' AND SLQNTY.LQNTY >= 0
     ) MS
WHERE tr = 1
ORDER BY MODATA.USRSQ, MODATA.CITEM

您的 WHERE 子句将外连接变成了内连接,所以我只是通过使用正确的 join 类型使查询更清晰。我还删除了 '0' 中的单引号,因为我猜该列是一个数字。

必须执行如上所述的子查询...

SELECT * From(
SELECT MODATA.CITEM, MODATA.QTREQ, MODATA.UNMSR, MODATA.CDESC, MODATA.USRSQ, SLQNTY.LLOCN, SLQNTY.LQNTY, ROW_NUMBER() OVER (PARTITION BY CITEM ORDER By CITEM) as L
FROM MODATA
LEFT OUTER JOIN SLQNTY ON SLQNTY.ITNBR = MODATA.CITEM
WHERE MODATA.ORDNO = 'M325040' AND SLQNTY.LQNTY >= '0'
ORDER BY MODATA.USRSQ, MODATA.CITEM) AS X
WHERE X.L = 1