Sie sind auf Seite 1von 9

Praktikum 1 : View - Menulis sebuah query SELECT untuk mendapatkan

semua produk dalam kategori tertentu

Soal 1.
1 SELECT productid, productname, supplierid, unitprice, discontinued
FROM Production.Products
WHERE categoryid = 1;

Soal 2.
1 CREATE VIEW Production.ProductsBeverages AS
SELECT productid, productname, supplierid, unitprice, discontinued
FROM Production.Products
WHERE categoryid = 1;

Praktikum 2 : View – Menulis query SELECT terhadap VIEW yang sudah


dibuat

Soal 3.
1 SELECT productid, productname FROM
Production.ProductsBeverages
WHERE supplierid = 1;
Praktikum 3 : View – Menambahkan klausa ORDER BY pada VIEW
Soal 4.
1 ALTER VIEW Production.ProductsBeverages AS
SELECT productid, productname, supplierid, unitprice, discountinued
FROM Production.Products
WHERE categoryid = 1
ORDER BY productname;

Iya, error karena ORDER BY didalam view tidak Valid

Soal 5.
1 ALTER VIEW Production.ProductsBeverages AS
SELECT TOP(100) PERCENT
productid, productname, supplierid, unitprice, discontinued
FROM Production.Products
WHERE categoryid = 1
ORDER BY productname;

Praktikum:View – Menambahkan kolom ke dalam VIEW


Soal 6.
1 ALTER VIEW Production.ProductsBeverages AS
SELECT
productid, productname, supplierid, unitprice, discontinued,
CASE WHEN unitprice > 100. THEN N'high' ELSE N'normal' END
FROM Production.Products
WHERE categoryid = 1;

eror tersebut muncul karena pembuatan view tidak ada nama kolom
Praktikum 5 : View – Menghapus VIEW
Soal 7.
1 ALTER VIEW Production.ProductsBeverages AS
SELECT
productid, productname, supplierid, unitprice, discontinued,
CASE WHEN unitprice > 100. THEN N'high' ELSE N'normal' END AS
new
FROM Production.Products
WHERE categoryid = 1;

Praktikum 6:Derived Table – Membuat query SELECT dalam derived table


Soal 8.
1 SELECT p.productid, p.productname
FROM(
SELECT productid, productname, supplierid, unitprice, discontinued,
CASE WHEN unitprice > 100. THEN N'high' ELSE N'normal' END AS pricetype
FROM Production.Products
WHERE categoryid = 1
) as p
WHERE pricetype = 'high'
ORDER BY productid;

Praktikum 7: Derived Table – Membuat query SELECT untuk mengetahui


total dan rata – rata jumlah order (nominal)
Soal 9.
1 SELECT new.custid,
SUM(new.total) AS totalsalesamount,
AVG(new.total) AS avgsalesamount
FROM (
SELECT so.custid, so.orderid, SUM(sd.unitprice * sd.qty) AS total
FROM Sales.Orders so
INNER JOIN Sales.OrderDetails sd on sd.orderid = so.orderid
GROUP BY so.custid, so.orderid ) AS new
GROUP BY new.custid
Praktikum 8: Derived Table – Membuat query SELECT untuk mendapatkan
prosentase perkembangan penjualan
Soal 10.
1 SELECT a.orderyear,
a.totalsalesamount AS curtotalsales,
b.totalsalesamount AS prevtotalsales,
(a.totalsalesamount - b.totalsalesamount) / b.totalsalesamount * 100.
AS percentgrowth
FROM (
SELECT YEAR(orderdate) AS orderyear, SUM(val) AS totalsalesamount
FROM Sales.OrderValues
GROUP BY YEAR(orderdate)
) AS a
LEFT OUTER JOIN (
SELECT YEAR(orderdate) as orderyear, SUM(val) AS totalsalesamount
FROM Sales.OrderValues
GROUP BY YEAR(orderdate)
) AS b ON a.orderyear = b.orderyear + 1
ORDER BY a.orderyear;
Praktikum 9: CTE Membuat query SELECT yang menggunakan CTE
Soal 11.
1 WITH ProductBeverage AS (
SELECT productid, productname, supplierid, unitprice, discontinued,
CASE WHEN unitprice > 100. THEN N'high' ELSE N'normal' END AS pricetype
FROM Production.Products
WHERE categoryid = 1 )
SELECT productid, productname
FROM ProductBeverage
WHERE pricetype = 'high';

