10-Course-Database Northwind - 7 With Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

DBA – Soetam Rizky - Information System Study Program

Database Northwind

1. Buat fungsi untuk menghasilkan rata-rata nilai transaksi per kategori, selanjutnya fungsi skalar
tersebut digunakan untuk menampilkan data transaksi per kategori yang nilai transaksnya di
bawah/atas rata-rata.

create function AverageSales


(@CategoryName as nvarchar(15))
Returns Float
as
Begin
Declare @Temp as float;
select @Temp=avg(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
categoryname=@CategoryName
Return @Temp
End

select ProductName,categoryname,quantity*[order details].unitprice as Total


from orders,[order details],products,categories
where
orders.orderid=[order details].orderid and
[order details].productid=products.productid and
products.categoryid=categories.categoryid and
categoryname='Beverages'
group by productname,categoryname

select dbo.averagesales('Beverages')
select Orders.OrderId,OrderDate,
ProductName,quantity*[order details].unitprice as Total
from orders,[order details],products,categories
where
orders.orderid=[order details].orderid and
[order details].productid=products.productid and
products.categoryid=categories.categoryid and
categoryname='Beverages' and
quantity*[order details].unitprice>dbo.averagesales('Beverages')

2. Buat fungsi untuk menampilkan total penjualan tiap barang, dan kemudian dapat digunakan untuk
menampilkan secara spesifik total penjualan dari barang tertentu.

create function OrderSubTotalPerProduct()


Returns Table
as
Return
select productname,
sum(quantity*[order details].unitprice) as Total
from orders,[order details],products
where
orders.orderid=[order details].orderid and

1
DBA – Soetam Rizky - Information System Study Program

[order details].productid = products.productid


group by productname

select * from OrderSubTotalPerProduct()


where productname='Chai'

3. Buat fungsi untuk menampilkan omzet tahunan dari tiap jenis barang, sehingga nantinya dapat
terbentuk sebuah tabel yang dapat menampilkan data dari sebuah tabel temporer dengan
parameter jenis barang.

create function AnnualCategory


(@CategoryName as nvarchar(15))
Returns
@AnnualCategory Table
(
[Year] nvarchar(15),
[1996] float,
[1997] float,
[1998] float)
as
Begin
insert into @AnnualCategory ([Year],[1996],[1997],[1998])
select @CategoryName as 'Omzet Annually',[1996],[1997],[1998]
from
(
select year(orderdate) as tahun,Quantity*[order
details].UnitPrice as Qty from
orders,[order details],customers,products,categories
where orders.orderid=[order details].orderid and
orders.customerid=customers.customerid and
[order details].productid=products.productid and
products.categoryid=categories.categoryid and
categoryname=@CategoryName
)
as DataAwal
pivot
(
sum(DataAwal.Qty) for DataAwal.tahun in
([1996],[1997],[1998])) as temp
return
end

select * from AnnualCategory('Beverages')


select * from AnnualCategory('Seafood')

You might also like