Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

A consulta para o total deve continuar adicionando a cada linha da Coluna de custo


tente isso
DECLARE @Table TABLE(
        ID INT IDENTITY(1,1),
        Descr VARCHAR(20),
        Val FLOAT
)

INSERT INTO @Table (Descr,Val) SELECT 'X', 10
INSERT INTO @Table (Descr,Val) SELECT 'Y', 20
INSERT INTO @Table (Descr,Val) SELECT 'Z', 50
INSERT INTO @Table (Descr,Val) SELECT 'A', 75
INSERT INTO @Table (Descr,Val) SELECT 'B', 100

SELECT  t1.Descr,
        t1.Val,
        SUM(ISNULL(t2.Val,0))
FROM    @Table t1 LEFT JOIN
        @Table t2 ON t1.ID >= t2.ID
GROUP BY    t1.Descr,
            t1.Val