1

被我用于 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 then 语句来解决问题?

4

3 回答 3

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;
于 2017-01-30T13:47:25.013 回答
0

Check This.

using 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

于 2017-01-30T11:31:11.617 回答
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

现场演示

于 2017-01-30T12:04:04.633 回答