SQL在具有相同值的所有列中生成Null Value

时间:2012-09-07 15:02:05

标签: sql sql-server-2005

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

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

同样聪明的我需要删除再次返回的值并且agin用于相同的信息...任何人都可以帮助我....并且每次都会根据条件生成表格值一直都不一样......

1 个答案:

答案 0 :(得分:1)

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

; 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

因为这看起来像master-detail,所以当使用左连接加入master时,如果这是第一行细节,你也可以这样做:

; 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

这会将主数据添加到第一行详细信息中。其他行将包含空值。