我创建了一个名为 NOV_23 的表格,我希望得到最右边的部分和运输,膳食,住宿,其他,增值税和邮资的总和
CREATE TABLE NOV_23
(
EMPLOYEE_ID INT,
DIVISION VARCHAR (25),
LAST_NAME VARCHAR (25),
FIRST_NAME VARCHAR (25),
TRANSPORTATION INT,
LODGING INT,
TOTAL INT
);
然后我插入了一些值
INSERT INTO NOV_23 VALUES
('1','DivA','LName1','FName1','1','1','')
INSERT INTO NOV_23 VALUES
('2','DivA','LName2','FName2','5','5','')
因此我得到的结果为:
Employee_ID Division Last_Name First_Name Transportation LODGING TOTAL
1 DivA LNameA FNameA 1 1
2 DivA LNameB FNameB 5 5
我可以通过使用更新来获得总数
UPDATE NOV_23
SET TOTAL = (Transportation + Lodging)
WHERE Employee_ID = 1
和
UPDATE NOV_23
SET TOTAL = (Transportation + Lodging)
WHERE Employee_ID = 2
我可以将结果作为
Employee_ID Division Last_Name First_Name Transportation LODGING TOTAL
1 DivA LNameA FNameA 1 1 2
2 DivA LNameB FNameB 5 5 10
我想知道的是如何获取和显示交通总和( 6 ),住宿总和( 6 )及其总体总和(6) +6 = 12 )
Employee_ID Division Last_Name First_Name Transportation LODGING TOTAL
1 DivA LNameA FNameA 1 1 2
2 DivA LNameB FNameB 5 5 10
-- -- -- -- 6 6 12
答案 0 :(得分:0)
你可以计算小计和&使用rollup
SELECT Employee_ID, Division, First_Name||' '||Last_Name Name,
SUM(Transportation) AS Transportation,
SUM(Lodging) AS Lodging,
SUM(Total) AS Total
FROM nov_23
GROUP BY ROLLUP (Employee_ID, Division, First_Name||' '||Last_Name)
ORDER BY Employee_ID, Division, First_Name||' '||Last_Name;
你可以计算小计和&使用cube
SELECT Employee_ID, Division, First_Name||' '||Last_Name Name,
SUM(Transportation) AS Transportation,
SUM(Lodging) AS Lodging,
SUM(Total) AS Total
FROM nov_23
GROUP BY CUBE (Employee_ID, Division, First_Name||' '||Last_Name)
ORDER BY Employee_ID, Division, First_Name||' '||Last_Name;