Row_Number,其中row = 1不起作用

时间:2017-10-30 19:46:26

标签: sql row-number

我有这个选择声明:

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

哪个拉这个: enter image description here

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

2 个答案:

答案 0 :(得分: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'中的单引号,因为我猜这个列是一个数字。

答案 1 :(得分: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