Praktikum 10: CTE – Membuat query SELECT untuk mendapatkan total


jumlah penjualan (nominalnya) untuk setiap customer
Soal 12.
1 with c2008 (custid, salesamt2008) AS (
SELECT custid, sum(val)
FROM Sales.OrderValues
WHERE year(orderdate) = 2008
GROUP BY custid )
SELECT s.custid, s.contactname, c2008.salesamt2008
FROM Sales.Customers AS s
LEFT JOIN c2008 ON s.custid = c2008.custid;
Praktikum 11: CTE – Membuat query SELECT untuk membandingka
jumlah total penjualan untuk setiap customer dengan tahun sebelumnya
Soal 13.
1 with c2008(custid, salesamt2008) AS (
SELECT custid, sum(val)
FROM Sales.OrderValues
WHERE year (orderdate) = 2008
GROUP BY custid ),
c2007 (custid, salesamt2007) AS (
SELECT custid, sum(val)
FROM Sales.OrderValues
WHERE year (orderdate) = 2007
GROUP BY custid )
SELECT s.custid, s.contactname, c2008.salesamt2008, c2007.salesamt2007,
coalesce((c2008.salesamt2008 - c2007.salesamt2007)/c2007.salesamt2007 *
100.,0)
AS percentgrowth
FROM Sales.Customers AS s
left outer join c2008 ON s.custid = c2008.custid
left outer join c2007 ON s.custid = c2007.custid
ORDER BY percentgrowth DESC;

Praktikum 12: Inline TVF – Membuat query SELECT untuk mendapatkan


total jumlah penjualan (nominal) untuk setiap customer
Soal 14.
1 SELECT custid , sum(val) AS totalsalesamount
FROM Sales.OrderValues
WHERE year(orderdate) = 2007
GROUP BY custid;
Soal 15.
1 CREATE FUNCTION dbo.fnGetSalesByCustomer
(@orderyear as int) returns table
AS
RETURN
SELECT custid, sum (val) AS totalsalesamount
FROM Sales.OrderValues
WHERE year(orderdate) = 2015
GROUP BY custid;

Soal 16.
1
Praktikum 12: Inline ITF – Membuat query SELECT yang beroperasi pada
inline table- valued function
Soal 17.
1 SELECT custid, totalsalesamount
FROM dbo.fnGetSalesByCustomer (2007);

Praktikum 13: Inline ITF – Membuat query SELECT untuk mendapatkan 3


produk terlaris untuk customer tertentu
Soal 18.
1 SELECT TOP(3) so.productid,
max(p.productname) as productname,
sum(so.qty * so.unitprice) as totalsalesamount
FROM Sales.Orders as o
inner join Sales.OrderDetails as so
ON so.orderid = o.orderid
inner join Production.Products as p ON
p.productid = so.productid WHERE
custid = 1
GROUP BY so.productid
ORDER BY totalsalesamount desc;
Soal 19.
1 CREATE FUNCTION dbo.fnGetTop3ProductsForCustomer
(@custid as int) returns table
AS
RETURN
SELECT TOP(3) so.productid,
max(p.productname) as productname,
sum(so.qty * so.unitprice) as totalsalesamount
FROM Sales.Orders as o
inner join Sales.OrderDetails as so
ON so.orderid = o.orderid
inner join Production.Products as p ON
p.productid = so.productid WHERE
custid = 1
GROUP BY so.productid
ORDER BY totalsalesamount desc

Soal 20.
1 SELECT a.productid, a.productname, a.totalsalesamount
FROM dbo.fnGetTop3ProductsForCustomer(1) as a

Das könnte Ihnen auch gefallen