Add files via upload
This commit is contained in:
Родитель
128f7d9678
Коммит
6a64a8a436
|
@ -0,0 +1,5 @@
|
|||
CREATE PROCEDURE removeItem (@item_sk int)
|
||||
AS
|
||||
BEGIN
|
||||
DELETE from item where i_item_sk = @item_sk;
|
||||
END
|
|
@ -0,0 +1,22 @@
|
|||
--people who shopped in 5 diffrent months in a given year from store
|
||||
--make it a function maybe
|
||||
|
||||
create or alter procedure repeatedShoppers (@year int)
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select ss_customer_sk, count(*)
|
||||
from
|
||||
(select distinct ss_customer_sk, d_moy
|
||||
from store_sales_history, date_dim
|
||||
where ss_sold_date_sk = d_date_sk
|
||||
and d_year = 2001
|
||||
and ss_customer_sk is not NULL
|
||||
)t
|
||||
group by ss_customer_sk
|
||||
having count(*)>=5
|
||||
order by count(*) desc
|
||||
|
||||
end
|
||||
go
|
|
@ -0,0 +1,11 @@
|
|||
create or alter procedure returnReason_highEducated
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select r_reason_desc, count(*) as cnt
|
||||
from store_returns_history, customer_demographics, reason
|
||||
where sr_customer_sk = cd_demo_sk and sr_reason_sk = r_reason_sk and cd_education_status = 'Advanced Degree'
|
||||
group by r_reason_desc
|
||||
order by cnt desc;
|
||||
end
|
|
@ -0,0 +1,11 @@
|
|||
create or alter procedure returnReason_lessEducated
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select r_reason_desc, count(*) as cnt
|
||||
from store_returns_history, customer_demographics, reason
|
||||
where sr_customer_sk = cd_demo_sk and sr_reason_sk = r_reason_sk and cd_education_status = 'Primary'
|
||||
group by r_reason_desc
|
||||
order by cnt desc;
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
--Report the total extended sales price per item brand of a specific manufacturer for all catalog sales in a specific month
|
||||
--of the year.
|
||||
CREATE PROCEDURE salePerBrandCatalog (@year int, @month int, @manufacture int)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
select i_brand, sum(cs_ext_sales_price) as totalSale
|
||||
from catalog_sales_history, item, date_dim
|
||||
where i_item_sk = cs_item_sk
|
||||
and i_manufact_id = @manufacture
|
||||
and cs_sold_date_sk = d_date_sk
|
||||
and d_year = @year
|
||||
and d_moy = @month
|
||||
group by i_brand
|
||||
END
|
|
@ -0,0 +1,16 @@
|
|||
--Report the total extended sales price per item brand of a specific manufacturer for all store sales in a specific month
|
||||
--of the year.
|
||||
CREATE PROCEDURE salePerBrandStore (@year int, @month int, @manufacture int)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
select i_brand, sum(ss_ext_sales_price) as totalSale
|
||||
from store_sales_history, item, date_dim
|
||||
where i_item_sk = ss_item_sk
|
||||
and i_manufact_id = @manufacture
|
||||
and ss_sold_date_sk = d_date_sk
|
||||
and d_year = @year
|
||||
and d_moy = @month
|
||||
group by i_brand
|
||||
END
|
|
@ -0,0 +1,16 @@
|
|||
--Report the total extended sales price per item brand of a specific manufacturer for all web sales in a specific month
|
||||
--of the year.
|
||||
CREATE PROCEDURE salePerBrandWeb (@year int, @month int, @manufacture int)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
select i_brand, sum(ws_ext_sales_price) as totalSale
|
||||
from web_sales_history, item, date_dim
|
||||
where i_item_sk = ws_item_sk
|
||||
and i_manufact_id = @manufacture
|
||||
and ws_sold_date_sk = d_date_sk
|
||||
and d_year = @year
|
||||
and d_moy = @month
|
||||
group by i_brand
|
||||
END
|
|
@ -0,0 +1,21 @@
|
|||
--information on customers who purchased items from catalog that were on sale during a particular time
|
||||
CREATE PROCEDURE saleShoppers
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
|
||||
select cs_bill_customer_sk as customer_sk,
|
||||
c_first_name, c_last_name, c_email_address, c_birth_year,
|
||||
cs_item_sk, d1.d_date as sold_date,
|
||||
d2.d_date as promo_strt_date, d3.d_date as promo_end_date,
|
||||
cs_promo_sk
|
||||
from catalog_sales_history, promotion, date_dim d1, date_dim d2, date_dim d3, customer
|
||||
where p_promo_sk = cs_promo_sk
|
||||
and cs_sold_date_sk = d1.d_date_sk
|
||||
and p_start_date_sk = d2.d_date_sk
|
||||
and p_end_date_sk = d3.d_date_sk
|
||||
and d1.d_date>=d2.d_date
|
||||
and d1.d_date<=d3.d_date
|
||||
and cs_bill_customer_sk = c_customer_sk
|
||||
|
||||
END
|
|
@ -0,0 +1,7 @@
|
|||
CREATE PROCEDURE updateItemPrice (@itemSk int, @newPrice decimal(7, 2))
|
||||
AS
|
||||
BEGIN
|
||||
UPDATE item set i_current_price = @newPrice
|
||||
where i_item_sk = @itemSk
|
||||
and @newPrice < 3*i_wholesale_cost;
|
||||
END
|
|
@ -0,0 +1,14 @@
|
|||
CREATE or alter PROCEDURE updateWebURL(@oldUrl varchar(100), @newUrl varchar(100))
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
|
||||
UPDATE web_page
|
||||
set wp_url = @newUrl
|
||||
where wp_url = @oldUrl;
|
||||
END
|
||||
go
|
||||
|
||||
--execution query
|
||||
exec updateWebURL 'http://www.bar.com', 'http://www.foo.com'
|
||||
go
|
|
@ -0,0 +1,16 @@
|
|||
--Calculate the average sales quantity, average sales price, average wholesale cost, total wholesale cost for store
|
||||
--sales of different customer types (based on marital status and gender) from the given state.
|
||||
|
||||
CREATE PROCEDURE customerDemographicSaleInfo (@state char(2))
|
||||
AS
|
||||
BEGIN
|
||||
|
||||
select ca_state, cd_gender, cd_marital_status, avg(ss_quantity) as avg_qty, avg(ss_sales_price) avg_sale,
|
||||
avg(ss_ext_wholesale_cost) as avg_wholsesale, sum(ss_ext_wholesale_cost) as sum_wholesale
|
||||
from store_sales_history, customer_demographics, customer, customer_address
|
||||
where ss_cdemo_sk = cd_demo_sk
|
||||
and c_customer_sk = ss_customer_sk
|
||||
and c_current_cdemo_sk = ca_address_sk
|
||||
and ca_state = '@state'
|
||||
group by ca_state, cd_gender, cd_marital_status
|
||||
END
|
|
@ -0,0 +1,8 @@
|
|||
CREATE PROCEDURE warehouseAddress_Get (@warehouseId char(16))
|
||||
AS
|
||||
BEGIN
|
||||
select w_warehouse_name, w_street_number, w_street_name, w_suite_number, w_city, w_county, w_state, w_zip, w_country
|
||||
FROM warehouse
|
||||
where w_warehouse_id = @warehouseId
|
||||
END
|
||||
GO
|
|
@ -0,0 +1,18 @@
|
|||
create or alter procedure activatePromoCat
|
||||
as
|
||||
begin
|
||||
declare @promo_sk int;
|
||||
declare c1 cursor static for (select promo_sk from dbo.bestPromosCatalog());
|
||||
open c1;
|
||||
fetch next from c1 into @promo_sk;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update promotion set p_end_date_sk = 10000000, p_discount_active='Y'
|
||||
where p_promo_sk = @promo_sk;
|
||||
|
||||
fetch next from c1 into @promo_sk;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,18 @@
|
|||
create or alter procedure activatePromoStore
|
||||
as
|
||||
begin
|
||||
declare @promo_sk int;
|
||||
declare c1 cursor static for (select promo_sk from dbo.bestPromoStore());
|
||||
open c1;
|
||||
fetch next from c1 into @promo_sk;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update promotion set p_end_date_sk = 10000000, p_discount_active='Y'
|
||||
where p_promo_sk = @promo_sk;
|
||||
|
||||
fetch next from c1 into @promo_sk;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,18 @@
|
|||
create or alter procedure activatePromoWeb
|
||||
as
|
||||
begin
|
||||
declare @promo_sk int;
|
||||
declare c1 cursor static for (select promo_sk from dbo.bestPromosWeb());
|
||||
open c1;
|
||||
fetch next from c1 into @promo_sk;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update promotion set p_end_date_sk = 10000000, p_discount_active='Y'
|
||||
where p_promo_sk = @promo_sk;
|
||||
|
||||
fetch next from c1 into @promo_sk;
|
||||
end
|
||||
CLOSE C1;
|
||||
DEALLOCATE C1;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,24 @@
|
|||
CREATE or alter PROCEDURE CreateRandomString
|
||||
@randomString VARCHAR(16) OUTPUT
|
||||
AS BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
DECLARE @stringLength INT =16;
|
||||
DECLARE @chars VARCHAR(200) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
SET @randomString = '';
|
||||
|
||||
WHILE LEN(@randomString) < @stringLength
|
||||
BEGIN
|
||||
SET @randomString = @randomString + dbo.genRandomChar(@chars, RAND());
|
||||
END
|
||||
END
|
||||
go
|
||||
|
||||
--ivocation query
|
||||
declare @randomString VARCHAR(16);
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @randomString AS [Random String];
|
||||
go
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
create or alter procedure deleteCatalogPage(@delContent varchar(20), @updateContent varchar(20), @updateDept varchar(40))
|
||||
as
|
||||
begin
|
||||
if(@delContent!='')
|
||||
begin
|
||||
select cp_catalog_number as deletedCat, cp_catalog_page_number as deletedPgNum
|
||||
from catalog_page
|
||||
where cp_description like '%'+@delContent+'%';
|
||||
|
||||
delete from catalog_page where cp_description like '%'+@delContent+'%';
|
||||
end
|
||||
|
||||
if(@updateContent!='')
|
||||
update catalog_page set cp_department = @updateDept where cp_description like '%'+@updateDept+'%';
|
||||
end
|
||||
go
|
||||
|
||||
--execution query
|
||||
exec dbo.deleteCatalogPage 'weapon', 'patient', 'Medical'
|
||||
go
|
|
@ -0,0 +1,28 @@
|
|||
use tpcds_10gb;
|
||||
go
|
||||
--does look rather weird. Change logic.
|
||||
create procedure getReturnReason
|
||||
@reason_sk int,
|
||||
@reason_id nvarchar(16)
|
||||
as
|
||||
begin
|
||||
set nocount on
|
||||
|
||||
if(@reason_sk = null and @reason_id = null)
|
||||
begin
|
||||
raiserror('Invalid arguments', 16, 1)
|
||||
end
|
||||
|
||||
declare @curDate date = GetDate();
|
||||
declare @day int = DAY(@curDate);
|
||||
declare @month int = MONTH(@curDate);
|
||||
declare @year int = YEAR(@curDate);
|
||||
|
||||
select cr_reason_sk, r_reason_desc
|
||||
from catalog_returns, date_dim, reason
|
||||
where cr_returned_date_sk = d_date_sk
|
||||
and r_reason_sk = cr_reason_sk
|
||||
and d_moy <= @month and d_moy >= @month-5;
|
||||
end
|
||||
|
||||
select * from reason
|
|
@ -0,0 +1,12 @@
|
|||
create or alter procedure getStoreByManager(@manager varchar(40))
|
||||
as
|
||||
begin
|
||||
if not exists (select * from store where s_manager= @manager)
|
||||
begin
|
||||
RAISERROR('No stores operated by this manager', 10, 16);
|
||||
end
|
||||
else
|
||||
select s_street_number, s_street_name, s_street_type, s_suite_number, s_city, s_county, s_state,s_zip, s_country
|
||||
from store
|
||||
where s_manager = @manager;
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
CREATE PROCEDURE InsertCallCenter (@strt_date date, @end_date date, @cc_closed_date int, @open_date int , @cc_name varchar(50),
|
||||
@sk int, @id char(16), @class varchar(50), @numEmpl int, @size int, @hrs char(20),
|
||||
@manager varchar(40), @mkt_id int, @mkt_cls char(50), @mkt_desc varchar(100),
|
||||
@Mktmanager varchar(40), @div int, @divName varchar(50), @company int, @company_name char(50),
|
||||
@st_num char(10), stName varchar(60), stType char(15), @cc_suite char(10),
|
||||
@city varchar(60), @county varchar(30), @state char(2), @zip char(10),
|
||||
@country varchar(20), @offset decimal(5,2), @taxPercent decimal(5,2))
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
if not exists (select * from call_center where cc_call_center_sk = @sk)
|
||||
insert into call_center values (@strt_date, @end_date, @cc_closed_date, @open_date, @cc_name, @sk, @id, @class, @numEmpl, @size, @hrs,
|
||||
@manager, @mkt_id, @mkt_cls, @mkt_desc, @Mktmanager, @div, @divName, @company,
|
||||
@company_name, @st_num, stName, stType, @cc_suite, @cit, @county, @state, @zip, @country,
|
||||
@offset, @taxPercent)
|
||||
END
|
|
@ -0,0 +1,24 @@
|
|||
create or alter procedure newShippingCarrier(@type char(30), @code char(10), @name char(20),
|
||||
@contract char(20))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
declare @randomString char(16);
|
||||
declare @sk int;
|
||||
declare @id char(16);
|
||||
|
||||
set @sk = (select max(sm_ship_mode_sk)+1 from ship_mode );
|
||||
|
||||
EXEC dbo.CreateRandomString @randomString OUTPUT;
|
||||
set @id = @randomString;
|
||||
|
||||
insert into ship_mode (sm_ship_mode_sk, sm_ship_mode_id, sm_type, sm_code, sm_carrier, sm_contract)
|
||||
values (@sk, @id, @type, @code, @name, @contract);
|
||||
|
||||
end
|
||||
|
||||
--calling query
|
||||
exec dbo.newShippingCarrier 'FORTNIGHT', 'SEA', 'SHIPCo', '83hdjk0hf8j'
|
||||
go
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
CREATE PROCEDURE excessReturn_web
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
|
||||
WITH ReturnTable
|
||||
AS (SELECT wr_returning_customer_sk,
|
||||
ca_state,
|
||||
Sum(wr_return_amt) as returnAmt
|
||||
FROM web_returns_history,
|
||||
customer_address,
|
||||
date_dim
|
||||
WHERE wr_returned_date_sk = d_date_sk
|
||||
and wr_returning_addr_sk = ca_address_sk
|
||||
AND d_year = 2001
|
||||
GROUP BY wr_returning_customer_sk,
|
||||
ca_state)
|
||||
SELECT c_customer_id, c_salutation, c_first_name, c_last_name, c_email_address, c_birth_year, c_birth_country
|
||||
FROM ReturnTable tr1,
|
||||
customer_address ca1,
|
||||
customer
|
||||
WHERE tr1.returnAmt > (SELECT Avg(returnAmt) * 1.2
|
||||
FROM ReturnTable tr2
|
||||
WHERE tr1.ca_state = tr2.ca_state)
|
||||
AND ca_address_sk = c_current_addr_sk
|
||||
AND tr1.wr_returning_customer_sk = c_customer_sk
|
||||
AND ca1.ca_state = 'TX'
|
||||
END
|
|
@ -0,0 +1,26 @@
|
|||
--Total loss incurred on each channel by credit risky, hihgly educated men with non-zero dependents
|
||||
|
||||
create or alter procedure lossByEducated_risky
|
||||
as
|
||||
begin
|
||||
declare @web_loss decimal(15,2);
|
||||
declare @cat_loss decimal(15,2);
|
||||
declare @total_loss decimal(15,2);
|
||||
|
||||
select @web_loss = sum(ws_net_profit)
|
||||
from web_sales_history
|
||||
where ws_bill_cdemo_sk in
|
||||
(select cd_demo_sk from customer_demographics where cd_gender='M' and cd_education_status = 'Advanced Degree'
|
||||
and cd_dep_count>0 and cd_credit_rating = 'High Risk')
|
||||
and ws_net_profit<0;
|
||||
|
||||
select @cat_loss = sum(cs_net_profit)
|
||||
from catalog_sales_history
|
||||
where cs_bill_cdemo_sk in
|
||||
(select cd_demo_sk from customer_demographics where cd_gender='M' and cd_education_status = 'Advanced Degree'
|
||||
and cd_dep_count>0 and cd_credit_rating = 'High Risk')
|
||||
and cs_net_profit<0;
|
||||
|
||||
select @total_loss = @web_loss + @cat_loss;
|
||||
print(@total_loss);
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
--which months registered the highest store sale of electronics for a given 3 year period.
|
||||
|
||||
--which months registered the highest sale of electronics for 3 year period startig at the given year.
|
||||
|
||||
create or alter procedure maxSaleElectronicsMonth(@year int)
|
||||
as
|
||||
begin
|
||||
declare @month1 int, @month2 int, @month3 int;
|
||||
|
||||
set @month1 = (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year and i_category='Electronics'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
set @month2= (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year+1 and i_category='Electronics'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
set @month3 = (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year+2 and i_category='Electronics'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
select @month1 as year1 , @month2 as year2 , @month3 as year3;
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
--which months registered the highest sale of jewelry for 3 year period startig at the given year.
|
||||
|
||||
create or alter procedure maxSaleJewelryMonth(@year int)
|
||||
as
|
||||
begin
|
||||
declare @month1 int, @month2 int, @month3 int;
|
||||
set @month1 = (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year and i_category='jewelry'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
set @month2= (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year+1 and i_category='jewelry'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
set @month3 = (select top 1 d_moy
|
||||
from store_sales_history, item, date_dim
|
||||
where ss_item_sk = i_item_sk and ss_sold_date_sk=d_date_sk and d_year = @year+2 and i_category='jewelry'
|
||||
group by d_moy
|
||||
order by count(*) desc);
|
||||
|
||||
select @month1 as year1 , @month2 as year2 , @month3 as year3
|
||||
end
|
|
@ -0,0 +1,18 @@
|
|||
create or alter procedure newCatalogPage(@strtDate int, @endDate int, @dept varchar(50), @catNumber int,
|
||||
@pgNum int, @desc varchar(100), @typevarchar varchar(100))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
declare @randomString char(16);
|
||||
declare @sk int;
|
||||
declare @id char(16);
|
||||
|
||||
set @sk = (select max(cp_catalog_page_sk)+1 from catalog_page);
|
||||
|
||||
EXEC dbo.CreateRandomString @randomString OUTPUT;
|
||||
set @id = @randomString;
|
||||
insert into catalog_page (cp_catalog_page_sk, cp_catalog_page_id, cp_start_date_sk, cp_end_date_sk,
|
||||
cp_department, cp_catalog_number, cp_catalog_page_number, cp_description, cp_type)
|
||||
values (@sk, @id, @strtDate, @endDate, @dept, @catNumber, @pgNum, @desc, @typevarchar);
|
||||
end
|
|
@ -0,0 +1,12 @@
|
|||
create procedure new_promotion(@item_sk int, @start_date int, @end_date int, @cost decimal(15, 2))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @newSk int, @maxSk int;
|
||||
set @maxSk = (select max(p_promo_sk) from promotion);
|
||||
|
||||
set @newSk = @maxSk+1;
|
||||
|
||||
insert into promotion(p_promo_sk, p_start_date_sk, p_end_date_sk, p_item_sk, p_cost, p_discount_active)
|
||||
values (@newSk, @start_date, @end_date, @item_sk, @cost, 'Y');
|
||||
end
|
|
@ -0,0 +1,10 @@
|
|||
create procedure newStore(@store_sk int, @manager varchar(40))
|
||||
as
|
||||
begin
|
||||
if not exists (select * from store where s_store_sk=@store_sk)
|
||||
insert into store (s_store_sk, s_store_id, s_manager) values (@store_sk, DEFAULT, @manager);
|
||||
else
|
||||
update store set s_manager = @manager where s_store_sk = @store_sk;
|
||||
end
|
||||
|
||||
go
|
|
@ -0,0 +1,18 @@
|
|||
--add a new warehouse to the database:
|
||||
create or alter procedure newWarehouse(@name varchar(20), @sqFt int, @streetNo char(10), @stName varchar(60),
|
||||
@suite char(10),@city varchar(60), @county varchar(30), @state char(2),
|
||||
@zip char(10), @country varchar(20))
|
||||
as
|
||||
begin
|
||||
declare @randomString char(16);
|
||||
declare @sk int;
|
||||
declare @id char(16);
|
||||
set @sk = (select max(w_warehouse_sk)+1 from warehouse );
|
||||
|
||||
EXEC dbo.CreateRandomString @randomString OUTPUT;
|
||||
set @id = @randomString;
|
||||
insert into warehouse (w_warehouse_sk, w_warehouse_id, w_warehouse_name, w_warehouse_sq_ft, w_street_number,
|
||||
w_street_name, w_suite_number, w_city, w_county, w_state, w_zip, w_country)
|
||||
values (@sk, @id, @name, @sqFt, @streetNo, @stName, @suite, @city, @county, @state, @zip, @country);
|
||||
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
--new return request
|
||||
|
||||
create procedure processReturn_store
|
||||
(@item_sk int, @cust_sk int, @reason_sk int, @ticketNum int, @returnQty int, @returnReason char(100))
|
||||
as
|
||||
begin
|
||||
declare @curDate date = GETDATE();
|
||||
declare @dateSk int;
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date = @curDate);
|
||||
insert into store_returns (sr_returned_date_sk, sr_item_sk, sr_customer_sk, sr_reason_sk, sr_ticket_number, sr_return_quantity)
|
||||
values (@dateSk, @item_sk, @cust_sk, @reason_sk, @ticketNum, @returnQty);
|
||||
if exists (select * from reason where r_reason_sk=@reason_sk)
|
||||
return;
|
||||
else
|
||||
insert into reason (r_reason_sk, r_reason_desc) values (@reason_sk, @returnReason);
|
||||
end
|
|
@ -0,0 +1,18 @@
|
|||
CREATE or ALTER PROCEDURE RemoveObject(
|
||||
@objName nvarchar(max),
|
||||
@newName NVARCHAR(MAX)=NULL OUTPUT,
|
||||
@IfExists int = 0)
|
||||
AS
|
||||
BEGIN
|
||||
DECLARE @ObjectId INT;
|
||||
SELECT @ObjectId = OBJECT_ID(@objName);
|
||||
|
||||
IF(@ObjectId IS NULL)
|
||||
BEGIN
|
||||
IF(@IfExists = 1) RETURN;
|
||||
RAISERROR('%s does not exist!',16,10,@objName);
|
||||
END;
|
||||
|
||||
EXEC dbo.RenameObjectUsingObjectId @ObjectId, @NewName = @NewName OUTPUT;
|
||||
END;
|
||||
GO
|
|
@ -0,0 +1,11 @@
|
|||
CREATE PROCEDURE RemoveObjectIfExists(
|
||||
@ObjectName NVARCHAR(MAX),
|
||||
@NewName NVARCHAR(MAX) = NULL OUTPUT
|
||||
)
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
if exists (select * from sys.objects where name = @ObjectName)
|
||||
EXEC dbo.RemoveObject @ObjectName = @ObjectName, @NewName = @NewName OUT, @IfExists = 1;
|
||||
END;
|
||||
GO
|
|
@ -0,0 +1,33 @@
|
|||
CREATE PROCEDURE getExcessReturnCustInfo
|
||||
AS
|
||||
BEGIN
|
||||
WITH totalReturn
|
||||
AS (SELECT sr_customer_sk,
|
||||
sr_store_sk,
|
||||
Sum(sr_return_amt) as returnAmt
|
||||
FROM store_returns_history,
|
||||
date_dim
|
||||
WHERE sr_returned_date_sk = d_date_sk
|
||||
AND d_year = 2001
|
||||
GROUP BY sr_customer_sk,
|
||||
sr_store_sk)
|
||||
SELECT distinct c_customer_id, c_email_address, cd_gender, cd_credit_rating
|
||||
FROM totalReturn tr1,
|
||||
store,
|
||||
customer,
|
||||
customer_demographics
|
||||
WHERE tr1.returnAmt > (SELECT Avg(returnAmt) * 1.2
|
||||
FROM totalReturn tr2
|
||||
WHERE tr1.sr_store_sk = tr2.sr_store_sk)
|
||||
AND s_store_sk = tr1.sr_store_sk
|
||||
AND tr1.sr_customer_sk = c_customer_sk
|
||||
AND c_current_cdemo_sk = cd_demo_sk
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
CREATE PROCEDURE RenameObject(
|
||||
@SchemaName NVARCHAR(MAX),
|
||||
@ObjectName NVARCHAR(MAX),
|
||||
@NewName NVARCHAR(MAX) = NULL OUTPUT
|
||||
)
|
||||
AS
|
||||
BEGIN
|
||||
SET @NewName='newName123';
|
||||
|
||||
DECLARE @RenameCmd NVARCHAR(MAX);
|
||||
SET @RenameCmd = 'EXEC sp_rename ''' +
|
||||
@SchemaName + '.' + @ObjectName + ''', ''' +
|
||||
@NewName + ''';';
|
||||
|
||||
EXEC @RenameCmd;
|
||||
|
||||
END;
|
|
@ -0,0 +1,16 @@
|
|||
CREATE PROCEDURE RenameObjectUsingObjectId(
|
||||
@ObjectId INT,
|
||||
@NewName NVARCHAR(MAX) = NULL OUTPUT
|
||||
)
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
|
||||
DECLARE @SchemaName NVARCHAR(MAX);
|
||||
DECLARE @ObjectName NVARCHAR(MAX);
|
||||
|
||||
SELECT @SchemaName = QUOTENAME(OBJECT_SCHEMA_NAME(@ObjectId)), @ObjectName = QUOTENAME(OBJECT_NAME(@ObjectId));
|
||||
|
||||
EXEC dbo.RenameObject @SchemaName,@ObjectName, @NewName OUTPUT;
|
||||
END;
|
||||
GO
|
|
@ -0,0 +1,44 @@
|
|||
create procedure totalInventoryDefeciency
|
||||
as
|
||||
begin
|
||||
declare @webSales table(item_sk int, date_sk int, qty int)
|
||||
declare @catSales table(item_sk int, date_sk int, qty int)
|
||||
|
||||
insert into @webSales
|
||||
select ws_item_sk, ws_sold_date_sk,sum(ws_quantity)
|
||||
from web_sales_history, date_dim
|
||||
where d_date_sk = ws_sold_date_sk and d_year>=2002 and d_year<=2003
|
||||
group by ws_sold_date_sk, ws_item_sk
|
||||
order by ws_item_sk
|
||||
|
||||
insert into @catSales
|
||||
select cs_item_sk, cs_sold_date_sk,sum(cs_quantity)
|
||||
from catalog_sales_history, date_dim
|
||||
where d_date_sk = cs_sold_date_sk and d_year>=2002 and d_year<=2003
|
||||
group by cs_sold_date_sk, cs_item_sk
|
||||
order by cs_item_sk
|
||||
|
||||
select d as date_sk, i as item_sk, sum(addi) as def_amount
|
||||
from
|
||||
(select t.date_sk as d, t.item_sk as i, -1*sum(qty) as addi
|
||||
from
|
||||
(select item_sk, date_sk, qty
|
||||
from @webSales ws
|
||||
|
||||
UNION ALL
|
||||
|
||||
select item_sk, date_sk, qty
|
||||
from @catSales cs)t
|
||||
group by item_sk, date_sk
|
||||
|
||||
UNION ALL
|
||||
|
||||
select inv_date_sk as d, inv_item_sk as i, sum(inv_quantity_on_hand) as addi
|
||||
from inventory_history, date_dim
|
||||
where inv_date_sk=d_date_sk and d_year>=2002 and d_year<=2003
|
||||
group by inv_date_sk, inv_item_sk)t2
|
||||
group by d, i
|
||||
having sum(addi)<0
|
||||
order by d, i
|
||||
end
|
||||
go
|
|
@ -0,0 +1,29 @@
|
|||
create or alter procedure deleteCustomer(@user_list UserList READONLY)
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @sk int;
|
||||
|
||||
declare custCur cursor static for (select UserSk from @user_list);
|
||||
open custCur;
|
||||
fetch next from custCur into @sk;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
delete from customer where c_customer_sk = @sk;
|
||||
fetch next from custCur into @sk;
|
||||
end
|
||||
close custCur;
|
||||
deallocate custCur;
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
--invocation query
|
||||
CREATE TYPE UserList AS TABLE ( UserSk INT );
|
||||
go
|
||||
|
||||
DECLARE @UL UserList;
|
||||
INSERT @UL VALUES (5),(44),(72),(81),(126), (230), (467), (876), (1609), (3254), (78574), (435893);
|
||||
|
||||
exec dbo.deleteCustomer @UL
|
||||
go
|
|
@ -0,0 +1,22 @@
|
|||
--List all customers living in a specified city, in a given income band
|
||||
|
||||
create or alter procedure cusWithIncomeInRange(@city varchar(60), @givenIb int)
|
||||
as
|
||||
begin
|
||||
declare @cust int, @ib int, @hhd int;
|
||||
declare c1 cursor for (select c_customer_sk from customer, customer_address
|
||||
where c_current_addr_sk=ca_address_sk and ca_city = @city);
|
||||
open c1;
|
||||
fetch next from c1 into @cust;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @hhd = (select c_current_hdemo_sk from customer where c_customer_sk = @cust);
|
||||
set @ib = (select hd_income_band_sk from household_demographics where hd_demo_sk=@hhd);
|
||||
if(@ib = @givenIb)
|
||||
print (@cust);
|
||||
fetch next from c1 into @cust;
|
||||
end
|
||||
end
|
||||
|
||||
--invocation query
|
||||
exec dbo.cusWithIncomeInRange Hopewell, 2
|
|
@ -0,0 +1,21 @@
|
|||
-- Customers who spend more money online than in stores
|
||||
|
||||
create or alter procedure moreOnlineThanStore
|
||||
as
|
||||
begin
|
||||
declare @preferredChannel varchar(50);
|
||||
declare @cust int;
|
||||
declare c1 cursor static for (select c_customer_sk from customer);
|
||||
open c1;
|
||||
fetch next from c1 into @cust;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @preferredChannel = (select dbo.preferredChannel_wrtExpenditure(@cust));
|
||||
if(@preferredChannel='catalog' or @preferredChannel='web')
|
||||
print(@cust);
|
||||
fetch next from c1 into @cust;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
end
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
--new return request
|
||||
|
||||
create or alter procedure processReturn
|
||||
(@channel char(1), @item_sk int, @cust_sk int, @reason_sk int, @orderNum int, @returnQty int, @returnReason char(100),
|
||||
@retAmt decimal(7, 2), @refundAmt decimal(7, 2))
|
||||
as
|
||||
begin
|
||||
declare @curDate date = GETDATE();
|
||||
declare @dateSk int, @cnt int;
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date = @curDate);
|
||||
if(@channel='c')
|
||||
begin
|
||||
insert into catalog_returns (cr_returned_date_sk, cr_item_sk, cr_refunded_customer_sk, cr_reason_sk,
|
||||
cr_order_number, cr_return_quantity, cr_return_amt_inc_tax, cr_refunded_cash)
|
||||
values (@dateSk, @item_sk, @cust_sk, @reason_sk, @orderNum, @returnQty, @retAmt, @refundAmt);
|
||||
end
|
||||
if(@channel='w')
|
||||
begin
|
||||
insert into web_returns (wr_returned_date_sk, wr_item_sk, wr_refunded_customer_sk, wr_reason_sk,
|
||||
wr_order_number, wr_return_quantity, wr_return_amt_inc_tax, wr_refunded_cash)
|
||||
values (@dateSk, @item_sk, @cust_sk, @reason_sk, @orderNum, @returnQty , @retAmt, @refundAmt);
|
||||
end
|
||||
select @cnt = count(*) from reason where r_reason_sk=@reason_sk
|
||||
if(@cnt!=0)
|
||||
return;
|
||||
else
|
||||
begin
|
||||
declare @randomString VARCHAR(16);
|
||||
declare @reason_id varchar(16);
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @reason_id = @randomString;
|
||||
insert into reason (r_reason_sk, r_reason_id, r_reason_desc) values (@reason_sk, @reason_id, @returnReason);
|
||||
end
|
||||
end
|
||||
|
||||
--invocation query
|
||||
exec dbo.processReturn 'c', 123, 234, 50, 12, 2, 'did not like it', 245.56, 240.43
|
|
@ -0,0 +1,25 @@
|
|||
create or alter procedure setPreferredCustomers
|
||||
as
|
||||
begin
|
||||
declare @currentPref char(1);
|
||||
declare @cust int;
|
||||
|
||||
create table #prefCust (cust_sk int);
|
||||
|
||||
INSERT INTO #prefCust EXEC dbo.repeatedShoppers;
|
||||
|
||||
declare c1 cursor static for (select cust_sk from #prefCust);
|
||||
open c1;
|
||||
fetch next from c1 into @cust;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @currentPref = (select c_preferred_cust_flag from customer where c_customer_sk=@cust);
|
||||
if(@currentPref='N')
|
||||
begin
|
||||
update customer set c_preferred_cust_flag='Y' where c_customer_sk=@cust;
|
||||
end
|
||||
fetch next from c1 into @cust;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
--customers who after returning an order did not shop for atleast 6 months through the same channel(catalog).
|
||||
|
||||
create or alter procedure unsatisfiedCustomersCat
|
||||
as
|
||||
begin
|
||||
declare @cust int;
|
||||
declare @retDate date, @minDate date;
|
||||
declare @custTable table (custSk int);
|
||||
declare c1 cursor static for
|
||||
(select cr_refunded_customer_sk, d_date from catalog_returns, date_dim
|
||||
where d_date_sk = cr_returned_date_sk and cr_refunded_customer_sk is not NULL);
|
||||
open c1;
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @minDate = (select min(d_date) from catalog_sales, date_dim
|
||||
where cs_sold_date_sk=d_date_sk and cs_bill_customer_sk=@cust
|
||||
and d_date>@retDate);
|
||||
if(DATEDIFF(MONTH, @retDate, @minDate)>=6)
|
||||
begin
|
||||
insert into @custTable values (@cust);
|
||||
end
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
select * from @custTable;
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
--customers who after returning an order did not shop for atleast 6 months through the same channel(store).
|
||||
|
||||
create or alter procedure unsatisfiedCustomersStore
|
||||
as
|
||||
begin
|
||||
declare @cust int;
|
||||
declare @retDate date, @minDate date;
|
||||
declare @custTable table (custSk int);
|
||||
declare c1 cursor static for
|
||||
(select sr_customer_sk, d_date from store_returns, date_dim
|
||||
where d_date_sk = sr_returned_date_sk and sr_customer_sk is not NULL);
|
||||
open c1;
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @minDate = (select min(d_date) from store_sales, date_dim
|
||||
where ss_sold_date_sk=d_date_sk and ss_customer_sk=@cust
|
||||
and d_date>@retDate);
|
||||
if(DATEDIFF(MONTH, @retDate, @minDate)>=6)
|
||||
begin
|
||||
insert into @custTable values (@cust);
|
||||
end
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
select * from @custTable;
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
create procedure getCustomerInfo(@custKey int)
|
||||
as
|
||||
begin
|
||||
select c_salutation, c_first_name, c_last_name, c_birth_year, c_email_address,
|
||||
ca_street_number, ca_street_name, ca_suite_number, ca_city, ca_county, ca_country
|
||||
from customer, customer_address
|
||||
where c_current_addr_sk = ca_address_sk
|
||||
and c_customer_sk = @custKey
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
--customers who after returning an order did not shop for atleast 6 months through the same channel(web).
|
||||
|
||||
create or alter procedure unsatisfiedCustomersWeb
|
||||
as
|
||||
begin
|
||||
declare @cust int;
|
||||
declare @retDate date, @minDate date;
|
||||
declare @custTable table (custSk int);
|
||||
declare c1 cursor static for
|
||||
(select wr_refunded_customer_sk, d_date from web_returns, date_dim
|
||||
where d_date_sk = wr_returned_date_sk and wr_refunded_customer_sk is not NULL);
|
||||
open c1;
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @minDate = (select min(d_date) from web_sales, date_dim
|
||||
where ws_sold_date_sk=d_date_sk and ws_bill_customer_sk=@cust
|
||||
and d_date>@retDate);
|
||||
if(DATEDIFF(MONTH, @retDate, @minDate)>=6)
|
||||
begin
|
||||
insert into @custTable values (@cust);
|
||||
end
|
||||
fetch next from c1 into @cust, @retDate;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
select * from @custTable;
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
create or alter procedure defectiveItemSellers
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @sellerTable table (manufact int , cnt int);
|
||||
declare @manu int, @cnt int;
|
||||
|
||||
declare c1 cursor for
|
||||
select i_manufact_id, count(*) cnt
|
||||
from web_returns, item, reason
|
||||
where wr_item_sk = i_item_sk and wr_reason_sk = r_reason_sk and
|
||||
wr_reason_sk is not NULL and wr_item_sk is not NULL and
|
||||
i_manufact_id is not NULL and
|
||||
(r_reason_desc = 'Parts missing' or r_reason_desc = 'Not working any more' or r_reason_desc = 'reason 38')
|
||||
group by i_manufact_id;
|
||||
|
||||
open c1;
|
||||
fetch next from c1 into @manu, @cnt;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
if(@cnt>=20)
|
||||
insert into @sellerTable values (@manu, @cnt);
|
||||
fetch next from c1 into @manu, @cnt;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
select * from @sellerTable;
|
||||
end
|
|
@ -0,0 +1,40 @@
|
|||
--process cancellation of an order from catalog and record reason in the reason table.
|
||||
create or alter procedure catalogOrderCancellation (@item_sk int, @orderNo int, @reasonId int)
|
||||
as
|
||||
begin
|
||||
declare @shipDate date, @curDate date;
|
||||
declare @dateDiff int, @reasonSk int;
|
||||
declare @id VARCHAR(16);
|
||||
|
||||
if not exists (select * from catalog_sales where cs_item_sk=@item_sk and cs_order_number=@orderNo)
|
||||
begin
|
||||
RAISERROR ('invalid order', 16, 10); --no such order exists.
|
||||
return;
|
||||
end
|
||||
|
||||
set @shipDate = (select d_date from catalog_sales, date_dim
|
||||
where cs_ship_date_sk=d_date_sk and cs_item_sk=@item_sk and cs_order_number=@orderNo);
|
||||
set @dateDiff = DATEDIFF(day, @shipDate, GETDATE());
|
||||
if(@dateDiff>=0) --shipdate<=today's date
|
||||
begin
|
||||
RAISERROR ('Item already shipped and cannot be cancelled. Try returning instead.',16,10);
|
||||
return;
|
||||
end
|
||||
else
|
||||
begin
|
||||
delete from catalog_sales where cs_item_sk=@item_sk and cs_order_number=@orderNo;
|
||||
if exists (select * from reason where r_reason_sk=@reasonId)
|
||||
return;
|
||||
else
|
||||
begin
|
||||
set @reasonSk = (select max(r_reason_sk)+1 from reason);
|
||||
EXEC dbo.CreateRandomString @id OUTPUT;
|
||||
--set @id = 'ABCDEFSDRGFTDVGF';
|
||||
insert into reason (r_reason_sk, r_reason_id) values (@reasonSk, @id);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--invocation query
|
||||
exec dbo.catalogOrderCancellation 7273, 169579, 57
|
||||
go
|
|
@ -0,0 +1,36 @@
|
|||
CREATE or alter TYPE ManagerType
|
||||
AS TABLE
|
||||
(man_name varchar(40) unique, numClosed int);
|
||||
GO
|
||||
|
||||
CREATE PROCEDURE ShutOldCallCenterInCounty
|
||||
@county varchar(30)
|
||||
AS
|
||||
BEGIN
|
||||
declare @closedManager ManagerType;
|
||||
declare @cc_sk int , @openDate int, @numEmpl int;
|
||||
declare @manager varchar(40)= '', @city varchar(60) = '', @year int, @numCl int;
|
||||
declare c1 cursor static for (select cc_call_center_sk, cc_open_date_sk, cc_employees, cc_manager, cc_city
|
||||
from call_center where cc_county=@county);
|
||||
open c1;
|
||||
fetch next from c1 into @cc_sk, @openDate, @numEmpl, @manager, @city
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
set @year = (select d_year from date_dim where d_date_sk = @openDate);
|
||||
if(@year<=1998 and @numEmpl<400)
|
||||
begin
|
||||
if not exists (select * from @closedManager where man_name = @manager)
|
||||
insert into @closedManager values (@manager, 1);
|
||||
else
|
||||
begin
|
||||
set @numCl = (select numClosed from @closedManager where man_name = @manager);
|
||||
insert into @closedManager values (@manager, @numCl+1);
|
||||
end
|
||||
delete from call_center where cc_call_center_sk = @cc_sk; --delete this old, small CC
|
||||
end
|
||||
fetch next from c1 into @cc_sk, @openDate, @numEmpl, @city;
|
||||
end
|
||||
declare @tableName varchar(50) = '@closedManager';
|
||||
select * from dbo.unemployedManagers(@closedManager);
|
||||
END;
|
||||
GO
|
|
@ -0,0 +1,36 @@
|
|||
-- Track the sale of a particular item through catalog for 2 consecutive years.
|
||||
create or alter procedure trackSale_cat (@item int)
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
declare @compareTable table (mnth_cmpr int, sale_2001 int, sale_2002 int);
|
||||
declare @firstYr table (mnth int, sale_01 int);
|
||||
declare @secYr table (mnth int, sale_02 int);
|
||||
declare @month int, @sale int;
|
||||
|
||||
insert into @firstYr
|
||||
select d_moy, count(*) as sl_01 from catalog_sales, date_dim
|
||||
where cs_sold_date_sk = d_date_sk and d_year = 2001 and cs_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @secYr
|
||||
select d_moy, count(*) as sl_02 from catalog_sales, date_dim
|
||||
where cs_sold_date_sk = d_date_sk and d_year = 2002 and cs_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @compareTable(mnth_cmpr, sale_2001) select * from @firstYr;
|
||||
|
||||
declare c1 cursor for select * from @secYr;
|
||||
open c1;
|
||||
fetch next from c1 into @month, @sale;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update @compareTable set sale_2002 = @sale where mnth_cmpr= @month;
|
||||
fetch next from c1 into @month, @sale;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
|
||||
select * from @compareTable order by mnth_cmpr;
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
--Track the sale of a particular item through store for 2 consecutive years.
|
||||
create or alter procedure trackSale_store (@item int)
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
declare @compareTable table (mnth_cmpr int, sale_2001 int, sale_2002 int);
|
||||
declare @firstYr table (mnth int, sale_01 int);
|
||||
declare @secYr table (mnth int, sale_02 int);
|
||||
declare @month int, @sale int;
|
||||
|
||||
insert into @firstYr
|
||||
select d_moy, count(*) as sl_01 from store_sales, date_dim
|
||||
where ss_sold_date_sk = d_date_sk and d_year = 2001 and ss_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @secYr
|
||||
select d_moy, count(*) as sl_02 from store_sales, date_dim
|
||||
where ss_sold_date_sk = d_date_sk and d_year = 2002 and ss_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @compareTable(mnth_cmpr, sale_2001) select * from @firstYr;
|
||||
|
||||
declare c1 cursor for select * from @secYr;
|
||||
open c1;
|
||||
fetch next from c1 into @month, @sale;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update @compareTable set sale_2002 = @sale where mnth_cmpr= @month;
|
||||
fetch next from c1 into @month, @sale;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
|
||||
select * from @compareTable order by mnth_cmpr;
|
||||
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
-- Track the sale of a particular item through web for 2 consecutive years.
|
||||
create or alter procedure trackSale_web (@item int)
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
declare @compareTable table (mnth_cmpr int, sale_2001 int, sale_2002 int);
|
||||
declare @firstYr table (mnth int, sale_01 int);
|
||||
declare @secYr table (mnth int, sale_02 int);
|
||||
declare @month int, @sale int;
|
||||
|
||||
insert into @firstYr
|
||||
select d_moy, count(*) as sl_01 from web_sales, date_dim
|
||||
where ws_sold_date_sk = d_date_sk and d_year = 2001 and ws_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @secYr
|
||||
select d_moy, count(*) as sl_02 from web_sales, date_dim
|
||||
where ws_sold_date_sk = d_date_sk and d_year = 2002 and ws_item_sk = @item
|
||||
group by d_moy;
|
||||
|
||||
insert into @compareTable(mnth_cmpr, sale_2001) select * from @firstYr;
|
||||
|
||||
declare c1 cursor for select * from @secYr;
|
||||
open c1;
|
||||
fetch next from c1 into @month, @sale;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
update @compareTable set sale_2002 = @sale where mnth_cmpr= @month;
|
||||
fetch next from c1 into @month, @sale;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
|
||||
select * from @compareTable order by mnth_cmpr;
|
||||
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
--process cancellation of an order fromm web and record reason in the reason table.
|
||||
|
||||
create or alter procedure webOrderCancellation (@item_sk int, @orderNo int, @reasonId int)
|
||||
as
|
||||
begin
|
||||
declare @soldDate date, @curDate date;
|
||||
declare @dateDiff int, @reasonSk int;
|
||||
declare @id VARCHAR(16);
|
||||
|
||||
if not exists (select * from web_sales where ws_item_sk=@item_sk and ws_order_number=@orderNo)
|
||||
begin
|
||||
RAISERROR ('invalid order', 16, 10);
|
||||
return;
|
||||
end
|
||||
|
||||
set @soldDate = (select d_date from web_sales, date_dim
|
||||
where ws_sold_date_sk=d_date_sk and ws_item_sk=@item_sk and ws_order_number=@orderNo);
|
||||
set @dateDiff = DATEDIFF(day, @soldDate, GETDATE());
|
||||
if(@dateDiff>30)
|
||||
begin
|
||||
RAISERROR ('Item not eligible for return',16,10);
|
||||
return;
|
||||
end
|
||||
else
|
||||
begin
|
||||
delete from web_sales where ws_item_sk=@item_sk and ws_order_number=@orderNo;
|
||||
if exists (select * from reason where r_reason_sk=@reasonId)
|
||||
return;
|
||||
else
|
||||
begin
|
||||
set @reasonSk = (select max(r_reason_sk)+1 from reason);
|
||||
EXEC dbo.CreateRandomString @id OUTPUT;
|
||||
insert into reason (r_reason_sk, r_reason_id) values (@reasonSk, @id);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
--extract ship mode information for a given order.
|
||||
|
||||
create or alter procedure getShipMode(@item int, @order int, @channel char(1))
|
||||
as
|
||||
begin
|
||||
if(@channel is NULL)
|
||||
begin
|
||||
if exists (select * from web_sales where ws_item_sk=@item and ws_order_number=@order)
|
||||
begin
|
||||
if exists (select * from catalog_sales where cs_item_sk=@item and cs_order_number=@order)
|
||||
RAISERROR('ambiguous channel for this order', 10, 16);
|
||||
else
|
||||
select sm_type from web_sales, ship_mode where ws_item_sk=@item and ws_order_number=@order and ws_ship_mode_sk = sm_ship_mode_sk;
|
||||
end
|
||||
else
|
||||
begin
|
||||
if not exists (select * from catalog_sales where cs_item_sk=@item and cs_order_number=@order)
|
||||
RAISERROR('Order not found in any chanel', 10, 16);
|
||||
else
|
||||
select sm_type from catalog_sales, ship_mode where cs_item_sk=@item and cs_order_number=@order and cs_ship_mode_sk = sm_ship_mode_sk;
|
||||
end
|
||||
end
|
||||
|
||||
else if (@channel='w')
|
||||
begin
|
||||
if not exists (select * from web_sales where ws_item_sk=@item and ws_order_number=@order)
|
||||
RAISERROR('order not foud in web', 10, 16);
|
||||
else
|
||||
select sm_type from web_sales, ship_mode where ws_item_sk=@item and ws_order_number=@order and ws_ship_mode_sk = sm_ship_mode_sk;
|
||||
end
|
||||
|
||||
else if (@channel='c')
|
||||
begin
|
||||
if not exists (select * from catalog_sales where cs_item_sk=@item and cs_order_number=@order)
|
||||
RAISERROR('order not foud in catalog', 10, 16);
|
||||
else
|
||||
select sm_type from catalog_sales, ship_mode where cs_item_sk=@item and cs_order_number=@order and cs_ship_mode_sk = sm_ship_mode_sk;
|
||||
end
|
||||
end
|
||||
|
||||
--invocation example
|
||||
exec dbo.getShipMode 31714, 485252, NULL
|
||||
go
|
|
@ -0,0 +1,53 @@
|
|||
--new catalog sale record.
|
||||
create or alter procedure newCatalogSale(@item_sk int, @cust_sk int, @custname char(10), @page_sk int, @promo_sk int,
|
||||
@qty int, @listPrice decimal(7, 2), @sp decimal(7, 2), @npaid decimal(7, 2),
|
||||
@profit decimal(7, 2), @streetnum char(10), @suite char(10), @city varchar(60),
|
||||
@county varchar(30), @state char(2), @zip char(10))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @curDate date;
|
||||
declare @dateSk int, @hr int, @mn int, @sc int, @timeSk int, @addr_sk int, @ordr int;
|
||||
declare @curTime time;
|
||||
declare @adr_id varchar(16), @cus_id varchar(16);
|
||||
set @hr = (select DATEPART(hour, GETDATE()));
|
||||
set @mn = (select DATEPART(MINUTE, GETDATE()));
|
||||
set @sc = (select DATEPART(SECOND, GETDATE()));
|
||||
set @curDate = GETDATE();
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date=@curDate);
|
||||
set @timeSk = (select t_time_sk from time_dim where t_hour= @hr and t_minute=@mn and t_second=@sc);
|
||||
|
||||
|
||||
--this is a new customer
|
||||
if(@cust_sk is NULL)
|
||||
begin
|
||||
print ('New customer')
|
||||
set @cust_sk = (select max(c_customer_sk)+1 from customer);
|
||||
set @addr_sk = (select max(ca_address_sk)+1 from customer_address);
|
||||
set @ordr = (select max(cs_order_number)+1 from dbo.catalog_sales where cs_item_sk=@item_sk)
|
||||
declare @randomString VARCHAR(16);
|
||||
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @adr_id = @randomString;
|
||||
|
||||
insert into customer_address(ca_address_sk, ca_address_id, ca_street_number, ca_suite_number,
|
||||
ca_city, ca_county, ca_state, ca_zip)
|
||||
values (@addr_sk, @adr_id, @streetnum, @suite, @city, @county, @state, @zip);
|
||||
|
||||
set @randomString= '';
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @cus_id = @randomString;
|
||||
|
||||
insert into customer(c_customer_sk, c_customer_id, c_first_name, c_current_addr_sk)
|
||||
values (@cust_sk, @cus_id, @custname, @addr_sk);
|
||||
end
|
||||
|
||||
else
|
||||
begin
|
||||
set @addr_sk = (select c_current_addr_sk from customer where c_customer_sk=@cust_sk)
|
||||
end
|
||||
|
||||
insert into catalog_sales (cs_sold_date_sk, cs_sold_time_sk, cs_item_sk, cs_bill_customer_sk, cs_ship_addr_sk, cs_catalog_page_sk,
|
||||
cs_promo_sk, cs_order_number, cs_quantity, cs_list_price, cs_sales_price, cs_net_paid, cs_net_profit)
|
||||
values (@dateSk, @timeSk, @item_sk, @cust_sk, @addr_sk, @page_sk, @promo_sk, @ordr, @qty, @listPrice, @sp, @npaid, @profit);
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
create or alter procedure getImmigrantCustomers
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select c_customer_sk, c_birth_country, ca_state as currentState, cd_gender, cd_marital_status,
|
||||
cd_education_status, ib_lower_bound, ib_upper_bound,
|
||||
cd_credit_rating, cd_dep_count
|
||||
from customer, customer_address, household_demographics, income_band, customer_demographics
|
||||
where c_current_addr_sk = ca_address_sk
|
||||
and c_birth_country != ca_country
|
||||
and c_current_hdemo_sk=hd_demo_sk
|
||||
and hd_income_band_sk = ib_income_band_sk
|
||||
and cd_demo_sk = c_current_cdemo_sk
|
||||
and c_customer_sk is not NULL;
|
||||
end
|
|
@ -0,0 +1,56 @@
|
|||
--new store sale record.
|
||||
create or alter procedure newStoreSale(@item_sk int, @cust_sk int, @custname char(10), @str_sk int, @promo_sk int,
|
||||
@qty int, @listPrice decimal(7, 2), @sp decimal(7, 2), @npaid decimal(7, 2),
|
||||
@profit decimal(7, 2), @streetnum char(10), @suite char(10), @city varchar(60),
|
||||
@county varchar(30), @state char(2), @zip char(10))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @curDate date;
|
||||
declare @dateSk int, @hr int, @mn int, @sc int, @timeSk int, @addr_sk int, @tckt int;
|
||||
declare @curTime time;
|
||||
declare @adr_id varchar(16), @cus_id varchar(16);
|
||||
set @hr = (select DATEPART(hour, GETDATE()));
|
||||
set @mn = (select DATEPART(MINUTE, GETDATE()));
|
||||
set @sc = (select DATEPART(SECOND, GETDATE()));
|
||||
set @curDate = GETDATE();
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date=@curDate);
|
||||
|
||||
set @timeSk = (select t_time_sk from time_dim where t_hour= @hr and t_minute=@mn and t_second=@sc);
|
||||
|
||||
--this is a new customer
|
||||
if(@cust_sk is NULL)
|
||||
begin
|
||||
print('new customer')
|
||||
set @cust_sk = (select max(c_customer_sk)+1 from customer);
|
||||
set @addr_sk = (select max(ca_address_sk)+1 from customer_address);
|
||||
set @tckt = (select max(ss_ticket_number)+1 from store_sales where ss_item_sk=@item_sk);
|
||||
declare @randomString VARCHAR(16)='';
|
||||
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @adr_id = @randomString;
|
||||
|
||||
insert into customer_address(ca_address_sk, ca_address_id, ca_street_number, ca_suite_number,
|
||||
ca_city, ca_county, ca_state, ca_zip)
|
||||
values (@addr_sk, @adr_id, @streetnum, @suite, @city, @county, @state, @zip);
|
||||
|
||||
set @randomString ='';
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @cus_id = @randomString;
|
||||
|
||||
insert into customer(c_customer_sk, c_customer_id, c_first_name, c_current_addr_sk)
|
||||
values (@cust_sk, @cus_id, @custname, @addr_sk);
|
||||
end
|
||||
|
||||
else
|
||||
begin
|
||||
set @addr_sk = (select c_current_addr_sk from customer where c_customer_sk=@cust_sk)
|
||||
end
|
||||
|
||||
insert into store_sales (ss_sold_date_sk, ss_sold_time_sk, ss_item_sk, ss_customer_sk, ss_addr_sk, ss_store_sk,
|
||||
ss_promo_sk, ss_ticket_number, ss_quantity, ss_list_price, ss_sales_price, ss_net_paid, ss_net_profit)
|
||||
values (@dateSk, @timeSk, @item_sk, @cust_sk, @addr_sk, @str_sk, @promo_sk, @tckt, @qty, @listPrice, @sp, @npaid, @profit);
|
||||
end
|
||||
|
||||
--invocation example
|
||||
exec dbo.newStoreSale 2, NULL, 'nyName', 1, 3, 2, 24.56, 23.56, 25, 4, '12', 'abcSuites', 'myCity', 'myCounty', 'myState', '302893';
|
|
@ -0,0 +1,50 @@
|
|||
--new web sale record.
|
||||
create procedure newWebSale(@item_sk int, @cust_sk int, @custname char(10), @page_sk int, @promo_sk int,
|
||||
@qty int, @listPrice decimal(7, 2), @sp decimal(7, 2), @npaid decimal(7, 2),
|
||||
@profit decimal(7, 2), @streetnum char(10), @suite char(10), @city varchar(60),
|
||||
@county varchar(30), @state char(2), @zip char(10))
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
declare @curDate date;
|
||||
declare @dateSk int, @hr int, @mn int, @sc int, @timeSk int, @addr_sk int;
|
||||
declare @curTime time;
|
||||
declare @adr_id varchar(16), @cus_id varchar(16);
|
||||
set @hr = (select DATEPART(hour, GETDATE()));
|
||||
set @mn = (select DATEPART(MINUTE, GETDATE()));
|
||||
set @sc = (select DATEPART(SECOND, GETDATE()));
|
||||
set @curDate = GETDATE();
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date=@curDate);
|
||||
set @timeSk = (select t_time_sk from time_dim where t_hour= @hr and t_minute=@mn and t_second=@sc);
|
||||
|
||||
--this is a new customer
|
||||
if(@cust_sk=NULL)
|
||||
begin
|
||||
set @cust_sk = (select max(c_customer_sk)+1 from customer);
|
||||
set @addr_sk = (select max(ca_address_sk)+1 from customer_address);
|
||||
DECLARE @chars VARCHAR(26) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
declare @randomString VARCHAR(16);
|
||||
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @adr_id = @randomString;
|
||||
|
||||
insert into customer_address(ca_address_sk, ca_address_id, ca_street_number, ca_suite_number,
|
||||
ca_city, ca_county, ca_state, ca_zip)
|
||||
values (@addr_sk, @adr_id, @streetnum, @suite, @city, @county, @state, @zip);
|
||||
|
||||
EXEC [dbo].[CreateRandomString] @randomString OUTPUT;
|
||||
SELECT @cus_id = @randomString;
|
||||
|
||||
insert into customer(c_customer_sk, c_customer_id, c_first_name, c_current_addr_sk)
|
||||
values (@cust_sk, @cus_id, @custname, @addr_sk);
|
||||
end
|
||||
|
||||
else
|
||||
begin
|
||||
set @addr_sk = (select c_current_addr_sk from customer where c_customer_sk=@cust_sk)
|
||||
end
|
||||
|
||||
insert into web_sales (ws_sold_date_sk, ws_sold_time_sk, ws_item_sk, ws_bill_customer_sk, ws_ship_addr_sk, ws_web_page_sk,
|
||||
ws_promo_sk, ws_quantity, ws_list_price, ws_sales_price, ws_net_paid, ws_net_profit)
|
||||
values (@dateSk, @timeSk, @item_sk, @cust_sk, @addr_sk, @page_sk, @promo_sk, @qty, @listPrice, @sp, @npaid, @profit);
|
||||
end
|
|
@ -0,0 +1,36 @@
|
|||
create or alter procedure custTotalLoss(@returnReason char(100))
|
||||
as
|
||||
begin
|
||||
declare @orderNo int, @item int;
|
||||
declare @reasonSk int;
|
||||
declare @soldAmt decimal(7, 2), @retCredit decimal(7, 2);
|
||||
declare @totalCustLoss decimal(15, 0);
|
||||
|
||||
set @totalCustLoss=0;
|
||||
set @reasonSk = (select r_reason_sk from reason where r_reason_desc=@returnReason);
|
||||
declare c1 cursor for (select wr_order_number, wr_item_sk, wr_refunded_cash from web_returns where wr_reason_sk=1);
|
||||
open c1;
|
||||
fetch next from c1 into @orderNo, @item, @retCredit;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @soldAmt = (select ws_net_paid_inc_ship_tax from web_sales where ws_order_number = @orderNo and ws_item_sk=@item);
|
||||
set @totalCustLoss += @soldAmt - @retCredit ;
|
||||
fetch next from c1 into @orderNo, @item, @retCredit;
|
||||
end
|
||||
close c1;
|
||||
|
||||
declare c2 cursor for (select cr_order_number, cr_item_sk, cr_refunded_cash from catalog_returns where cr_reason_sk=1);
|
||||
open c2;
|
||||
fetch next from c2 into @orderNo, @item, @retCredit;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @soldAmt = (select cs_net_paid_inc_ship_tax from catalog_sales where cs_order_number = @orderNo and cs_item_sk=@item);
|
||||
set @totalCustLoss += @soldAmt - @retCredit;
|
||||
fetch next from c2 into @orderNo, @item, @retCredit;
|
||||
end
|
||||
select @totalCustLoss as totalLossToCustomer;
|
||||
|
||||
close c2;
|
||||
deallocate c1;
|
||||
deallocate c2;
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
create or alter procedure AccessItemQuality
|
||||
as
|
||||
begin
|
||||
declare @mostReturnItem table(itemNo int, manufactId int);
|
||||
insert into @mostReturnItem select * from dbo.MaxRetunItems();
|
||||
delete from item where i_item_sk in (select itemNo from @mostReturnItem); --these items deleted
|
||||
update item set i_item_desc = 'HIGH RISH ITEM' where i_manufact_id in (select manufactId from @mostReturnItem);
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
create or alter procedure getNativeCustomers
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select c_customer_sk, ca_state, cd_gender, cd_marital_status,
|
||||
cd_education_status, ib_lower_bound, ib_upper_bound,
|
||||
cd_credit_rating, cd_dep_count
|
||||
from customer, customer_address, household_demographics, income_band, customer_demographics
|
||||
where c_current_addr_sk = ca_address_sk
|
||||
and c_birth_country=ca_country
|
||||
and c_current_hdemo_sk=hd_demo_sk
|
||||
and hd_income_band_sk = ib_income_band_sk
|
||||
and cd_demo_sk = c_current_cdemo_sk
|
||||
and c_customer_sk is not NULL;
|
||||
end
|
|
@ -0,0 +1,14 @@
|
|||
--cusotmers belonging to a low income band and who made purchases of more than a given amount.
|
||||
|
||||
CREATE PROCEDURE lowIncomeCustomerWithHighPurchaseAmount(@amount decimal(7, 2))
|
||||
AS
|
||||
BEGIN
|
||||
set nocount on;
|
||||
|
||||
select distinct ss_customer_sk, ib_income_band_sk
|
||||
from store_sales_history, household_demographics, income_band
|
||||
where ss_hdemo_sk = hd_demo_sk
|
||||
and hd_income_band_sk = ib_income_band_sk
|
||||
and ss_net_paid_inc_tax>@amount
|
||||
and ib_income_band_sk<=4
|
||||
END
|
|
@ -0,0 +1,18 @@
|
|||
--Customers who have shopped in stores from more than 1 state in a given year and month
|
||||
create or alter procedure multiStateShoppers (@year int, @moy int)
|
||||
as
|
||||
begin
|
||||
declare @custTable Table (cust int, cnt int);
|
||||
|
||||
insert into @custTable
|
||||
select ss_customer_sk, count(*) from
|
||||
(select distinct ss_customer_sk, s_state
|
||||
from store_sales_history, date_dim, store
|
||||
where ss_sold_date_sk = d_date_sk and ss_store_sk = s_store_sk
|
||||
and d_year = @year and d_moy =@moy and ss_customer_sk is not NULL and ss_store_sk is not NULL
|
||||
)t
|
||||
group by ss_customer_sk
|
||||
order by ss_customer_sk;
|
||||
|
||||
select * from @custTable where cnt>1 order by cust;
|
||||
end
|
|
@ -0,0 +1,12 @@
|
|||
--how may people shopped from a web_page
|
||||
create procedure popularWebPages
|
||||
as
|
||||
begin
|
||||
set nocount on;
|
||||
|
||||
select top 100 ws_web_page_sk,count(*) as cnt from web_sales_history, web_page
|
||||
where ws_web_page_sk = wp_web_page_sk
|
||||
group by ws_web_page_sk
|
||||
order by cnt desc
|
||||
end
|
||||
go
|
|
@ -0,0 +1,92 @@
|
|||
--Output the late delivery statistics of each of the 2 delivery channels
|
||||
|
||||
Create or Alter Function ChannelWiseLateDeliveryStatistics(@channel char(1), @year integer)
|
||||
RETURNS @deliveryStats TABLE(warehouseName varchar(20), shipMode char(30), thirty_days int, sixty_days int,
|
||||
ninety_days int, oneTwenty_days int, veryLate int)
|
||||
begin
|
||||
if(@channel='c')
|
||||
begin
|
||||
INSERT into @deliveryStats
|
||||
SELECT w_warehouse_name, sm_type,
|
||||
Sum(CASE
|
||||
WHEN ( cs_ship_date_sk - cs_sold_date_sk <= 30 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS thirty_days,
|
||||
Sum(CASE
|
||||
WHEN ( cs_ship_date_sk - cs_sold_date_sk > 30 )
|
||||
AND ( cs_ship_date_sk - cs_sold_date_sk <= 60 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS sixty_days,
|
||||
Sum(CASE
|
||||
WHEN ( cs_ship_date_sk - cs_sold_date_sk > 60 )
|
||||
AND ( cs_ship_date_sk - cs_sold_date_sk <= 90 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS ninety_days,
|
||||
Sum(CASE
|
||||
WHEN ( cs_ship_date_sk - cs_sold_date_sk > 90 )
|
||||
AND ( cs_ship_date_sk - cs_sold_date_sk <= 120 ) THEN
|
||||
1
|
||||
ELSE 0
|
||||
END) AS oneTwenty_days,
|
||||
Sum(CASE
|
||||
WHEN ( cs_ship_date_sk - cs_sold_date_sk > 120 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS veryLateDelivery
|
||||
|
||||
FROM catalog_sales_history, warehouse, ship_mode, date_dim
|
||||
WHERE d_year BETWEEN @year AND @year+5
|
||||
AND cs_ship_date_sk = d_date_sk
|
||||
AND cs_warehouse_sk = w_warehouse_sk
|
||||
AND cs_ship_mode_sk = sm_ship_mode_sk
|
||||
GROUP BY w_warehouse_name,
|
||||
sm_type
|
||||
end
|
||||
|
||||
else if(@channel='w')
|
||||
begin
|
||||
INSERT into @deliveryStats
|
||||
SELECT w_warehouse_name, sm_type,
|
||||
Sum(CASE
|
||||
WHEN ( ws_ship_date_sk - ws_sold_date_sk <= 30 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS thirty_days,
|
||||
Sum(CASE
|
||||
WHEN ( ws_ship_date_sk - ws_sold_date_sk > 30 )
|
||||
AND ( ws_ship_date_sk - ws_sold_date_sk <= 60 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS sixty_days,
|
||||
Sum(CASE
|
||||
WHEN ( ws_ship_date_sk - ws_sold_date_sk > 60 )
|
||||
AND ( ws_ship_date_sk - ws_sold_date_sk <= 90 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS ninety_days,
|
||||
Sum(CASE
|
||||
WHEN ( ws_ship_date_sk - ws_sold_date_sk > 90 )
|
||||
AND ( ws_ship_date_sk - ws_sold_date_sk <= 120 ) THEN
|
||||
1
|
||||
ELSE 0
|
||||
END) AS oneTwenty_days,
|
||||
Sum(CASE
|
||||
WHEN ( ws_ship_date_sk - ws_sold_date_sk > 120 ) THEN 1
|
||||
ELSE 0
|
||||
END) AS veryLateDelivery
|
||||
FROM web_sales_history,
|
||||
warehouse,
|
||||
ship_mode,
|
||||
date_dim
|
||||
WHERE d_year BETWEEN @year AND @year+5
|
||||
AND ws_ship_date_sk = d_date_sk
|
||||
AND ws_warehouse_sk = w_warehouse_sk
|
||||
AND ws_ship_mode_sk = sm_ship_mode_sk
|
||||
GROUP BY w_warehouse_name,
|
||||
sm_type
|
||||
end
|
||||
|
||||
return;
|
||||
end
|
||||
go
|
||||
|
||||
--calling : select dbo.ChannelWiseLateDeliveryStatistics('c', 1998);
|
||||
select warehouseName , shipMode , thirty_days , sixty_days ,
|
||||
ninety_days , oneTwenty_days , veryLate
|
||||
from dbo.ChannelWiseLateDeliveryStatistics('c', 1998)
|
|
@ -0,0 +1,84 @@
|
|||
--Total of high(>1000) return amounts for return reasons pertaining to price issues for all three channels. (the the current non-history cycle)
|
||||
|
||||
create or alter function dbo.highReturnReasons()
|
||||
returns @return_reason TABLE(channel char(1), reason nvarchar(max), totalAmount decimal(15, 2))
|
||||
as
|
||||
begin
|
||||
declare @reason_store nvarchar(max);
|
||||
declare @total_store decimal(15, 2);
|
||||
declare @reason_web nvarchar(max);
|
||||
declare @total_web decimal(15, 2);
|
||||
declare @reason_catalog nvarchar(max);
|
||||
declare @total_catalog decimal(15, 2);
|
||||
|
||||
declare @return_amt decimal(15, 2);
|
||||
declare @desc nvarchar(max);
|
||||
|
||||
set @total_store = 0.0;
|
||||
set @reason_store='';
|
||||
set @total_catalog = 0.0;
|
||||
set @reason_catalog = '';
|
||||
set @total_web = 0.0;
|
||||
set @reason_web = '';
|
||||
|
||||
declare c1 cursor static for (select sr_return_amt, r_reason_desc from store_returns, reason
|
||||
where sr_reason_sk = r_reason_sk and sr_return_amt>1000.0)
|
||||
|
||||
open c1;
|
||||
fetch next from c1 into @return_amt, @desc;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
if(@desc like '%price%')
|
||||
begin
|
||||
set @total_store+=@return_amt;
|
||||
set @reason_store+=@desc;
|
||||
end
|
||||
fetch next from c1 into @return_amt, @desc;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
insert into @return_reason values ('s', @reason_store, @total_store);
|
||||
|
||||
declare c2 cursor static for (select wr_return_amt, r_reason_desc from web_returns, reason
|
||||
where wr_reason_sk = r_reason_sk and wr_return_amt>1000.0)
|
||||
|
||||
open c2;
|
||||
fetch next from c2 into @return_amt, @desc;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
if(@desc like '%price%')
|
||||
begin
|
||||
set @total_web+=@return_amt;
|
||||
set @reason_web+=@desc;
|
||||
end
|
||||
fetch next from c2 into @return_amt, @desc;
|
||||
end
|
||||
close c2;
|
||||
deallocate c2;
|
||||
insert into @return_reason values ('w', @reason_web, @total_web);
|
||||
|
||||
declare c3 cursor static for (select cr_return_amount, r_reason_desc from catalog_returns, reason
|
||||
where cr_reason_sk = r_reason_sk and cr_return_amount>1000.0)
|
||||
|
||||
open c3;
|
||||
fetch next from c3 into @return_amt, @desc;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
if(@desc like '%price%')
|
||||
begin
|
||||
set @total_catalog+=@return_amt;
|
||||
set @reason_catalog+=@desc;
|
||||
end
|
||||
fetch next from c3 into @return_amt, @desc;
|
||||
end
|
||||
close c3;
|
||||
deallocate c3;
|
||||
|
||||
insert into @return_reason values ('c', @reason_catalog, @total_catalog);
|
||||
return;
|
||||
end
|
||||
go
|
||||
|
||||
|
||||
select channel, reason, totalAmount
|
||||
from dbo.highReturnReasons()
|
|
@ -0,0 +1,33 @@
|
|||
--Compute store sales net profit ranking by state and city for a given year
|
||||
--and determine the five most profitable states.
|
||||
|
||||
Create or Alter Function dbo.maxProfitStates(@givenYear int)
|
||||
RETURNS @profitRanking TABLE(state char(2), city varchar(60), profit decimal(15, 2))
|
||||
as
|
||||
begin
|
||||
insert into @profitRanking
|
||||
select top 5 s_state, s_city, sum(ss_net_profit) as totalProfit
|
||||
from store_sales_history, store, date_dim
|
||||
where ss_store_sk = s_store_sk
|
||||
and ss_sold_date_sk = d_date_sk
|
||||
and d_year = @givenYear
|
||||
group by s_state, s_city
|
||||
order by totalProfit desc;
|
||||
|
||||
return;
|
||||
end
|
||||
go
|
||||
|
||||
--invocation query
|
||||
SELECT date_dim.d_year,
|
||||
Results.state,
|
||||
Results.city,
|
||||
Results.profit
|
||||
FROM date_dim
|
||||
OUTER APPLY dbo.maxProfitStates(d_year) AS Results
|
||||
where results.profit IS NOT NULL
|
||||
group by d_year, Results.state, Results.city,
|
||||
Results.profit
|
||||
order by d_year desc
|
||||
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
--highest profit earning promos on the web
|
||||
|
||||
create or alter function bestPromosWeb()
|
||||
returns @bestPromoWeb TABLE(promo_sk int, profit_web decimal(15,2))
|
||||
as
|
||||
begin
|
||||
|
||||
insert into @bestPromoWeb
|
||||
select top 5 ws_promo_sk, sum(ws_net_profit) as posProfit from web_sales_history
|
||||
where ws_net_profit>0
|
||||
and ws_promo_sk is not NULL
|
||||
group by ws_promo_sk
|
||||
order by posProfit desc
|
||||
|
||||
return;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,18 @@
|
|||
--highest profit earning promos on the catalog
|
||||
|
||||
create or alter function bestPromosCatalog()
|
||||
returns @bestPromoCat TABLE(promo_sk int, profit_cat decimal(15,2))
|
||||
as
|
||||
begin
|
||||
|
||||
insert into @bestPromoCat
|
||||
select top 5 cs_promo_sk, sum(cs_net_profit) as posProfit from catalog_sales_history
|
||||
where cs_net_profit>0
|
||||
and cs_promo_sk is not NULL
|
||||
group by cs_promo_sk
|
||||
order by posProfit desc
|
||||
|
||||
return;
|
||||
|
||||
end
|
||||
go
|
|
@ -0,0 +1,18 @@
|
|||
--highest profit earning promos through stores
|
||||
|
||||
create or alter function bestPromoStore()
|
||||
returns @bestPromoStore TABLE(promo_sk int, profit_store decimal(15,2))
|
||||
as
|
||||
begin
|
||||
|
||||
insert into @bestPromoStore
|
||||
select top 5 ss_promo_sk, sum(ss_net_profit) as posProfit from store_sales_history
|
||||
where ss_net_profit>0
|
||||
and ss_promo_sk is not NULL
|
||||
group by ss_promo_sk
|
||||
order by posProfit desc
|
||||
|
||||
return;
|
||||
|
||||
end
|
||||
go
|
|
@ -0,0 +1,26 @@
|
|||
CREATE TYPE ManagerType
|
||||
AS TABLE
|
||||
(man_name varchar(40) unique, numClosed int);
|
||||
GO
|
||||
|
||||
CREATE or alter function unemployedManagers (@closedManagers ManagerType readonly)
|
||||
RETURNS @unemployedMan table (manName varchar(40))
|
||||
AS
|
||||
BEGIN
|
||||
declare @manager varchar(40);
|
||||
declare c1 cursor for (select man_name from @closedManagers);
|
||||
open c1;
|
||||
fetch next from c1 into @manager;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
if not exists (select * from call_center where cc_manager = @manager)
|
||||
begin
|
||||
insert into @unemployedMan values (@manager);
|
||||
end
|
||||
fetch next from c1 into @manager;
|
||||
end
|
||||
return;
|
||||
END
|
||||
go
|
||||
|
||||
--given a list of call-center manager names, find which of these are currently unemployed. This is called from stored procedure proc_53_shutOldCC
|
|
@ -0,0 +1,24 @@
|
|||
--top stores for each category based on the number of items sold
|
||||
create or alter function bestStoreForCatgory()
|
||||
returns @relation table (category char(50), store int)
|
||||
as
|
||||
begin
|
||||
declare @cat char(50);
|
||||
declare @maxStore int;
|
||||
declare c1 cursor for (select distinct i_category from item where i_category is not null)
|
||||
open c1;
|
||||
fetch next from c1 into @cat;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @maxStore = (select ss_store_sk from (
|
||||
select top 1 ss_store_sk, count(*) as cnt
|
||||
from store_sales, item
|
||||
where ss_item_sk = i_item_sk and i_category = @cat and ss_store_sk is not NULL
|
||||
group by ss_store_sk
|
||||
order by cnt desc)t);
|
||||
insert into @relation values (@cat, @maxStore);
|
||||
fetch next from c1 into @cat;
|
||||
end
|
||||
return;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,22 @@
|
|||
--analyse the profit of a store for a given duration.
|
||||
create or alter function profitMonitoring(@startDate date, @endDate date, @givenStore int)
|
||||
returns @profitTable table(dt date, profit decimal(15, 2))
|
||||
as
|
||||
begin
|
||||
if(@startDate>@endDate)
|
||||
return;
|
||||
declare @dateSk int;
|
||||
declare @dayProfit decimal (15, 2);
|
||||
while(@startDate<=@endDate)
|
||||
begin
|
||||
set @dateSk = (select d_date_sk from date_dim where d_date = @startDate)
|
||||
set @dayProfit = (select sum(ss_net_profit)
|
||||
from store_sales_history
|
||||
where ss_sold_date_sk=@dateSk and ss_store_sk = @givenStore);
|
||||
|
||||
insert into @profitTable values (@startDate, @dayProfit);
|
||||
set @startDate = DATEADD(dy, 1, @startDate);
|
||||
end
|
||||
return;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,37 @@
|
|||
--Out of the 1000 most returned items across all the sales channel, find the ones that are outdated.
|
||||
|
||||
create or alter function MaxReturnItems()
|
||||
returns @maxretItems table (itemNo int, manufactId int)
|
||||
as
|
||||
begin
|
||||
declare @itemNo int, @manufact int;
|
||||
declare @recDate date;
|
||||
declare @itemTbl table (itemNo int, cnt int);
|
||||
|
||||
insert into @itemTbl
|
||||
select top 1000 cr_item_sk, count(cnt) totalCnt
|
||||
from
|
||||
(select cr_item_sk, count(*) cnt from catalog_returns group by cr_item_sk
|
||||
union all
|
||||
select wr_item_sk, count(*) cnt from web_returns group by wr_item_sk
|
||||
union all
|
||||
select sr_item_sk, count(*) cnt from store_returns group by sr_item_sk)t
|
||||
group by cr_item_sk
|
||||
order by totalCnt desc
|
||||
|
||||
declare c1 cursor for select itemNo from @itemTbl;
|
||||
open c1;
|
||||
fetch next from c1 into @itemNo;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
set @recDate = (select i_rec_start_date from item where i_item_sk = @itemNo);
|
||||
set @manufact = (select i_manufact_id from item where i_item_sk = @itemNo);
|
||||
if(DATEDIFF(day, @recDate, '2000-01-01')>0)
|
||||
insert into @maxretItems values (@itemNo, @manufact);
|
||||
fetch next from c1 into @itemNo;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
return;
|
||||
end
|
||||
go
|
|
@ -0,0 +1,41 @@
|
|||
CREATE or ALTER TRIGGER dateTableChanges ON date_dim
|
||||
after INSERT
|
||||
AS
|
||||
declare @year int, @month int, @day int;
|
||||
declare @date date;
|
||||
set @year = (select d_year from inserted);
|
||||
set @month = (select d_moy from inserted);
|
||||
set @day = (select d_dom from inserted);
|
||||
set @date = (select d_date from inserted);
|
||||
|
||||
if(@year=NULL or @year<2100 or (@year=2100 and @month=01 and @day=01))
|
||||
begin
|
||||
RAISERROR('illegal insert in date table', 10, 1);
|
||||
rollback transaction;
|
||||
return;
|
||||
end
|
||||
|
||||
if(@date is NULL and (@year is NULL or @month is NULL or @day is NULL))
|
||||
begin
|
||||
RAISERROR('cannot insert incomplete date information', 10, 1);
|
||||
rollback transaction;
|
||||
return;
|
||||
end
|
||||
|
||||
if(@date is not NULL)
|
||||
begin
|
||||
declare @dDay int = DAY(@date);
|
||||
declare @dMonth int = MONTH(@date);
|
||||
declare @dYear int = YEAR(@date);
|
||||
if((@year is not NULL and @year!=@dYear) or (@month is not NULL and @month!=@dMonth) or (@day is not NULL and @day!=@dDay))
|
||||
begin
|
||||
RAISERROR('Inconsistent data values', 10, 1);
|
||||
rollback transaction;
|
||||
return;
|
||||
end
|
||||
end
|
||||
|
||||
GO
|
||||
|
||||
--invocation query
|
||||
insert into date_dim (d_date_sk, d_date_id, d_date, d_year, d_moy, d_dom) values (3488070, 'ACHOFIRSYCHGRUFG', '3022-01-19', 3022, 01, 19);
|
|
@ -0,0 +1,44 @@
|
|||
CREATE OR ALTER TRIGGER ca_update ON customer_address
|
||||
AFTER UPDATE
|
||||
AS
|
||||
if(update(ca_address_sk))
|
||||
begin
|
||||
RAISERROR('Illegal update operation', 10, 1);
|
||||
ROLLBACK TRANSACTION;
|
||||
end
|
||||
|
||||
else
|
||||
begin
|
||||
declare @adr_sk int;
|
||||
declare @adr_sk_varchar varchar(max);
|
||||
declare @insCity varchar(60), @delCity varchar(60), @insState char(2), @delState char(2), @insCount varchar(30), @delCount varchar(30);
|
||||
declare c1 cursor for select ca_address_sk, ca_city, ca_state, ca_country from inserted;
|
||||
open c1;
|
||||
fetch next from c1 into @adr_sk, @insCity, @insState, @insCount;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
select @delCity = ca_city, @delState=ca_state, @delCount = ca_country from deleted d where d.ca_address_sk = @adr_sk;
|
||||
set @adr_sk_varchar = CAST(@adr_sk AS varchar(max));
|
||||
if(@delCount!=@insCount)
|
||||
insert into logTable values ('address' + @adr_sk_varchar + 'changed to different country', GETDATE());
|
||||
else if (@delState!=@insState)
|
||||
insert into logTable values ('address' + @adr_sk_varchar + 'changed to different state', GETDATE());
|
||||
else if (@delCity!=@insCity)
|
||||
insert into logTable values ('address' + @adr_sk_varchar + 'changed to different city', GETDATE());
|
||||
fetch next from c1 into @adr_sk, @insCity, @insState, @insCount ;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
end
|
||||
GO
|
||||
|
||||
--invocation Query
|
||||
update customer_address
|
||||
set ca_country=
|
||||
(case
|
||||
when (ca_address_sk<=5) then 'India'
|
||||
when (ca_address_sk>5 and ca_address_sk<=10) then 'NewZealand'
|
||||
when (ca_address_sk>10 and ca_address_sk<=15) then 'France'
|
||||
end
|
||||
)
|
||||
where ca_address_sk<=15;
|
|
@ -0,0 +1,13 @@
|
|||
CREATE or ALTER TRIGGER promo_update ON promotion
|
||||
AFTER UPDATE
|
||||
AS
|
||||
insert into logTable
|
||||
select 'promo sk number ' + cast(i.p_promo_sk as varchar(max))+ ' re-activated', getDate()
|
||||
from deleted d, inserted i
|
||||
where d.p_promo_sk = i.p_promo_sk
|
||||
and d.p_discount_active='N' and i.p_discount_active='Y';
|
||||
|
||||
GO
|
||||
|
||||
--invocation query
|
||||
update promotion set p_discount_active='N' where p_promo_sk>=200 and p_promo_sk<400;
|
|
@ -0,0 +1,77 @@
|
|||
CREATE or ALTER TRIGGER delUp_item ON item
|
||||
AFTER DELETE, UPDATE
|
||||
AS
|
||||
if(update(i_item_sk))
|
||||
begin
|
||||
raiserror('Operation not allowed', 16, 10);
|
||||
rollback transaction;
|
||||
end
|
||||
|
||||
if exists (select * from inserted) --this means updates to item table happened
|
||||
begin
|
||||
insert into logTable values ('logging updation to item table', GETDATE());
|
||||
return;
|
||||
end
|
||||
|
||||
--Triggered due to a deletion in the item table.
|
||||
else
|
||||
begin
|
||||
delete from catalog_sales
|
||||
where cs_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from catalog_returns
|
||||
where cr_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from store_sales
|
||||
where ss_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from store_returns
|
||||
where sr_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from web_sales
|
||||
where ws_item_sk in (select i_item_sk from deleted);
|
||||
delete from web_returns
|
||||
|
||||
where wr_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from promotion
|
||||
where p_item_sk in (select i_item_sk from deleted);
|
||||
|
||||
delete from inventory
|
||||
where inv_item_sk in (select i_item_sk from deleted);
|
||||
end
|
||||
|
||||
GO
|
||||
|
||||
--invocation query
|
||||
declare @item int;
|
||||
declare c1 cursor for select * from #itemTable;
|
||||
open c1;
|
||||
fetch next from c1 into @item;
|
||||
while(@@FETCH_STATUS=0)
|
||||
begin
|
||||
delete from item where i_item_sk = @item;
|
||||
fetch next from c1 into @item;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
|
||||
|
||||
create table #itemTable (itemSk int)
|
||||
|
||||
insert into #itemTable
|
||||
select cs_item_sk from catalog_sales where cs_item_sk<30000
|
||||
intersect
|
||||
select ss_item_sk from store_sales where ss_item_sk<30000 and ss_item_sk>=20000
|
||||
intersect
|
||||
select ws_item_sk from web_sales where ws_item_sk<30000 and ws_item_sk>=20000
|
||||
intersect
|
||||
select cr_item_sk from catalog_returns where cr_item_sk<30000 and cr_item_sk>=20000
|
||||
intersect
|
||||
select sr_item_sk from store_returns where sr_item_sk<30000 and sr_item_sk>=20000
|
||||
intersect
|
||||
select wr_item_sk from web_returns where wr_item_sk<30000 and wr_item_sk>=20000
|
||||
intersect
|
||||
select inv_item_sk from inventory where inv_item_sk<30000 and inv_item_sk>=20000
|
||||
intersect
|
||||
select p_item_sk from promotion where p_item_sk<30000 and p_item_sk>=20000;
|
|
@ -0,0 +1,28 @@
|
|||
CREATE or ALTER TRIGGER deleteCatPage ON catalog_page
|
||||
AFTER DELETE
|
||||
AS
|
||||
declare @catalog_page int;
|
||||
declare @type varchar(100);
|
||||
declare c1 cursor static for (select cp_catalog_page_sk, cp_type from DELETED);
|
||||
open c1;
|
||||
fetch next from c1 into @catalog_page, @type;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
if(@type='monthly ')
|
||||
begin
|
||||
raiserror('cannot delete page from monthly catalog as per policy', 10, 1);
|
||||
rollback transaction;
|
||||
end
|
||||
else
|
||||
begin
|
||||
DELETE from catalog_sales where cs_catalog_page_sk = @catalog_page;
|
||||
DELETE from catalog_returns where cr_catalog_page_sk = @catalog_page;
|
||||
end
|
||||
fetch next from c1 into @catalog_page, @type;
|
||||
end
|
||||
close c1;
|
||||
deallocate c1;
|
||||
GO
|
||||
|
||||
--invocation query
|
||||
delete from catalog_page where cp_description like '%weapon%';
|
|
@ -0,0 +1,25 @@
|
|||
CREATE or ALTER TRIGGER deleteCustomer ON customer
|
||||
AFTER DELETE
|
||||
AS
|
||||
declare @cust_sk int;
|
||||
declare c2 cursor static for (select c_customer_sk from DELETED);
|
||||
open c2;
|
||||
fetch next from c2 into @cust_sk;
|
||||
while(@@fetch_status=0)
|
||||
begin
|
||||
UPDATE web_page set wp_customer_sk=NULL where wp_customer_sk = @cust_sk;
|
||||
DELETE from web_returns where wr_refunded_customer_sk = @cust_sk or wr_returning_customer_sk=@cust_sk;
|
||||
DELETE from catalog_returns where cr_refunded_customer_sk = @cust_sk or cr_returning_customer_sk=@cust_sk;
|
||||
DELETE from store_returns where sr_customer_sk = @cust_sk;
|
||||
|
||||
DELETE from web_sales where ws_bill_customer_sk = @cust_sk or ws_ship_customer_sk=@cust_sk;
|
||||
DELETE from catalog_sales where cs_bill_customer_sk = @cust_sk or cs_ship_customer_sk=@cust_sk;
|
||||
DELETE from store_sales where ss_customer_sk = @cust_sk;
|
||||
fetch next from c2 into @cust_sk;
|
||||
end
|
||||
close c2;
|
||||
deallocate c2;
|
||||
GO
|
||||
|
||||
--DML to invoke
|
||||
delete from customer where c_customer_sk=1
|
Загрузка…
Ссылка в новой задаче