0

我有一个场景,我在该表中生成一个临时表,我必须删除重复值,如....

id      Ref_No  css_no  haz_no  Emp_no  X-Fac       Y-Fac   Location    street_name     Locale  town_name   admin_area
341139  317000334   NULL    NULL    37101195    588550.00   185922.00       SUTTON ROAD     SOUTHEND-ON-SEA SOUTHEND-ON-SEA
477641  317000334   NULL    NULL    37100003    582297.00   582297.00   ttttttttttttttttttttttttttttttttttttttttttttttttt.....  ABERDEEN GARDENS        LEIGH-ON-SEA    SOUTHEND-ON-SEA
477641  317000334   NULL    NULL    37100003    85000.00    85000.00    ttttttttttttttttttttttttttttttttttttttttttttttttt.....  ABERDEEN GARDENS        LEIGH-ON-SEA    SOUTHEND-ON-SEA
477641  317000334   NULL    NULL    37100003    85000.00    85000.00    ttttttttttttttttttttttttttttttttttttttttttttttttt.....  ABERDEEN GARDENS        LEIGH-ON-SEA    SOUTHEND-ON-SEA
477649  317000334   NULL    NULL    37100004    591278.00   591278.00   aaaaaaaaaaaaaaaaaaaa    ACACIA DRIVE    THORPE BAY  SOUTHEND-ON-SEA SOUTHEND-ON-SEA
477651  317000334   NULL    NULL    37101738    593365.00   593365.00   adfasdsfwerwer  ADJACENT TO 51 SANDPIPER CLOSE  SHOEBURYNESS    SOUTHEND-ON-SEA SOUTHEND-ON-SEA
477661  317000334   NULL    NULL    37100036    589791.00   187149.00       ARCHER AVENUE       SOUTHEND-ON-SEA SOUTHEND-ON-SEA
487012  317000334   NULL    NULL    42203885    359280.00   359280.00   yuiuyiuy    ABERDARE CLOSE  BURTONWOOD AND WESTBROOK    WARRINGTON  WARRINGTON

我希望这个表输出为......

id      Ref_No  css_no  haz_no  Emp_no  X-Fac       Y-Fac   Location    street_name     Locale  town_name   admin_area
    341139  317000334   NULL    NULL    37101195    588550.00   185922.00       SUTTON ROAD     SOUTHEND-ON-SEA SOUTHEND-ON-SEA
    477641  317000334   NULL    NULL    37100003    582297.00   582297.00   ttttttttttttttttttttttttttttttttttttttttttttttttt.....  ABERDEEN GARDENS        LEIGH-ON-SEA    SOUTHEND-ON-SEA
                        NULL    NULL                85000.00    85000.00    
                        NULL    NULL                85000.00    85000.00    
    477649  317000334   NULL    NULL    37100004    591278.00   591278.00   aaaaaaaaaaaaaaaaaaaa    ACACIA DRIVE    THORPE BAY  SOUTHEND-ON-SEA SOUTHEND-ON-SEA
    477651  317000334   NULL    NULL    37101738    593365.00   593365.00   adfasdsfwerwer  ADJACENT TO 51 SANDPIPER CLOSE  SHOEBURYNESS    SOUTHEND-ON-SEA SOUTHEND-ON-SEA
    477661  317000334   NULL    NULL    37100036    589791.00   187149.00       ARCHER AVENUE       SOUTHEND-ON-SEA SOUTHEND-ON-SEA
    487012  317000334   NULL    NULL    42203885    359280.00   359280.00   yuiuyiuy    ABERDARE CLOSE  BURTONWOOD AND WESTBROOK    WARRINGTON  WARRINGTON

同样,我需要删除再次返回的值并重新获取相同的信息...任何人都可以帮我解决这个问题...并且每次都会根据条件生成表格,因此值可能不是一直都一样...

4

1 回答 1

1

这是显示问题,因此最好在客户端应用程序中处理。但是,如果您别无选择,只能在 t-sql 中执行此操作,您可以使用row_number() over()来隔离某个值的第一次出现,以及何时将所有其他出现的值替换为空字符串:

; with cte as
(
  select *,
         row_number() over(partition by id
         -- determine order by detail table.
         -- I don't know your schema.
                           order by [y-fac]) rn
    from tempTable
)
select case when rn = 1
            then convert(varchar(20), id)
            else ''
        end as id,
       case when rn = 1
            then convert(varchar(20), Ref_No)
            else ''
        end as Ref_No,
       css_no,
       haz_no,
       case when rn = 1
            then convert(varchar(20), Emp_no)
            else ''
        end as Emp_no,
       X-Fac,
       Y-Fac,
       case when rn = 1
            then Location
            else ''
        end as Location,
       case when rn = 1
            then street_name
            else ''
        end as street_name,
       case when rn = 1
            then Locale
            else ''
        end as Locale,
       case when rn = 1
            then town_name
            else ''
        end as town_name,
       case when rn = 1
            then admin_area
            else ''
        end as admin_area
from cte
order by id, rn

由于这看起来像主详细信息,因此您可以在使用左连接插入主详细信息时执行相同操作,前提是这是第一行详细信息:

; with ordered as
(
  select *,
         row_number() over (partition by master_id
                            order by detail_id) rn
    from detail
)
insert into tempTable ( ... )
select ...
  from ordered
  left join master
    on ordered.master_id = master.master_id
   and ordered.rn = 1

这只会将主数据添加到第一行明细。其他行将包含空值。

于 2012-09-07T23:20:24.017 回答