我有这个select语句,我希望能够通过row_number()进行分区,我刚才在前一行中定义了这个。
但我得到“ORA-30483:此处不允许使用窗口功能”。
如何按row_number进行分区?
如果这不可能,我怎么能重写这个“分区依据”声明?
SELECT DISTINCT
ID "ID",
career "Career",
ROW_NUMBER ()
OVER (PARTITION BY ID, Career
ORDER BY ID, Career)
"Row",
(SUM (some numbers + some numbers)
OVER (
PARTITION BY ID,
(ROW_NUMBER ()
OVER (PARTITION BY ID, Career
ORDER BY ID, Career))))
"All_Units"
FROM TABLE a .....
以下是样本。 我喜欢做的是通过ID,Row(Row是row_number()over(按ID划分,职业订单按ID,Career)的“All_Units”列分区。但是我得到“这里不允许窗口函数”错误。 所以对于02233445,我希望All_Units按ID和Row进行分区,对于第1行返回251是我想要的答案。
ID Career Row All_Units
01122334 GR 1 #
01122334 GR 2 #
02233445 GR 1 251
02233445 GR 2 100
02233445 LAW 1 251
04323333 GR 1 #
04323333 GR 2 #
答案 0 :(得分:0)
您需要使用子查询来引用另一个窗口函数的结果。像这样(未经测试):
SELECT DISTINCT
a.emplid AS "Student ID",
a.acad_career AS "Acad Career",
a_row_number AS "Row",
(SUM (
(SELECT COALESCE (SUM (i.unt_passd_prgrss), 0)
FROM ps_stf_gr_term_vw i
WHERE a.emplid = i.emplid AND a.acad_career = i.acad_career)
+ (SELECT COALESCE (SUM (j.unt_trnsfr), 0)
FROM ps_stf_gr_trnsfr_v j
WHERE a.emplid = j.emplid AND a.acad_career = j.acad_career))
OVER (PARTITION BY a.emplid, a_row_number))
AS "All Units"
FROM (SELECT a.*,
ROW_NUMBER ()
OVER (PARTITION BY a.emplid, a.acad_career
ORDER BY a.emplid, a.acad_career)
AS a_row_number
FROM table_a) a
但是,如果我正确理解您的查询目的,这是获得所需结果的一种迂回方式。我通常发现列列表中存在子查询是一个建议,即查询方向不好。
如果目标只是让table
中的每一行都包含行号以及传递和传输的单位数,我认为这个查询将是一个更好的设计(虽然它也是未经测试的)。
SELECT a.emplid AS "Student ID",
a.acad_career AS "Acad Career",
ROW_NUMBER ()
OVER (PARTITION BY a.emplid, a.acad_career
ORDER BY a.emplid, a.acad_career)
AS "Row",
COALESCE (unt_passd_prgrss, 0) + COALESCE (unt_trnsfr, 0))
AS "All Units"
FROM table_a a
LEFT JOIN
(SELECT emplid,
acad_career,
COALESCE (SUM (unt_passd_prgrss), 0) AS unt_passd_prgrss
FROM ps_stf_gr_term_vw) i
ON a.emplid = i.emplid AND a.acad_career = i.acad_career
LEFT JOIN
(SELECT emplid,
acad_career,
COALESCE (SUM (unt_trnsfr), 0) AS unt_trnsfr
FROM ps_stf_gr_trnsfr_v) j
ON a.emplid = j.emplid AND a.acad_career = j.acad_career
然而,虽然这些解决方案在语法上应该是正确的,但我不相信它们会产生您想要的结果。我没有足够的信息来制作它。