2012-09-07 82 views
0

我在我生成該表中我不得不移除重複值的臨時表的場景中的所有列使空值....SQL中具有相同的值

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 

像明智我需要刪除其再次返回阿恩爲同一infomartion價值......任何一個可以幫我這個....和該表將根據每次的條件生成,因此所有時間的值可能不相同...

+0

[你有什麼嘗試](http://whathaveyoutried.com)? – Oded

+0

其實我仍然在微調上述的表格生成...我無法專注於它如何刪除cols因此只發布...每個條目X-fac和Y-fac將是獨特的... –

+0

How桌子很大?你可以用'SELECT DISTINCT * FROM temp'將它包裝在CTE中嗎? –

回答

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 

由於這看起來主 -

; 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 

這將主數據加起來也只有細節的第一行:細節,同時使用LEFT JOIN加盟掌握只有當這是細節的第一行插入,你可能會做同樣的。其他行將包含空值。

+0

什麼是「left join master」可以嗎?請解釋一下....主點到哪個表.... –

+0

謝謝你...我得到了解決方案...真的很感謝... –

+0

@DineshGuptha不客氣:-) –