我应该使用哪个 Oracle 分析函数来获得这个结果?
Which Oracle Analytic Function should I use to get this result?
可能是个虚拟问题,但今天是星期天,我的大脑无法正常工作。我正在尝试使用分析函数来获取以下内容
我有这个查询(这只是一条记录的一个小例子,但足以说明我要解释的内容)
select h.party_number,organization_name,address_line1,address_type
,row_number() over(partition by party_number,organization_name,address_line1 order by address_line1,address_type ) as newrn
from h
where party_number = 'FI-110584'
order by address_type,party_number;
这给了我这个结果
我真正想要的是 party_number、organization_name 和 adress_line1
的组合具有相同的排名(因此相同的数字)
所以,最后一列应该是这样的
1
1
2
因为同样的party_number,organization_name和address_line应该赋予相同的排名值
我应该在这里使用什么分析函数?我尝试了 dense_rank
和 rank
但没有成功。
谢谢大家
你需要 DENSE_RANK 而不是 ROW_NUMBER -
SELECT h.party_number,
organization_name,
address_line1,
address_type,
DENSE_RANK() OVER(PARTITION BY party_number,organization_name,address_line1 ORDER BY address_type) as newrn
FROM h
WHERE party_number = 'FI-110584'
ORDER BY address_type, party_number;
对我来说,您要找的似乎是 DENSE_RANK
没有 ADDR
列:
SQL> with test (party, org, addr) as
2 (select 'fi-11', 'neka', 'golfe 4' from dual union all
3 select 'fi-11', 'neka', 'golfe 4' from dual union all
4 select 'fi-11', 'neka', 'pl 100' from dual
5 )
6 select t.*,
7 dense_rank() over (partition by party, org order by addr) rnk
8 from test t
9 order by addr;
PARTY ORG ADDR RNK
----- ---- ------- ----------
fi-11 neka golfe 4 1
fi-11 neka golfe 4 1
fi-11 neka pl 100 2
SQL>
您的分区和排序有点不对:
select h.party_number,organization_name,address_line1,address_type
,dense_rank() over(partition by party_number,organization_name order by address_line1 ) as newrn
from h
where party_number = 'FI-110584'
order by address_type,party_number;
PARTY_NUMBER
ORGANIZATION_NAME
ADDRESS_LINE1
ADDRESS_TYPE
NEWRN
FI-110584
NEKAPAINO OY
GOLFKENTÄNTIE 4
Default
1
FI-110584
NEKAPAINO OY
GOLFKENTÄNTIE 4
DivAdd
1
FI-110584
NEKAPAINO OY
PL 100
InvAdd
2
可能是个虚拟问题,但今天是星期天,我的大脑无法正常工作。我正在尝试使用分析函数来获取以下内容
我有这个查询(这只是一条记录的一个小例子,但足以说明我要解释的内容)
select h.party_number,organization_name,address_line1,address_type
,row_number() over(partition by party_number,organization_name,address_line1 order by address_line1,address_type ) as newrn
from h
where party_number = 'FI-110584'
order by address_type,party_number;
这给了我这个结果
我真正想要的是 party_number、organization_name 和 adress_line1
的组合具有相同的排名(因此相同的数字)所以,最后一列应该是这样的
1
1
2
因为同样的party_number,organization_name和address_line应该赋予相同的排名值
我应该在这里使用什么分析函数?我尝试了 dense_rank
和 rank
但没有成功。
谢谢大家
你需要 DENSE_RANK 而不是 ROW_NUMBER -
SELECT h.party_number,
organization_name,
address_line1,
address_type,
DENSE_RANK() OVER(PARTITION BY party_number,organization_name,address_line1 ORDER BY address_type) as newrn
FROM h
WHERE party_number = 'FI-110584'
ORDER BY address_type, party_number;
对我来说,您要找的似乎是 DENSE_RANK
没有 ADDR
列:
SQL> with test (party, org, addr) as
2 (select 'fi-11', 'neka', 'golfe 4' from dual union all
3 select 'fi-11', 'neka', 'golfe 4' from dual union all
4 select 'fi-11', 'neka', 'pl 100' from dual
5 )
6 select t.*,
7 dense_rank() over (partition by party, org order by addr) rnk
8 from test t
9 order by addr;
PARTY ORG ADDR RNK
----- ---- ------- ----------
fi-11 neka golfe 4 1
fi-11 neka golfe 4 1
fi-11 neka pl 100 2
SQL>
您的分区和排序有点不对:
select h.party_number,organization_name,address_line1,address_type
,dense_rank() over(partition by party_number,organization_name order by address_line1 ) as newrn
from h
where party_number = 'FI-110584'
order by address_type,party_number;
PARTY_NUMBER | ORGANIZATION_NAME | ADDRESS_LINE1 | ADDRESS_TYPE | NEWRN |
---|---|---|---|---|
FI-110584 | NEKAPAINO OY | GOLFKENTÄNTIE 4 | Default | 1 |
FI-110584 | NEKAPAINO OY | GOLFKENTÄNTIE 4 | DivAdd | 1 |
FI-110584 | NEKAPAINO OY | PL 100 | InvAdd | 2 |