您好我使用以下查询获取前1行时遇到问题。 它在oracle中,plsql。
SELECT per.person_no,
perbus.person_no,
perbus.edit_dt,
perbus.org_name,
perbus.FIRST_NAME
FROM users.persons per
LEFT OUTER JOIN (
SELECT ASSOC.edit_dt,PER_CONTACTS.PERSON_NO,
PER_CONTACTS.PERSON_ID AS PER_ID,
PER.EXTERNAL_PERSON_ID AS EXT_PER_ID,
PER_CONTACTS.LNAME||', '||PER_CONTACTS.FNAME AS NAME,
PER_CONTACTS.FNAME AS FIRST_NAME,
PER_CONTACTS.MNAME AS MIDDLE_NAME,
PER_CONTACTS.LNAME AS LAST_NAME,
ORG.ORG_NAME AS ORG_NAME,
T_ORG.ORG_ID AS ORG_ID,
T_ORG.ORG_DISPLAY_ID AS EXT_ORG_ID,
rownum AS rn
FROM USERS.PER_CONTACTS PER_CONTACTS
LEFT OUTER JOIN USERS.PERSONS PER ON PER.PERSON_ID = PER_CONTACTS.PERSON_ID
LEFT OUTER JOIN USERS.ASSOC ASSOC ON ASSOC.PERSON_ID = PER.PERSON_ID
LEFT OUTER JOIN USERS.T_ORG T_ORG ON T_ORG.T_ORG_ID = ASSOC.T_ORG_ID
LEFT OUTER JOIN USERS.ORG ORG ON ORG.ORG_ID = T_ORG.ORG_ID
WHERE PER_CONTACTS.CONTACT_ROLE_LOV = 'EMPLOYEE' AND PER_CONTACTS.PERSON_NO IN (SELECT PERSON_NO FROM USERS.QA_APPROVED)
ORDER BY ASSOC.edit_dt DESC
)
perbus ON perbus.PERSON_NO = PER.PERSON_NO
WHERE PER.PERSON_NO IN (SELECT PERSON_NO FROM USERS.QA_APPROVED)
我是oracle的新手,在上面的查询中我因为数据而得到重复 - 我需要根据 Assoc.edit_dt 获得第一条记录。
我可以通过Assoc.edit_dt订购记录,但是当我在Join 中使用 row_num = 1 之类的子句时,我的输出查询为null对于所有perbus表字段。
请告知。
答案 0 :(得分:0)
试试这个
如果您想通过PERSON_NO返回TOP 1行,那么
替换
rownum as rn
与
ROW_NUMBER() OVER (PARTITION BY PER_CONTACTS.PERSON_NO
ORDER BY ASSOC.edit_dt DESC) RN
如果你想要返回TOP 1整体,那么
替换
rownum as rn
与
ROW_NUMBER() OVER (ORDER BY ASSOC.edit_dt DESC) RN
最后在你的外部where子句中,只做RN = 1来挑选前1行
WHERE PER.PERSON_NO IN (select PERSON_NO from USERS.QA_APPROVED) AND RN = 1
答案 1 :(得分:0)
您可以使用结果偏移并获取第一个子句(仅适用于12c)
只需添加
FETCH FIRST ROW ONLY;
在order by子句之后
了解更多信息。 read this
希望这有助于=)
编辑:
SELECT per.person_no,
perbus.person_no,
perbus.edit_dt,
perbus.org_name,
perbus.FIRST_NAME
FROM users.persons per
LEFT OUTER JOIN (
SELECT ASSOC.edit_dt AS EDIT_DT,PER_CONTACTS.PERSON_NO,
PER_CONTACTS.PERSON_ID AS PER_ID,
PER.EXTERNAL_PERSON_ID AS EXT_PER_ID,
PER_CONTACTS.LNAME||', '||PER_CONTACTS.FNAME AS NAME,
PER_CONTACTS.FNAME AS FIRST_NAME,
PER_CONTACTS.MNAME AS MIDDLE_NAME,
PER_CONTACTS.LNAME AS LAST_NAME,
ORG.ORG_NAME AS ORG_NAME,
T_ORG.ORG_ID AS ORG_ID,
T_ORG.ORG_DISPLAY_ID AS EXT_ORG_ID,
rownum AS rn
FROM USERS.PER_CONTACTS PER_CONTACTS
LEFT OUTER JOIN USERS.PERSONS PER ON PER.PERSON_ID = PER_CONTACTS.PERSON_ID
LEFT OUTER JOIN USERS.ASSOC ASSOC ON ASSOC.PERSON_ID = PER.PERSON_ID
LEFT OUTER JOIN USERS.T_ORG T_ORG ON T_ORG.T_ORG_ID = ASSOC.T_ORG_ID
LEFT OUTER JOIN USERS.ORG ORG ON ORG.ORG_ID = T_ORG.ORG_ID
WHERE PER_CONTACTS.CONTACT_ROLE_LOV = 'EMPLOYEE' AND PER_CONTACTS.PERSON_NO IN (SELECT PERSON_NO FROM USERS.QA_APPROVED)
--ORDER BY ASSOC.edit_dt DESC
)
perbus ON perbus.PERSON_NO = PER.PERSON_NO
WHERE PER.PERSON_NO IN (SELECT PERSON_NO FROM USERS.QA_APPROVED)
ORDER BY perbus.edit_dt DESC
FETCH FIRST ROW ONLY;
仅供参考,您为所有列添加了别名,除了一个,这是 assoc.edit_dt ,但是您在主查询中调用它就好像它有一个别名。所以我把它带到了添加 assoc.edit_dt
的别名
答案 2 :(得分:0)
var x = '<div><span></span><div id="container"></div></div>';
console.log($(x).find('#container')[0].outerHTML);
答案 3 :(得分:0)
仅仅提取&#34;少数&#34;来自数据集的记录,您可以使用&#34; rownum&#34; - 这是一个例子:
subclasses
另一个例子(根据&#34; table_name选择前两行):
BaseEntity
注意:请勿使用相同( = )符号,必须使用(&lt; )。