Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

La query per il totale dovrebbe continuare ad aggiungere con ogni riga di costo Colonna

prova questo

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