当多个属性相同时匹配特征

时间:2017-01-30 11:14:56

标签: sql oracle fme

卡住了我用于FME的InlineQuerier变换器的SQL语句。我通常不会使用SQL代码那么多工作,只使用if来执行简单的一对一连接。我正在使用包含多个属性的一个数据框,我想基于这些创建一个额外的属性。一个简短的例子:

ID  road_name           road_type    Type     Traffic
1   bakerstreet         elements     B        light
2   bakerstreet         elements     B        light
3   piccadilly circus   asphalt      A        heavy
4   woodstreet          concrete     A        heavy
5   settlerstreet       concrete     A        heavy
6   woodstreet          concrete     A        heavy
7   settlerstreet       concrete     A        heavy
8   settlerstreet       concrete     B        heavy

我试图根据所有4个属性为所有功能提供唯一的RoadID,以便稍后我可以将它们合并。新属性RoadID必须是数据类型Integer,并且该值应匹配4个属性的唯一组合。

结果应该是这样的:

ID  road_name           road_type    Type     Traffic   RoadID
1   bakerstreet         elements     B        light     1
2   bakerstreet         elements     B        light     1
3   piccadilly circus   asphalt      A        heavy     2
4   woodstreet          concrete     A        heavy     3
5   settlerstreet       concrete     A        heavy     4
6   woodstreet          concrete     A        heavy     3 
7   settlerstreet       concrete     A        heavy     4
8   settlerstreet       concrete     B        heavy     5

我是否使用concat或者是否应该使用简单的if语句?

3 个答案:

答案 0 :(得分:0)

检查这个。

使用DENSE_RANK():

        with CTE as
        (
        select distinct * ,
        max(id) over ( Partition by road_name,road_type, Type ,Traffic )  mn    
        from #TableName
        )
        select ID,road_name,road_type,Type,Traffic,dense_rank() over ( order by mn ) RowID 
        from CTE C 
        order by id
  

OutPut:

enter image description here

答案 1 :(得分:0)

尝试mindense_rank窗口函数:

试试这个:

select
    Id, road_name, road_type, Type, Traffic,
    dense_rank() over (order by min_id) roadId
from (
    select
        t.*,
        min(id) over (partition by road_name, road_type, Type, Traffic) min_id
    from your_table t
) t order by id

Live demo

答案 2 :(得分:0)

with src(ID,road_name,           road_type,    Type,     Traffic) as (
    select 1,'bakerstreet','elements','B','light' from dual union all
    select 2,'bakerstreet','elements','B','light' from dual union all
    select 3,'piccadilly circus','asphalt','A','heavy' from dual union all
    select 4,'woodstreet','concrete','A','heavy' from dual union all
    select 5,'settlerstreet','concrete','A','heavy' from dual union all
    select 6,'woodstreet','concrete','A','heavy' from dual union all
    select 7,'settlerstreet','concrete','A','light' from dual union all
    select 8,'settlerstreet','concrete','B','heavy' from dual )
select a.* 
 ,dense_rank() over( order by road_name, road_type, Type, Traffic )
 from src a;