0% found this document useful (0 votes)
31 views

10-Course-Database Northwind - 6 With Answers

The document describes stored procedures created in SQL Server to analyze sales data from the Northwind database. The stored procedures: 1. Calculate sales revenue for each product by year. 2. Calculate sales revenue for each product by year and category with a category parameter. 3. Identify the customer with the highest sales revenue each year and print the customer ID. A procedure is also created to print the highest revenue customer name and amount each year.

Uploaded by

Budi Mulyono
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

10-Course-Database Northwind - 6 With Answers

The document describes stored procedures created in SQL Server to analyze sales data from the Northwind database. The stored procedures: 1. Calculate sales revenue for each product by year. 2. Calculate sales revenue for each product by year and category with a category parameter. 3. Identify the customer with the highest sales revenue each year and print the customer ID. A procedure is also created to print the highest revenue customer name and amount each year.

Uploaded by

Budi Mulyono
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DBA – Soetam Rizky - Information System Study Program

Database Northwind

1. Buat stored procedure untuk menampilkan omzet penjualan tiap barang per tahun

create proc ProductPerYear


as
Declare @ProductName as nvarchar(15);
Declare @ProductID as int;
Declare @Year as varchar(4);
Declare @Omzet as varchar(50);
Declare @Rows as int;
Set @Rows = 1 ;
while @Rows <=(select count(*) from products)
begin
Select top (@Rows) @ProductName=productname,
@ProductID =productid from products;
Print cast(@Rows as varchar(5)) + '. Products : ' + @ProductName;

Declare @Rows2 as int;


Set @Rows2 = 1;
while @Rows2<=
(select count(distinct year(orderdate)) from orders)
begin
Select distinct top (@Rows2) @Year=year(orderdate) from orders;
Select @Omzet=sum(quantity*[order details].unitprice)
from orders,[order details],products where
orders.orderid=[order details].orderid and
[order details].productid=products.productid and
products.productid=@productid and
year(orderdate)=@Year;
Print @Year + ' : ' +
'Omzet = ' + @Omzet;
Set @Rows2=@Rows2 + 1;
end;
Set @Rows=@Rows + 1;
Print ' ';
end;
go

exec productperyear

2. Buat stored procedure untuk menampilkan omzet penjualan tiap barang per tahun dengan
parameter kategori

create proc ProductPerYearPerCat


@CategoryName as nvarchar(15)
as
Declare @ProductName as nvarchar(15);
Declare @ProductID as int;
Declare @Year as varchar(4);
Declare @Omzet as varchar(50);
Declare @Rows as int;
Set @Rows = 1 ;
while @Rows <=(select count(*) from products ,categories
where products.categoryid=categories.categoryid and

1
DBA – Soetam Rizky - Information System Study Program

categories.categoryname=@CategoryName)
begin
Select top (@Rows) @ProductName=productname,
@ProductID =productid from products,categories
where products.categoryid=categories.categoryid and
categories.categoryname=@CategoryName;
Print cast(@Rows as varchar(5)) + '. Products : ' + @ProductName;
Declare @Rows2 as int;
Set @Rows2 = 1;
while @Rows2<=
(select count(distinct year(orderdate)) from orders)
begin
Select distinct top (@Rows2) @Year=year(orderdate) from orders;
Select @Omzet=sum(quantity*[order details].unitprice)
from orders,[order details],products,categories where
orders.orderid=[order details].orderid and
[order details].productid=products.productid and
products.categoryid=categories.categoryid and
products.productid=@productid and
categories.categoryname=@CategoryName and
year(orderdate)=@Year;
if cast(@Omzet as float)>0
Print @Year + ' : ' +
'Omzet = ' + @Omzet;
Set @Rows2=@Rows2 + 1;
end;
Set @Rows=@Rows + 1;
Print ' ';
end;
go

exec productperyearpercat 'Beverages'


exec productperyearpercat 'Seafood'
exec productperyearpercat 'Dairy Products'

3. Buat rangkaian prosedur untuk menampilkan data omzet customer yang tertinggi di tiap tahun

create proc MaxCustPerYear


@Year as int,
@CustomerID as nchar(5) output
as
Select top (1) @CustomerID=customers.customerid from
orders,[order details],customers where
orders.orderid=[order details].orderid and
orders.customerid=customers.customerid and
year(orderdate)=@Year
group by customers.customerid
order by sum(quantity*unitprice) desc
go

declare @Cust as nchar(5);


exec maxcustperyear 1996,@Cust out;
print @Cust

drop proc highestomzet


create proc HighestOmzet

2
DBA – Soetam Rizky - Information System Study Program

as
Declare @Rows as int;
Declare @CustomerID as nchar(5);
set @Rows = 1;
while @Rows<=(Select count(distinct year(orderdate)) from orders)
begin
Declare @Year as int;
Declare @CompanyName as nvarchar(50);
Declare @Omzet as varchar(20);

Select distinct top (@Rows) @Year=year(orderdate) from orders;


exec maxcustperyear @Year,@CustomerID out;
Select @CompanyName=companyname,@Omzet=sum(quantity*unitprice)
from orders,[order details],customers where
orders.orderid=[order details].orderid and
orders.customerid=customers.customerid and
customers.customerid=@CustomerID and
year(orderdate)=@Year
group by companyname;
Print 'Year : ' + cast(@Year as char(4)) ;
Print ' Highest omzet is ' + @CompanyName + ' = ' + @Omzet;
set @Rows=@Rows+1;
end;
go

exec highestomzet

You might also like