如何用 'Grand total' 替换列最后一行的 NULL 值,同时保留 'Total' 替换同一列中的 NULL 值?
How to replace the NULL value of the last row of a column with 'Grand total' while retaining 'Total' replacing NULL value in the same column?
下面是 table 创建并在其中插入的值:
CREATE TABLE Employees
(
Id INTEGER IDENTITY(1,1),
Name VARCHAR(50),
Gender VARCHAR(50),
Salary INTEGER,
Country VARCHAR(50)
)
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Mark', 'Male', 5000, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('John', 'Male', 4500, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Pam', 'Female', 5500, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Sara', 'Female', 4000, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Todd', 'Male', 3500, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Mary', 'Female', 5000, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Ben', 'Male', 6500, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Elizabeth', 'Female', 7000, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Tom', 'Male', 5500, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Ron', 'Male', 5000, 'USA')
SELECT * FROM Employees
现在我运行下面的查询:
SELECT
COALESCE(Country, '') AS [Country],
COALESCE(Gender, 'Total') AS [Gender],
SUM(Salary) AS [Total Salary]
FROM
Employees
GROUP BY
ROLLUP(Country, Gender)
当您查看查询结果时,性别列的最后一行包含值 'Total'。
我只想在性别列的最后一行将 'Total' 替换为 'Grand Total',同时在性别列的其他行中保留 'Total' 文本。
有没有可能实现?
如果是这样,那么实现它的最简单方法是什么?
你可以用GROUPING_ID()来做:
SELECT
COALESCE(Country,'') AS [Country],
CASE WHEN GROUPING_ID(Country)=1 THEN 'Grand Total' ELSE COALESCE(Gender,'Total') END as [Gender],
SUM(Salary) AS [Total Salary]
FROM Employees
GROUP BY ROLLUP(Country,Gender)
编辑:在问题的评论中指出,应该指定结果的顺序,以确保它是正确的。
此查询可以这样排序,以确保总计低于详细信息。
SELECT
COALESCE(Country,'') AS [Country],
CASE WHEN GROUPING_ID(Country)=1 THEN 'Grand Total' ELSE COALESCE(Gender,'Total') END as [Gender],
SUM(Salary) AS [Total Salary],
GROUPING_ID(Country),
GROUPING_ID(Gender)
FROM Employees
GROUP BY ROLLUP(Country,Gender)
ORDER BY COALESCE(Country,'ZZZ'),GROUPING_ID(Country),
Gender,GROUPING_ID(Gender)
另一种简单的方法是使用 isnull 连接国家名称,这在 Sql 服务器中更可取,只有两个值,例如:
select
isnull(Country,'') Country,
isnull(Gender, Concat(IsNull(Country, 'Grand'), ' Total')) Gender,
Sum(Salary) [Total Salary]
from Employees
group by rollup(Country,Gender);
下面是 table 创建并在其中插入的值:
CREATE TABLE Employees
(
Id INTEGER IDENTITY(1,1),
Name VARCHAR(50),
Gender VARCHAR(50),
Salary INTEGER,
Country VARCHAR(50)
)
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Mark', 'Male', 5000, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('John', 'Male', 4500, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Pam', 'Female', 5500, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Sara', 'Female', 4000, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Todd', 'Male', 3500, 'India')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Mary', 'Female', 5000, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Ben', 'Male', 6500, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Elizabeth', 'Female', 7000, 'USA')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Tom', 'Male', 5500, 'UK')
INSERT INTO Employees (Name, Gender, Salary, Country)
VALUES ('Ron', 'Male', 5000, 'USA')
SELECT * FROM Employees
现在我运行下面的查询:
SELECT
COALESCE(Country, '') AS [Country],
COALESCE(Gender, 'Total') AS [Gender],
SUM(Salary) AS [Total Salary]
FROM
Employees
GROUP BY
ROLLUP(Country, Gender)
当您查看查询结果时,性别列的最后一行包含值 'Total'。
我只想在性别列的最后一行将 'Total' 替换为 'Grand Total',同时在性别列的其他行中保留 'Total' 文本。
有没有可能实现?
如果是这样,那么实现它的最简单方法是什么?
你可以用GROUPING_ID()来做:
SELECT
COALESCE(Country,'') AS [Country],
CASE WHEN GROUPING_ID(Country)=1 THEN 'Grand Total' ELSE COALESCE(Gender,'Total') END as [Gender],
SUM(Salary) AS [Total Salary]
FROM Employees
GROUP BY ROLLUP(Country,Gender)
编辑:在问题的评论中指出,应该指定结果的顺序,以确保它是正确的。
此查询可以这样排序,以确保总计低于详细信息。
SELECT
COALESCE(Country,'') AS [Country],
CASE WHEN GROUPING_ID(Country)=1 THEN 'Grand Total' ELSE COALESCE(Gender,'Total') END as [Gender],
SUM(Salary) AS [Total Salary],
GROUPING_ID(Country),
GROUPING_ID(Gender)
FROM Employees
GROUP BY ROLLUP(Country,Gender)
ORDER BY COALESCE(Country,'ZZZ'),GROUPING_ID(Country),
Gender,GROUPING_ID(Gender)
另一种简单的方法是使用 isnull 连接国家名称,这在 Sql 服务器中更可取,只有两个值,例如:
select
isnull(Country,'') Country,
isnull(Gender, Concat(IsNull(Country, 'Grand'), ' Total')) Gender,
Sum(Salary) [Total Salary]
from Employees
group by rollup(Country,Gender);