22,209
社区成员
发帖
与我相关
我的任务
分享
if exists(select name from sys.objects where name = 'F_Discount')
drop table F_Discount
go
create table F_Discount( item_id char(4) , Discount_Period_Start date , Discount_Period_End date , Discount_Percentage decimal(2,2))
go
insert into F_Discount
select '1001' , '2011-01-01' , '2011-01-05' , 0.2 union all
select '1002' , '2011-01-15' , '2011-01-20' , 0.1union all
select '1003' , '2011-02-01' , '2011-02-05' , 0.1
go
if exists(select name from sys.objects where name = 'F_SalesDetail')
drop table F_SalesDetail
go
create table F_SalesDetail(Invoice_No char(7) , Invoice_Date date , Item_No char(4) , Amount decimal(6,2))
go
insert into F_SalesDetail
select 'A201101' , '2011-01-01' , '1001' , 64 UNION ALL
select 'A201101' , '2011-01-01' , '1002' , 60 UNION ALL
select 'A201102' , '2011-01-10' , '1001' , 64 UNION ALL
select 'A201102' , '2011-01-10' , '1002' , 54 UNION ALL
select 'A201102' , '2011-01-10' , '1003' , 27
GO
if exists(select name from sys.objects where name = 'D_Item')
drop table D_Item
go
create table D_Item(Item_ID char(4) , Unit_Price decimal(6,2))
go
insert into D_Item
select '1001' , 80 union all
select '1002' , 60 union all
select '1003' , 30
go
select fs.* ,
(1 - case when fd.Discount_Percentage is null then 0 else fd.Discount_Percentage end) * di.Unit_Price
from F_SalesDetail as fs inner join D_Item as di on fs.item_no = di.item_ID
left join F_Discount as fd on fs.item_no = fd.item_id and fs.Invoice_Date between fd.Discount_Period_Start and fd.Discount_Period_End
where fs.Amount <> (1 - case when fd.Discount_Percentage is null then 0 else fd.Discount_Percentage end) * di.Unit_Price
/*
Invoice_No Invoice_Date Item_No Amount
---------- ------------ ------- --------------------------------------- ---------------------------------------
A201102 2011-01-10 1001 64.00 80.0000
A201102 2011-01-10 1002 54.00 60.0000
A201102 2011-01-10 1003 27.00 30.0000
(3 個資料列受到影響)*/
if exists(select name from sys.objects where name = 'F_Discount_2')
drop table F_Discount_2
go
create table F_Discount_2( item_id char(4) ,Discount_Date date , Discount_Percentage decimal(2,2))
go
insert into F_Discount_2
select '1001' , '2011-01-01' , 0.2 union all
select '1001' , '2011-01-02' , 0.2 union all
select '1001' , '2011-01-03' , 0.2 union all
select '1001' , '2011-01-04' , 0.2 union all
select '1001' , '2011-01-05' , 0.2 union all
select '1002' , '2011-01-15' , 0.1union all
select '1002' , '2011-01-16' , 0.1union all
select '1002' , '2011-01-17' , 0.1union all
select '1002' , '2011-01-18' , 0.1union all
select '1002' , '2011-01-19' , 0.1union all
select '1002' , '2011-01-20' , 0.1union all
select '1003' , '2011-02-01' , 0.1 union all
select '1003' , '2011-02-02' , 0.1 union all
select '1003' , '2011-02-03' , 0.1 union all
select '1003' , '2011-02-04' , 0.1 union all
select '1003' , '2011-02-05' , 0.1
go
select fs.* ,
(1 - case when fd.Discount_Percentage is null then 0 else fd.Discount_Percentage end) * di.Unit_Price
from F_SalesDetail as fs inner join D_Item as di on fs.item_no = di.item_ID
left join F_Discount_2 as fd on fs.item_no = fd.item_id and fs.Invoice_Date = fd.Discount_Date
where fs.Amount <> (1 - case when fd.Discount_Percentage is null then 0 else fd.Discount_Percentage end) * di.Unit_Price
至於哪種更優,尚未驗證,我會選擇第二種作爲我的答案