如何在 SQL 的新列中使用案例信息?
How can I use case information in a new column in SQL?
我正在通过在线教程系列学习 SQL,在本视频中我们学习了如何使用 case 语句。
我知道怎么用了,但后来我想“我做的专栏的新数据可以用吗?”我试过尝试并在另一个 CASE 语句中添加以尝试使用 UpdatedSalary
列,但它不起作用。我试着用谷歌搜索了一堆,但我真的不知道如何表达这个问题。
SELECT
FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman' THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant' THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR' THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END AS UpdatedSalary
CASE
WHEN UpdatedSalary > 0 THEN UpdatedSalary - Salary
ELSE
END AS NetIncrease
FROM
SQLTutorial.dbo.EmployeeDemographics
JOIN
SQLTutorial.dbo.EmployeeSalary ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
你不能像这样使用列别名,如果你想使用它你需要在两个列中放置相同的条件或者需要使用外部查询。
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END
AS UpdatedSalary,
CASE
WHEN (CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END)> 0
THEN (CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END)- Salary
ELSE
END
AS NetIncrease
FROM SQLTutorial.dbo.EmployeeDemographics
JOIN SQLTutorial.dbo.EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
或者
SELECT FirstName, LastName, JobTitle, Salary,UpdatedSalary,
CASE
WHEN UpdatedSalary > 0
THEN UpdatedSalary - Salary
ELSE
END
AS NetIncrease
from (
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END
AS UpdatedSalary
FROM SQLTutorial.dbo.EmployeeDemographics
JOIN SQLTutorial.dbo.EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID)t
我正在通过在线教程系列学习 SQL,在本视频中我们学习了如何使用 case 语句。
我知道怎么用了,但后来我想“我做的专栏的新数据可以用吗?”我试过尝试并在另一个 CASE 语句中添加以尝试使用 UpdatedSalary
列,但它不起作用。我试着用谷歌搜索了一堆,但我真的不知道如何表达这个问题。
SELECT
FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman' THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant' THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR' THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END AS UpdatedSalary
CASE
WHEN UpdatedSalary > 0 THEN UpdatedSalary - Salary
ELSE
END AS NetIncrease
FROM
SQLTutorial.dbo.EmployeeDemographics
JOIN
SQLTutorial.dbo.EmployeeSalary ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
你不能像这样使用列别名,如果你想使用它你需要在两个列中放置相同的条件或者需要使用外部查询。
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END
AS UpdatedSalary,
CASE
WHEN (CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END)> 0
THEN (CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END)- Salary
ELSE
END
AS NetIncrease
FROM SQLTutorial.dbo.EmployeeDemographics
JOIN SQLTutorial.dbo.EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
或者
SELECT FirstName, LastName, JobTitle, Salary,UpdatedSalary,
CASE
WHEN UpdatedSalary > 0
THEN UpdatedSalary - Salary
ELSE
END
AS NetIncrease
from (
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'Salesman'
THEN Salary + (Salary * .10)
WHEN JobTitle = 'Accountant'
THEN Salary + (Salary * .05)
WHEN Jobtitle = 'HR'
THEN Salary + (Salary * .00001)
ELSE Salary + (Salary * .03)
END
AS UpdatedSalary
FROM SQLTutorial.dbo.EmployeeDemographics
JOIN SQLTutorial.dbo.EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID)t