-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLStaticFunction.sql
134 lines (119 loc) · 2.68 KB
/
SQLStaticFunction.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
CREATE OR REPLACE function month_turnover(
p_first_day date
)
returns table(
date text,
sum bigint
)
language plpgsql
as $$
begin
return query
select TO_CHAR(date_time :: date, 'dd/mm/yyyy'), sum(total)
from bill
where
date_time >= p_first_day and
date_time <= (date_trunc('month', p_first_day::date) +
interval '1 month' - interval '1 day')::date
and id_status = 5
group by date(date_time)
order by date;
end;$$;
CREATE OR REPLACE function year_turnover(
p_first_day date
)
returns table(
month numeric,
sum bigint
)
language plpgsql
as $$
begin
return query
select EXTRACT(month from date_time) as month, sum(total)
from bill
where
date_time >= p_first_day and
date_time <= (p_first_day::date + interval '1 year' - interval '1 day')::date and
id_status = 5
group by EXTRACT(month from date_time)
order by month;
end;$$;
CREATE OR REPLACE function month_status(
p_first_day date
)
returns table(
cancel bigint,
bought bigint,
transported bigint,
received bigint
)
language plpgsql
as $$
begin
return query
select count(id_bill) FILTER (WHERE id_status = 0) AS cancel,
count(id_bill) FILTER (WHERE id_status = 1 or id_status=2) AS bought,
count(id_bill) FILTER (WHERE id_status = 4 or id_status = 3) AS transported,
count(id_bill) FILTER (WHERE id_status = 5) AS received
from bill
where
date_time >= p_first_day and date_time <= (p_first_day::date + interval '1 month' - interval '1 day')::date;
end;$$;
CREATE OR REPLACE function get_admin_year(
)
returns table(
year int
)
language plpgsql
as $$
begin
return query
select DISTINCT extract(year from date_time)::int
from bill;
end;$$;
CREATE OR REPLACE function get_admin_month(
p_year int
)
returns table(
month int
)
language plpgsql
as $$
begin
return query
select DISTINCT extract(month from date_time)::int
from bill
where extract(year from date_time) = p_year;
end;$$;
CREATE OR REPLACE function total_month_turnover(
p_first_day date
)
returns bigint
language plpgsql
as $$
begin
return
(select sum(total)
from bill
where
date_time >= p_first_day and
date_time <= (date_trunc('month', p_first_day::date) +
interval '1 month' - interval '1 day')::date
and id_status = 5);
end;$$;
CREATE OR REPLACE function total_year_turnover(
p_first_day date
)
returns bigint
language plpgsql
as $$
begin
return
(select sum(total)
from bill
where
date_time >= p_first_day and
date_time <= (p_first_day::date + interval '1 year' - interval '1 day')::date and
id_status = 5);
end;$$;