-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queries_Phase2.sql
118 lines (89 loc) · 3.05 KB
/
Queries_Phase2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use FlowerShop
go
CREATE VIEW [Flower_Color] AS
select Flower.ID,Flower.Flower_name,Flower.Price,Colour.color_name,Colour.Hex_Code
from Flower,Colour
where Flower.Flower_color_ID=Colour.color_ID
select * from Flower_Color
--************************************************************************************
CREATE VIEW [Responsible_for_the_purchase] AS
select Buy.ID,Buy.Buy_date,GreenHouse.[name],SalesPerson.first_name,SalesPerson.last_name
from Buy, SalesPerson ,GreenHouse
where Buy.SalesPerson_ID=SalesPerson.PersonnelCode and Buy.Greenhouse_ID= GreenHouse.ID
select * from Responsible_for_the_purchase
--************************************************************************
CREATE VIEW [Order_with_Customer's_name] AS
select [Order].ID,[Order].Customer_ID,Customer.first_name,Customer.last_name,[Order].Shop_date
from [Order],Customer
where [Order].Customer_ID=Customer.ID
select * from [Order_with_Customer's_name]
--*******************************************************************
-- Description: <For calculating total money a consumer should pay>
CREATE FUNCTION Final_Price
(
-- Add the parameters for the function here
@shopID int
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @Result int
-- Add the T-SQL statements to compute the return value here
set @Result= (select [Order].Total_Cost from [Order] where [Order].ID=@shopID) - (select [Order].Discount from [Order] where [Order].ID=@shopID)
RETURN @Result
END
GO
select dbo.Final_Price(49)
--*******************************************************************************************
-- Description: <For calculating sum of the customer payments>
CREATE FUNCTION Total_Payment
(
-- Add the parameters for the function here
@Customer_ID int
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @Result int
set @Result= (select Sum(Final_Cost) as t from [Order] group by Customer_ID having [Order].Customer_ID=@Customer_ID)
RETURN @Result
END
GO
select dbo.Total_Payment(110) as total_Purchase
select * from Customer
--****************************************************************************
-- Description: <With name and last name of a customer show all of her/his shops>
CREATE FUNCTION Orders_with_Name
(
-- Add the parameters for the function here
@name varchar(20),
@family varchar(20)
)
RETURNS TABLE
AS
RETURN
(
SELECT [Order].ID,[Order].Customer_ID,[Order].Flower_ID,[Order].Order_type,[Order].Shop_date,[Order].Number,[Order].Occasion_ID,
[Order].Package_ID,[Order].Total_Cost,[Order].Discount,[Order].Final_Cost,[Order].More
from [Order],Customer
where [Order].Customer_ID=Customer.ID and Customer.first_name=@name and Customer.last_name=@family
)
GO
select * from Orders_with_Name('Neda','Ahadi')
--*****************************************************************
-- Description: <Show all online shops in the specific date>
CREATE FUNCTION Online_in_date
(
-- Add the parameters for the function here
@when date
)
RETURNS TABLE
AS
RETURN
(
select * from [Order] where Order_type='Online'
)
GO
select * from Online_in_date('1398-12-02')