use AdventureWorks
go
--1. The boss needs a quick query. All he needs is a query with Sales Order Id and --whether the order was online or offline. Select those two columns and display --either Offline or Online in the second column
select SalesOrderID,
case OnlineOrderFlag
when 0 then 'offline'
when 1 then 'online'
end online
from Sales.SalesOrderHeader SOD
--2. It's time for bonuses again. Go through all sales, listing the firstname and --lastname for the salesperson, as well as the sales level.
--less than a $50,000 sale is the Tin level
--less than a $100,000 sale is the Silver level
--Anything over $100,000 sale is the Gold level
select LastName, FirstName,
case
when TotalDue < 50000 then 'Tin Level'
when TotalDue < 100000 then 'Silver Level'
when TotalDue > 100000 then 'Gold Level'
end
from Sales.SalesOrderHeader soh
inner join Person.Contact p
on soh.ContactID = p.ContactID
--Using an if statement, you are going to write a query that has two modes.
--If a variable is set to Sales mode, it will give you a unique list of all
--the names of sales people in 2004. If the variable is set to Customer mode,
--it will give you a unique list of all the names of the customers in 2004.
declare @mode varchar (150)
set @mode = 'c'
If @mode = 's'
begin
select distinct firstname, lastname
from person.Contact pc
inner join HumanResources.Employee hr
on pc.ContactID = hr.ContactID
inner join Sales.SalesOrderHeader so
on hr.EmployeeID = so.contactID
where so.OrderDate between '20040101' and '20041231'
end
else if @mode = 'c'
begin
select distinct firstname, lastname
from Person.Contact pc
inner join Sales.SalesOrderHeader so
on pc.ContactID = so.ContactID
where so.OrderDate between '20040101' and '20041231'
end
Wednesday, April 20, 2011
SQL If/Case statement practice
Labels:
case statement,
If Statement,
sql
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment