본문 바로가기

SQL

Union / Case statement - SQL code (open)

반응형
SELECT TOP (1000) [EmployeeID]
      ,[FirstName]
      ,[LastName]
      ,[Age]
      ,[Gender]
  FROM [SQL Tutorial].[dbo].[EmployeeDemographics]


  --Union
  select EmployeeID,FirstName,Age
  from [SQL Tutorial].dbo.EmployeeDemographics
  Union All
  select EmployeeID,JobTitle,Salary
  from [SQL Tutorial].dbo.EmployeeSalary
  order by EmployeeID


  --case statement
  select firstname, lastname, Age,
  (Case
		when Age = 38 then 'stanley'
		when Age > 30 then 'old'
		else 'baby'
	End)
  from [SQL Tutorial].dbo.EmployeeDemographics
  where age is not null
  order by Age desc


  --case statement - calculate salaryAfterRaise
  select firstname, lastname, jobtitle, salary,
	case
		when jobtitle ='salesman' then salary+(Salary*.10) --10% raise
		when JobTitle ='accountant' then salary+(Salary*.05) --5% raise
		when JobTitle = 'HR' then salary + (Salary*.00001) 
		else Salary + (Salary*.03)
	end as salaryAfterRaise
  from [SQL Tutorial].dbo.EmployeeDemographics a
  join [SQL Tutorial].dbo.EmployeeSalary b
  on a.EmployeeID= b.EmployeeID​
/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP (1000) [EmployeeID]
      ,[FirstName]
      ,[LastName]
      ,[Age]
      ,[Gender]
  FROM [SQL Tutorial].[dbo].[EmployeeDemographics]


  --Union
  select EmployeeID,FirstName,Age
  from [SQL Tutorial].dbo.EmployeeDemographics
  Union All
  select EmployeeID,JobTitle,Salary
  from [SQL Tutorial].dbo.EmployeeSalary
  order by EmployeeID


  --case statement
  select firstname, lastname, Age,
  (Case
		when Age = 38 then 'stanley'
		when Age > 30 then 'old'
		else 'baby'
	End)
  from [SQL Tutorial].dbo.EmployeeDemographics
  where age is not null
  order by Age desc


  --case statement - calculate salaryAfterRaise
  select firstname, lastname, jobtitle, salary,
	case
		when jobtitle ='salesman' then salary+(Salary*.10) --10% raise
		when JobTitle ='accountant' then salary+(Salary*.05) --5% raise
		when JobTitle = 'HR' then salary + (Salary*.00001) 
		else Salary + (Salary*.03)
	end as salaryAfterRaise
  from [SQL Tutorial].dbo.EmployeeDemographics a
  join [SQL Tutorial].dbo.EmployeeSalary b
  on a.EmployeeID= b.EmployeeID

'SQL' 카테고리의 다른 글

7. Partition By -SQL code(open)  (0) 2022.11.19
6. Aliasing - SQL code (open)  (1) 2022.11.19
5. Updating/Deleting Data - SQL Code(open)  (0) 2022.11.17
4.Having Clause - SQL Code(open)  (0) 2022.11.17
Create a table and Insert int - SQL code (Open)  (0) 2022.11.15