目标:根据字段'DOCURL'和'ELEMENT'获取不同的值。
注意:(编辑)以下内容将在.aspx页面上使用,而不是在SQL中使用
问题:如果我作为独立查询运行,我会获得14行,这是正确的;
Select DISTINCT DOCURL, ELEMENT From TblReference Where Property= 'XYZ'
我的代码拉高了34,我将Row_Number更改为DENSE_RANK,但仍然无法降至14;
Select * from( Select DISTINCT DOCURL, ELEMENT,
DENSE_RANK() over (order by id desc) As rn From TblReference
Where Property= 'XYZ' ) as t Where rn = 1
*** rn处于变量循环中,通常可以计数到10。
参考:sql query distinct with Row_Number -
SELECT distinct id, DENSE_RANK() OVER (ORDER BY id) AS RowNum
FROM table
WHERE fid = 64
答案 0 :(得分:1)
由于MySQL不支持ROW_NUMBER()
和DENSE_RANK()
等窗口函数,请尝试使用连接:
Select t.DOCURL, t.ELEMENT
From TblReference t
LEFT JOIN TblReference s
ON(t.docurl = s.docurl and t.element = s.element and s.id < t.id and s.property = 'XYZ')
Where t.Property= 'XYZ' AND s.id is null
ORDER BY t.OrderColumn
LIMIT 10;
我不知道你如何使用这个功能,但你的问题是你没有使用PARTITION BY
部分:
Select *
from(Select DISTINCT DOCURL, ELEMENT,
ROW_NUMBER() over (PARTITION BY docurl,element order by id desc) As rn
From TblReference
Where Property= 'XYZ' ) t
Where t.rn = 1