此查询给出错误
ORA-00907:缺少右括号
我找不到任何括号问题
ngOnInit() {
this.fetchArchiveForGroupChat(jid, connection);
}
fetchArchiveForGroupChat(jid, connection) {
let maxNumberOfResults = "35";
if (this.lastMessageUID !== -1 && !this.archiveMessagesCompleted) {
console.log('Fetching second Archive...');
this.nextSetOfArchivedMessages = []; // // Clear array on each request
let iq = $iq({
type: "set",
id: generateUUIDv4(),
})
.c("query", {
xmlns: this.xmppNamespacesService.mamNS,
queryid: connection.getUniqueId("group-history")
})
.c("x", {
xmlns: this.xmppNamespacesService.dataFormsNS,
type: "submit"
})
.c("field", {
var: "FORM_TYPE",
type: "hidden"
})
.c("value").t(this.xmppNamespacesService.mamNS)
.up().up()
.c("field", {
var: "with"
})
.c("value").t(jid)
.up().up().up() //to reach query
.c("set", {
xmlns: this.xmppNamespacesService.resultSetManagementNS
})
.c("max").t(maxNumberOfResults)
.up()
.c("before").t(this.lastMessageUID); //to retrieve the last n messages
console.log("IQ to retrieve archive:", iq.tree());
// Sending the IQ stanza to the server
connection.sendIQ(iq, this.onReceivingArchive.bind(this),
(e) => {
console.log("An error has occurred while retrieving archive");
console.log(e);
}
);
} else if (!this.archiveMessagesCompleted) {
console.log("Fetching 1rst Archive");
// Clear array on each request
this.archiveList = [];
this.nextSetOfArchivedMessages = [];
let iq = $iq({
type: "set",
id: generateUUIDv4(),
})
.c("query", {
xmlns: this.xmppNamespacesService.mamNS,
queryid: connection.getUniqueId("group-history")
})
.c("x", {
xmlns: this.xmppNamespacesService.dataFormsNS,
type: "submit"
})
.c("field", {
var: "FORM_TYPE",
type: "hidden"
})
.c("value").t(this.xmppNamespacesService.mamNS)
.up().up()
.c("field", {
var: "with"
})
.c("value").t(jid)
.up().up().up() //to reach query
.c("set", {
xmlns: this.xmppNamespacesService.resultSetManagementNS
})
.c("max").t(maxNumberOfResults)
.up()
.c("before")//.t(this.lastMessageUID); // To retrieve the last n messages // To retrieve the last n messages
console.log("IQ to retrieve archive:", iq.tree());
// Sending the IQ stanza to the server
connection.sendIQ(iq, this.onReceivingArchive.bind(this),
(e) => {
console.log("An error has occurred while retrieving archive");
console.log(e);
}
);
}
答案 0 :(得分:4)
您的问题是这个
ORDER BY id DESC
您不能在充当列表达式的子查询中使用ORDER BY
。如果将其删除,您面临的错误将消失。
但是,我希望您使用with
子句重写查询,因为两个子查询表达式都提取同一行。
WITH auth AS ( SELECT *
FROM ( SELECT pre_desig_id AS perpared_by,
pre_last_date AS pre_end_dt
FROM authorization
WHERE project_id = 5
AND pre_desig_id = 48 ORDER BY id DESC )
WHERE ROWNUM = 1 )
SELECT a.perpared_by,a.pre_end_dt
FROM authorization au
LEFT JOIN project p ON au.project_id = p.project_id
LEFT JOIN designation d ON au.au_desig_id = d.desigid
CROSS JOIN auth a;
答案 1 :(得分:1)
您需要转换派生列的(prepared_by
)子查询,类似于下面的pre_end_dt
的子查询
Select
(Select pre_desig_id
From (Select pre_desig_id,
row_number() over (order by ID desc) as rn
From Authorization
Where project_id = 5
and pre_desig_id = 48
)
Where rn = 1) as prepared_by,
(Select pre_last_date
From (Select pre_last_date,
row_number() over (order by ID desc) as rn
From Authorization
Where project_id = 5
and pre_desig_id = 48
)
Where rn = 1) as pre_end_dt
From Authorization au
Left Join Project p
on au.project_id = p.project_id
Left Join Designation d
on au.au_desig_id = d.desigid;
其中Order By ID Desc
部分产生错误,应将其包装在额外的子查询中,否则编译器不允许直接使用Order By
。即限制(rownum=1
)和使用order by
不允许在同一级别上使用,它们必须分别存在于外部和内部select语句中。
实际上,最好使用row_number()
窗口分析函数,而不是rownum
伪列,这在大多数情况下是不可信的,因为它是在排序之前生成的。
即使将Where rownum = 1
与order by ID desc
一起用于内部select语句也足以供您使用,而是使用row_number()
函数养成习惯。