Ich brauche show sum col (Element) unter col mit SQL-Code? es ist möglichShow Summe Spalte SQL-Code
Code item
---- ----
1 30
3 40
4 50
9 80
---- ----
Total 200
Ich brauche show sum col (Element) unter col mit SQL-Code? es ist möglichShow Summe Spalte SQL-Code
Code item
---- ----
1 30
3 40
4 50
9 80
---- ----
Total 200
Verwenden Rollup
der Hauptzeile
SELECT CASE
WHEN Grouping(code) = 1 THEN 'Total'
ELSE Cast(code AS VARCHAR(50))
END,
Sum(item)
FROM Yourtable
GROUP BY code WITH rollup
select * from (select * from #Table1
union
select null, sum(item) item from #Table1)a
order by item
DECLARE @Table1 TABLE
(Code int, item int)
;
INSERT INTO @Table1
(Code, item)
VALUES
(1, 30),
(3, 40),
(4, 50),
(9, 80)
;
Script zu bekommen:
select Code , sum(item)item
from @Table1
group by GROUPING SETS((Code) ,())
order by Code DESC
Select
Code,
item
from
@ table_name
Union All
select
Null,
sum(item)item
from
@ table_name
Wie wir ar Die Verwendung von union all so eindeutig und Reihenfolge für Operation wird gespeichert.
obwohl dies funktionieren kann, greift dies zweimal auf die Tabelle zu. Siehe oben – TheGameiswar