2013-07-04 25 views
4

下面是sql select查詢輸出。單條記錄中的數據

Col1   Col2 Col3 Col4  Col5 Col6 Col7 Col8 Col9 
------------------------------------------------------------------------------------- 
General-Surgery John 193850 21/06/2013 Smith NULL 704.08 NULL NULL 
General-Surgery John 193850 21/06/2013 Smith 2510 NULL NULL NULL 
General-Surgery John 193850 21/06/2013 Smith NULL NULL NULL 19950 
General-Surgery John 193850 21/06/2013 Smith NULL NULL 0  NULL 

這裏Col1中,col2的,COL3,COL4,COL5重複.. 我只是想在一個記錄中的所有數據(除去NULL) 就像下面..

Col1   Col2 Col3 Col4  Col5 Col6  Col7  Col8 Col9 
--------------------------------------------------------------------------------------- 
General-Surgery John 193850 21/06/2013 Smith 704.08 2510  19950 0 

請幫助我在這方面

感謝預期。

回答

10
select 
Col1, 
Col2, 
Col3, 
Col4, 
Col5, 
max(isnull(Col6,0)), 
max(isnull(Col7,0)), 
max(isnull(Col8,0)), 
max(isnull(Col9,0)) 
from table1 
group by Col1, Col2, Col3, Col4, Col5 

SQL Fiddle

+0

對不起@Luv。我將你的isnull複製到我的查詢中。希望你不介意 – Saksham

1

希望這有助於YPU出來。

WITH TempT AS 
(
--YOUR SELECT QUERY FOR THE FIRST TABLE 
) 
SELECT Col1, Col2, Col3, Col4, Col5, 
MAX(isnull(Col6,0)), MAX(isnull(Col7,0)), 
MAX(isnull(Col8,0)), MAX(isnull(Col9,0)) 
FROM TempT 
GROUP BY Col1, Col2, Col3, Col4, Col5 
+0

謝謝Saksham ..你的解決方案工作正常.. – Srinivas

相關問題