0% found this document useful (0 votes)
57 views2 pages

10-Course-Database Northwind - 7 With Answers

1. A scalar function is created to calculate the average sales value per category. This function is then used to display transaction data per category that is below or above the average. 2. A table-valued function is created to display the total sales of each product. This can then be used to specifically display the total sales of a particular product. 3. A table-valued function is created to display the annual sales figures of each product type. This allows an temporary table to be generated displaying data from another table with a product type parameter.

Uploaded by

Budi Mulyono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

10-Course-Database Northwind - 7 With Answers

1. A scalar function is created to calculate the average sales value per category. This function is then used to display transaction data per category that is below or above the average. 2. A table-valued function is created to display the total sales of each product. This can then be used to specifically display the total sales of a particular product. 3. A table-valued function is created to display the annual sales figures of each product type. This allows an temporary table to be generated displaying data from another table with a product type parameter.

Uploaded by

Budi Mulyono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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