单个记录中的数据

时间:2013-07-04 06:03:57

标签: sql sql-server select

下面是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

请在这方面帮助我

感谢您的期待。

2 个答案:

答案 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

答案 1 :(得分:1)

希望这会帮助你。

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