sql选择唯一名称并从单个表中排序备用名称而不重复

时间:2014-08-26 18:46:42

标签: sql oracle10g

我有一张oracle 10街道名表。它包含官方街道名称和各种替代名称。我正在尝试创建一个选项,列出官方街道名称和streetid最多3个名称的替代名称。

该表格如

ID   STID   STDIR   STREET_NAME         SUFFIX  POSTDIR STREET_NAME_TYPE
146  585    E       David                DR             STREET
357  821    S       Front Nine           DR             STREET
51   483    S       Colchester           CT             STREET
766  250    E       Bluebird             LN             STREET
758  250            Blue Bird            LN             ALIAS
785  271    S       Breeden              RD             STREET
647  109            Arbutus              RD             HISTORIC
439  911    E       Hackers Creek        RD             STREET
196  642    E       Dora                 RD             STREET
572  22     E       1st Avenue Sanders                  STREET
1128 22             1st                  AVE            ALIAS
1129 22             1st Ave Sanders                     ALIAS
2782 22             First Avenue Sanders                ALIAS
578  28     W       20th                 ST             STREET
2464 4379   S       Tech Park            BLVD           HISTORIC
2988 4379   S       Cooperative          WAY            STREET
...

我想得到像

这样的东西
STIDREAL_STREET_NAME      ALIAS1        ALIAS2          ALIAS3
585  E David DR             
112  E Bluebird LN        Blue Bird LN          
271  S Breeden RD           
22   E 1st Avenue Sanders 1st AVE       1st Ave Sanders First Avenue Sanders
...

但我为任何具有替代名称的街道获取重复值。

STIDREAL_STREET_NAME         ALIAS1              ALIAS2                  ALIAS3
585 E David DR          
112 E Bluebird LN            Blue Bird LN        Blue Bird LN            Blue Bird LN
911 E Hackers Creek RD      
22  E 1st Avenue Sanders     1st Ave Sanders     1st Ave Sanders         1st AVE 
22  E 1st Avenue Sanders     1st Ave Sanders     First Avenue Sanders    1st Ave Sanders  
22  E 1st Avenue Sanders     1st Ave Sanders     1st Ave Sanders         1st Ave Sanders  
22  E 1st Avenue Sanders     1st Ave Sanders     First Avenue Sanders    First Avenue Sanders  
22  E 1st Avenue Sanders     1st Ave Sanders     1st AVE                 1st AVE 
22  E 1st Avenue Sanders     1st Ave Sanders     First Avenue Sanders    1st AVE 
...

这是我使用的查询

SELECT
   msn.street_id AS stid,
     rtrim(msn.street_direction_code)   || ' '
        || msn.street_name             || ' '
        || msn.street_type_suffix_code || ' '
        || rtrim(msn.post_direction_suffix_code) as real_street_name,

   asn1.alias1,
   asn2.alias2,
   asn3.alias3
        FROM eng.mast_street_names msn,

            (SELECT street_id, rtrim(street_direction_code) || ' ' || street_name || ' '
            || street_type_suffix_code || ' ' || rtrim(post_direction_suffix_code) AS alias1
               FROM eng.mast_street_names
                    WHERE street_name_type != 'STREET') asn1,

            (SELECT street_id, rtrim(street_direction_code) || ' ' || street_name || ' '
            || street_type_suffix_code || ' ' || rtrim(post_direction_suffix_code) AS alias2
                FROM eng.mast_street_names
                    WHERE street_name_type != 'STREET') asn2,

            (SELECT street_id, rtrim(street_direction_code) || ' ' || street_name || ' '
            || street_type_suffix_code || ' ' || rtrim(post_direction_suffix_code) AS alias3
                FROM eng.mast_street_names
                    WHERE street_name_type != 'STREET') asn3

        WHERE msn.street_name_type = 'STREET'
        AND msn.street_id = asn1.street_id (+)
        AND msn.street_id = asn2.street_id (+)
        AND msn.street_id = asn3.street_id (+)
;

我尝试过各种方法来分组或计算行数较差且效果较差的行。谢谢你的帮助。

0 个答案:

没有答案