Untitled - MARKUP 3.41 KB
                                
                                    Lab 4 - Klimek Bazy Danych

--1) Utworzyć procedurę sp_OrderCountryReport, do generowania raportu zamówień klientów. Parametry:
--country, datefrom, dateto wypisującą dane o ilości zamówień klientów z danego kraju
--Gdy parametry mają wartość NULL wszystkie dane są uwzględniane.

IF EXISTS (SELECT * FROM sys.procedures WHERE name= 'sp_OrderCountryReport' AND type='P')
    DROP PROCEDURE [dbo].[sp_OrderCountryReport]
GO
CREATE PROCEDURE dbo.sp_OrderCountryReport
@country varchar(25) = NULL,
@datefrom date = NULL,
@dateto date = NULL
AS
SELECT shipcountry, count(@country) AS 'Ilosc zamówien' from sales.orders
WHERE shipcountry = ISNULL(@country, shipcountry)
AND sales.orders.orderdate < ISNULL(@dateto, sales.orders.orderdate)
AND sales.orders.orderdate > ISNULL(@datefrom, sales.orders.orderdate)
GROUP BY shipcountry
GO
EXEC dbo.sp_OrderCountryReport @country='Switzerland', @datefrom='01-01-2000', @dateto='12-12-2007';
EXEC dbo.sp_OrderCountryReport @country='Germany', @datefrom='01-01-2007', @dateto='01-01-2008';


--2) Napisać procedurę sp_UpdateTopProductsPrice aktualizującą cenę (unitprice) produktów w zależności od ich 
--popularności (najmniej popularne cena zmniejszana, najbardziej popularne cena zwiększana). Parametry:
--topReduced, percentReduced, topIncreased, percentIncreased
--Dodać własne reguły sprawdzania poprawności parametrów i wartości domyślne parametrów.
--Wywołać i przetestować procedurę dla różnych danych.

IF EXISTS (SELECT * FROM sys.procedures WHERE name= 'sp_UpdateTopProductsPrice' AND type='P')
    DROP PROCEDURE [dbo].[sp_UpdateTopProductsPrice]
GO
CREATE PROCEDURE dbo.sp_UpdateTopProductsPrice
@topReduced int,
@percentReduced float,
@topIncreased int,
@percentIncreased float
AS

if @topReduced = null or @percentReduced = null or @topIncreased = null or @percentIncreased = null
begin
print('Not all data entered')
return 0
end

if @percentReduced <= 0 or @percentIncreased <= 0
begin
print('Incorrect percentages entered')
return 0
end

CREATE TABLE #najpopularniejsze(
    [product id najpopularniejsze] [int]  NOT NULL,
    [unitprice najpopularniejsze] [int] NOT NULL,
    [counter] [int] NOT NULL
)

CREATE TABLE #najmniejpopularne(
    [product id najmniejpopularnie ] [int]  NOT NULL,
    [unitprice najmniejpopularnie] [int] NOT NULL,
    [counter] [int] NOT NULL
)

INSERT INTO #najpopularniejsze 
select top(@topIncreased) productid, unitprice, count(productid) as 'Ilosc_zamowien'
from Sales.OrderDetails
group by productid, unitprice
order by count(productid) DESC
UPDATE #najpopularniejsze set [unitprice najpopularniejsze] = [unitprice najpopularniejsze] + [unitprice najpopularniejsze] * @percentIncreased;
SELECT * FROM #najpopularniejsze

INSERT INTO #najmniejpopularne 
select top(@topReduced) productid, unitprice, count(productid) as 'Ilosc_zamowien'
from Sales.OrderDetails
group by productid, unitprice
order by count(productid) ASC
UPDATE #najmniejpopularne set [unitprice najmniejpopularnie] = [unitprice najmniejpopularnie] + [unitprice najmniejpopularnie] * @percentReduced;
SELECT * FROM #najmniejpopularne

GO
--
EXEC dbo.sp_UpdateTopProductsPrice 
@topReduced = 10,
@percentReduced = 0.05,
@topIncreased = 10,
@percentIncreased = 0.08;
                                
                            

Paste Hosted With By Wklejamy.pl