bueno para que entiendas la logica del query....
Código SQL:
Ver originalCREATE TABLE #temp
(
num INT,
num_doc INT,
codigo_doc VARCHAR(20),
total_bs INT,
total DECIMAL(9,6)
)
INSERT INTO #temp VALUES (1,343,'P00014',1000,12.23)
INSERT INTO #temp VALUES (2,344,'P00014',100000,12.12)
INSERT INTO #temp VALUES (3,345,'P00014',50000,12.12)
INSERT INTO #temp VALUES (4,346,'P00015',20000000,22.12)
SELECT num,num_doc,t1.codigo_doc,total_bs,total FROM(
SELECT ROW_NUMBER() OVER(partition BY codigo_doc ORDER BY codigo_doc) AS rn, num,num_doc,codigo_doc FROM #temp) AS t1
LEFT JOIN (SELECT codigo_doc,SUM(total_bs) total_bs,SUM(total) total FROM #temp GROUP BY codigo_doc) AS t2 ON (t1.codigo_doc=t2.codigo_doc AND t1.rn=1)
WHERE rn=1
pruebalo y me dices si es o no es el resultado que buscas.
saludos!