-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpractical_fill_schema.sql
More file actions
2295 lines (2097 loc) · 107 KB
/
practical_fill_schema.sql
File metadata and controls
2295 lines (2097 loc) · 107 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***************************************************** **
practical_fill_schema.sql
Companion script for Practical Oracle SQL, Apress 2020
by Kim Berg Hansen, https://www.kibeha.dk
Use at your own risk
*****************************************************
Creation of objects in schema PRACTICAL
Tables, data and other objects
To be executed in schema PRACTICAL
** ***************************************************** */
/* -----------------------------------------------------
Create tables
----------------------------------------------------- */
create table customers (
id integer constraint customer_pk primary key
, name varchar2(20 char) not null
);
create table conway_gen_zero (
x integer not null
, y integer not null
, alive integer not null check (alive in (0,1))
, constraint conway_gen_zero_pk primary key (x, y)
);
create table web_devices (
day date constraint web_devices_pk primary key
, pc integer
, tablet integer
, phone integer
);
create table web_demographics (
day date constraint web_demographics_pk primary key
, m_tw_cnt integer
, m_tw_qty integer
, m_fb_cnt integer
, m_fb_qty integer
, f_tw_cnt integer
, f_tw_qty integer
, f_fb_cnt integer
, f_fb_qty integer
);
create table channels_dim (
id integer constraint channels_dim_pk primary key
, name varchar2(20 char) not null
, shortcut varchar2(2 char) not null
);
create table gender_dim (
letter char(1 char) constraint gender_dim_pk primary key
, name varchar2(10 char)
);
create table packaging (
id integer constraint packaging_pk primary key
, name varchar2(20 char) not null
);
create table packaging_relations (
packaging_id not null constraint packing_relations_parent_fk
references packaging
, contains_id not null constraint packing_relations_child_fk
references packaging
, qty integer not null
, constraint packaging_relations_pk primary key (packaging_id, contains_id)
);
create index packing_relations_child_fk_ix on packaging_relations (contains_id);
create table product_groups (
id integer constraint product_groups_pk primary key
, name varchar2(20 char) not null
);
create table products (
id integer constraint products_pk primary key
, name varchar2(20 char) not null
, group_id not null constraint products_product_groups_fk
references product_groups
);
create index products_product_groups_fk_ix on products (group_id);
create table monthly_sales (
product_id not null constraint monthly_sales_product_fk
references products
, mth date not null
, qty number not null
, constraint monthly_sales_pk primary key (product_id, mth)
, constraint monthly_sales_mth_valid check (
mth = trunc(mth, 'MM')
)
);
create table breweries (
id integer constraint brewery_pk primary key
, name varchar2(20 char) not null
);
create table purchases (
id integer constraint purchases_pk primary key
, purchased date not null
, brewery_id not null constraint purchases_brewery_fk
references breweries
, product_id not null constraint purchases_product_fk
references products
, qty number not null
, cost number not null
);
create index purchases_brewery_fk_ix on purchases (brewery_id);
create index purchases_product_fk_ix on purchases (product_id);
create table product_alcohol (
product_id not null constraint product_alcohol_pk primary key
constraint product_alcohol_product_fk
references products
, sales_volume number not null
, abv number not null
);
create table customer_favorites (
customer_id not null constraint customer_favorites_customer_fk
references customers
, favorite_list varchar2(4000 char)
, constraint customer_favorites_pk primary key (customer_id)
);
create table customer_reviews (
customer_id not null constraint customer_reviews_customer_fk
references customers
, review_list varchar2(4000 char)
, constraint customer_reviews_pk primary key (customer_id)
);
create table locations (
id integer constraint location_pk primary key
, warehouse integer not null
, aisle varchar2(1) not null
, position integer not null
, constraint locations_uq unique (warehouse, aisle, position)
);
create table inventory (
id integer constraint inventory_pk primary key
, location_id not null constraint inventory_location_fk
references locations
, product_id not null constraint inventory_product_fk
references products
, purchase_id not null constraint inventory_purchase_fk
references purchases
, qty number not null
);
create index inventory_location_fk_ix on inventory (location_id);
create index inventory_product_fk_ix on inventory (product_id);
create index inventory_purchase_fk_ix on inventory (purchase_id);
create table orders (
id integer constraint order_pk primary key
, customer_id not null constraint order_customer_fk
references customers
, ordered date
, delivery date
);
create index order_customer_fk_ix on orders (customer_id);
create table orderlines (
id integer constraint orderline_pk primary key
, order_id not null constraint orderline_order_fk
references orders
, product_id not null constraint orderline_product_fk
references products
, qty number not null
, amount number not null
);
create index orderline_order_fk_ix on orderlines (order_id);
create table monthly_budget (
product_id not null constraint monthly_budget_product_fk
references products
, mth date not null
, qty number not null
, constraint monthly_budget_pk primary key (product_id, mth)
, constraint monthly_budget_mth_valid check (
mth = trunc(mth, 'MM')
)
);
create table product_minimums (
product_id not null constraint product_minimums_pk primary key
constraint product_minimums_product_fk
references products
, qty_minimum number not null
, qty_purchase number not null
);
create table stock (
symbol varchar2(10) constraint stock_pk primary key
, company varchar2(40) not null
);
create table ticker (
symbol constraint ticker_symbol_fk references stock
, day date not null
, price number not null
, constraint ticker_pk primary key (symbol, day)
);
create table web_apps (
id integer constraint web_apps_pk primary key
, name varchar2(20 char) not null
);
create table web_pages (
app_id not null constraint web_pages_app_fk
references web_apps
, page_no integer not null
, friendly_url varchar2(20 char) not null
, constraint web_pages_pk primary key (app_id, page_no)
);
create table web_counter_hist (
app_id integer not null
, page_no integer not null
, day date not null
, counter integer not null
, constraint web_counter_hist_pk primary key (app_id, page_no, day)
, constraint web_counter_hist_page_fk foreign key (app_id, page_no)
references web_pages (app_id, page_no)
);
create table server_heartbeat (
server varchar2(15 char) not null
, beat_time date not null
, constraint server_heartbeat_uq unique (
server, beat_time
)
);
create table web_page_visits (
client_ip varchar2(15 char) not null
, visit_time date not null
, app_id integer not null
, page_no integer not null
, constraint web_page_visits_page_fk foreign key (app_id, page_no)
references web_pages (app_id, page_no)
);
create index web_page_visits_page_fk_ix on web_page_visits (app_id, page_no);
create table employees (
id integer constraint employees_pk primary key
, name varchar2(20 char) not null
, title varchar2(20 char) not null
, supervisor_id constraint employees_supervisor_fk
references employees
);
create index employees_supervisor_fk_ix on employees (supervisor_id);
create table emp_hire_periods (
emp_id not null constraint emp_hire_periods_emp_fk
references employees
, start_date date not null
, end_date date
, title varchar2(20 char) not null
, constraint emp_hire_periods_pk primary key (emp_id, start_date)
, period for employed_in (start_date, end_date)
);
create table picking_list (
id integer constraint picking_list_pk primary key
, created date not null
, picker_emp_id constraint picking_list_emp_fk
references employees
);
create index picking_list_emp_fk_ix on picking_list (picker_emp_id);
create table picking_line (
picklist_id not null constraint picking_line_picking_list_fk
references picking_list
, line_no integer not null
, location_id not null constraint picking_line_location_fk
references locations
, order_id not null constraint picking_line_order_fk
references orders
, product_id not null constraint picking_line_product_fk
references products
, qty number not null
, constraint picking_line_pk primary key (picklist_id, line_no)
);
create index picking_line_location_fk_ix on picking_line (location_id);
create index picking_line_order_fk_ix on picking_line (order_id);
create index picking_line_product_fk_ix on picking_line (product_id);
create table picking_log (
picklist_id not null constraint picking_log_picking_list_fk
references picking_list
, log_time date not null
, activity varchar2(1 char) not null
check (activity in ('A', 'P', 'D'))
, location_id constraint picking_log_location_fk
references locations
, pickline_no integer
, constraint picking_log_picking_line_fk foreign key (picklist_id, pickline_no)
references picking_line (picklist_id, line_no)
, constraint picking_log_picking_line_ck
check (not (activity = 'P' and pickline_no is null))
);
create index picking_log_picking_line_fk_ix on picking_log (picklist_id, pickline_no);
create index picking_log_location_fk on picking_log (location_id);
/* -----------------------------------------------------
Create types and type bodies
----------------------------------------------------- */
create or replace type id_name_type as object (
id integer
, name varchar2(20 char)
);
/
create or replace type id_name_coll_type
as table of id_name_type;
/
create type favorite_coll_type
as table of integer;
/
/*
ODCI implementation of delimited string parsing
https://www.kibeha.dk/2015/06/supposing-youve-got-data-as-text-string.html
*/
create or replace type delimited_col_row as object (
g_buffer varchar2(32000)
, g_type anytype
, g_numcols integer
, g_col_types sys.odcinumberlist
, g_col_delim varchar2(1)
, g_row_delim varchar2(1)
, g_pos1 integer
, g_pos2 integer
, g_col_num integer
, g_row_num integer
, static function parser(
p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return anydataset pipelined
using delimited_col_row
, static function odcitabledescribe(
p_tabtype out anytype
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
, static function odcitableprepare(
p_context out delimited_col_row
, p_tabinfo in sys.odcitabfuncinfo
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
, static function odcitablestart(
p_context in out delimited_col_row
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
, member function odcitablefetch(
self in out delimited_col_row
, p_numrows in number
, p_tab out anydataset
) return number
, member function odcitableclose(
self in delimited_col_row
) return number
)
/
create or replace type body delimited_col_row as
static function odcitabledescribe(
p_tabtype out anytype
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
is
l_type anytype;
l_numcols integer;
l_pos1 pls_integer;
l_pos2 pls_integer;
l_pos3 pls_integer;
l_colname varchar2(1932);
l_coltype varchar2(1932);
begin
anytype.begincreate(dbms_types.typecode_object, l_type);
l_pos1 := 0;
loop
l_pos2 := instr(p_cols, p_col_delim, l_pos1+1);
l_pos3 := nvl(nullif(instr(p_cols, p_row_delim, l_pos1+1),0),length(p_cols)+1);
l_colname := substr(p_cols, l_pos1+1, l_pos2-l_pos1-1);
l_coltype := upper(substr(p_cols, l_pos2+1, l_pos3-l_pos2-1));
if l_colname like '"%"' then
l_colname := trim(both '"' from l_colname);
else
l_colname := upper(l_colname);
end if;
case
when l_coltype like 'VARCHAR2(%)' then
l_type.addattr(
l_colname
, dbms_types.typecode_varchar2
, null
, null
, to_number(trim(substr(l_coltype,10,length(l_coltype)-10)))
, null
, null
);
when l_coltype = 'NUMBER' then
l_type.addattr(
l_colname
, dbms_types.typecode_number
, null
, null
, null
, null
, null
);
when l_coltype = 'DATE' then
l_type.addattr(
l_colname
, dbms_types.typecode_date
, null
, null
, null
, null
, null
);
else
raise dbms_types.invalid_parameters;
end case;
exit when l_pos3 > length(p_cols);
l_pos1 := l_pos3;
end loop;
l_type.endcreate;
anytype.begincreate(dbms_types.typecode_table, p_tabtype);
p_tabtype.setinfo(
null
, null
, null
, null
, null
, l_type
, dbms_types.typecode_object
, 0
);
p_tabtype.endcreate();
return odciconst.success;
exception
when others then
return odciconst.error;
end odcitabledescribe;
static function odcitableprepare(
p_context out delimited_col_row
, p_tabinfo in sys.odcitabfuncinfo
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
is
l_type anytype;
l_numcols integer;
l_col_types sys.odcinumberlist := sys.odcinumberlist();
l_typecode pls_integer;
l_prec pls_integer;
l_scale pls_integer;
l_len pls_integer;
l_csid pls_integer;
l_csfrm pls_integer;
l_aname varchar2(30);
l_schemaname varchar2(30);
l_typename varchar2(30);
l_version varchar2(30);
l_dummytype anytype;
begin
l_typecode := p_tabinfo.rettype.getattreleminfo(
1
, l_prec
, l_scale
, l_len
, l_csid
, l_csfrm
, l_type
, l_aname
);
l_typecode := l_type.getinfo(
l_prec
, l_scale
, l_len
, l_csid
, l_csfrm
, l_schemaname
, l_typename
, l_version
, l_numcols
);
l_col_types.extend(l_numcols);
for idx in 1..l_numcols loop
l_col_types(idx) := l_type.getattreleminfo(
idx
, l_prec
, l_scale
, l_len
, l_csid
, l_csfrm
, l_dummytype
, l_aname
);
end loop;
p_context := delimited_col_row(
p_text
, l_type
, l_numcols
, l_col_types
, p_col_delim
, p_row_delim
, 0
, 0
, 0
, 0
);
return odciconst.success;
end odcitableprepare;
static function odcitablestart(
p_context in out delimited_col_row
, p_text in varchar2
, p_cols in varchar2
, p_col_delim in varchar2 default '|'
, p_row_delim in varchar2 default ';'
) return number
is
begin
p_context.g_buffer := p_text;
p_context.g_pos1 := 0;
p_context.g_pos2 := 0;
p_context.g_col_num := 0;
p_context.g_row_num := 0;
return odciconst.success;
end odcitablestart;
member function odcitablefetch(
self in out delimited_col_row
, p_numrows in number
, p_tab out anydataset
) return number
is
l_row_cnt integer := 0;
begin
if self.g_pos2 < length(self.g_buffer) then
anydataset.begincreate(dbms_types.typecode_object, self.g_type, p_tab);
loop
self.g_col_num := self.g_col_num + 1;
if self.g_col_num = 1 then
l_row_cnt := l_row_cnt + 1;
self.g_row_num := self.g_row_num + 1;
p_tab.addinstance;
p_tab.piecewise();
end if;
if self.g_col_num < self.g_numcols then
self.g_pos2 := instr(self.g_buffer, self.g_col_delim, self.g_pos1+1);
else
self.g_pos2 := nvl(nullif(instr(self.g_buffer, self.g_row_delim, self.g_pos1+1),0),length(self.g_buffer)+1);
end if;
case self.g_col_types(self.g_col_num)
when dbms_types.typecode_varchar2 then
p_tab.setvarchar2(substr(self.g_buffer, self.g_pos1+1, self.g_pos2-self.g_pos1-1));
when dbms_types.typecode_number then
p_tab.setnumber(to_number(substr(self.g_buffer, self.g_pos1+1, self.g_pos2-self.g_pos1-1)));
when dbms_types.typecode_date then
p_tab.setdate(to_date(substr(self.g_buffer, self.g_pos1+1, self.g_pos2-self.g_pos1-1)));
end case;
exit when self.g_pos2 > length(self.g_buffer);
self.g_pos1 := self.g_pos2;
if self.g_col_num = self.g_numcols then
self.g_col_num := 0;
exit when l_row_cnt >= p_numrows;
end if;
end loop;
p_tab.endcreate;
end if;
return odciconst.success;
end odcitablefetch;
member function odcitableclose(
self in delimited_col_row
) return number
is
begin
return odciconst.success;
end odcitableclose;
end;
/
/* Should really be:
create type name_coll_type
as table of varchar2(20 char);
But an Oracle bug requires workaround with 80 byte:
*/
create or replace type name_coll_type
as table of varchar2(80 byte);
/
/*
ODCI implementation of string aggregate function - based on example by Tom Kyte
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2196162600402
*/
create or replace type stragg_expr_type as object (
element varchar2(4000 char)
, delimiter varchar2(4000 char)
, map member function map_func return varchar2
);
/
create or replace type body stragg_expr_type
as
map member function map_func return varchar2
is
begin
return element || '|' || delimiter;
end map_func;
end;
/
create or replace type stragg_type as object
(
aggregated varchar2(4000)
, delimiter varchar2(4000)
, static function ODCIAggregateInitialize(
new_self in out stragg_type
) return number
, member function ODCIAggregateIterate(
self in out stragg_type
, value in stragg_expr_type
) return number
, member function ODCIAggregateTerminate(
self in stragg_type
, returnvalue out varchar2
, flags in number
) return number
, member function ODCIAggregateMerge(
self in out stragg_type
, other_self in stragg_type
) return number
);
/
create or replace type body stragg_type
is
static function ODCIAggregateInitialize(
new_self in out stragg_type
) return number
is
begin
new_self := stragg_type( null, null );
return ODCIConst.Success;
end ODCIAggregateInitialize;
member function ODCIAggregateIterate(
self in out stragg_type
, value in stragg_expr_type
) return number
is
begin
if self.delimiter is null then
self.delimiter := value.delimiter;
end if;
self.aggregated := self.aggregated
|| self.delimiter
|| value.element;
return ODCIConst.Success;
end ODCIAggregateIterate;
member function ODCIAggregateTerminate(
self in stragg_type
, returnvalue out varchar2
, flags in number
) return number
is
begin
returnvalue := substr(
self.aggregated
, nvl(length(self.delimiter),0) + 1
);
return ODCIConst.Success;
end ODCIAggregateTerminate;
member function ODCIAggregateMerge(
self in out stragg_type
, other_self in stragg_type
) return number
is
begin
if self.delimiter is null then
self.delimiter := other_self.delimiter;
end if;
self.aggregated := self.aggregated
|| other_self.aggregated;
return ODCIConst.Success;
end ODCIAggregateMerge;
end;
/
/* -----------------------------------------------------
Create packages, functions and procedures
----------------------------------------------------- */
create or replace package formulas
is
function bac (
p_volume in number
, p_abv in number
, p_weight in number
, p_gender in varchar2
) return number deterministic;
end formulas;
/
create or replace package body formulas
is
function bac (
p_volume in number
, p_abv in number
, p_weight in number
, p_gender in varchar2
) return number deterministic
is
PRAGMA UDF;
begin
return round(
100 * (p_volume * p_abv / 100 * 0.789)
/ (p_weight * 1000 * case p_gender
when 'M' then 0.68
when 'F' then 0.55
end)
, 3
);
end bac;
end formulas;
/
create or replace function favorite_list_to_coll_type (
p_favorite_list in customer_favorites.favorite_list%type
)
return favorite_coll_type pipelined
is
v_from_pos pls_integer;
v_to_pos pls_integer;
begin
if p_favorite_list is not null then
v_from_pos := 1;
loop
v_to_pos := instr(p_favorite_list, ',', v_from_pos);
pipe row (to_number(
substr(
p_favorite_list
, v_from_pos
, case v_to_pos
when 0 then length(p_favorite_list) + 1
else v_to_pos
end - v_from_pos
)
));
exit when v_to_pos = 0;
v_from_pos := v_to_pos + 1;
end loop;
end if;
end favorite_list_to_coll_type;
/
create or replace function name_coll_type_to_varchar2 (
p_name_coll in name_coll_type
, p_delimiter in varchar2 default null
)
return varchar2
is
v_name_string varchar2(4000 char);
begin
for idx in p_name_coll.first..p_name_coll.last
loop
if idx = p_name_coll.first then
v_name_string := p_name_coll(idx);
else
v_name_string := v_name_string
|| p_delimiter
|| p_name_coll(idx);
end if;
end loop;
return v_name_string;
end name_coll_type_to_varchar2;
/
create or replace function stragg(input stragg_expr_type )
return varchar2
parallel_enable aggregate using stragg_type;
/
/* -----------------------------------------------------
Create views
----------------------------------------------------- */
create or replace view customer_order_products
as
select
c.id as customer_id
, c.name as customer_name
, o.ordered
, p.id as product_id
, p.name as product_name
, ol.qty
from customers c
join orders o
on o.customer_id = c.id
join orderlines ol
on ol.order_id = o.id
join products p
on p.id = ol.product_id;
create or replace view customer_order_products_obj
as
select
customer_id
, max(customer_name) as customer_name
, cast(
collect(
id_name_type(product_id, product_name)
order by product_id
)
as id_name_coll_type
) as product_coll
from customer_order_products
group by customer_id;
create view product_alcohol_bac
as
with
function gram_alcohol (
p_volume in number
, p_abv in number
) return number deterministic
is
begin
return p_volume * p_abv / 100 * 0.789;
end;
function gram_body_fluid (
p_weight in number
, p_gender in varchar2
) return number deterministic
is
begin
return p_weight * 1000 * case p_gender
when 'M' then 0.68
when 'F' then 0.55
end;
end;
function bac (
p_volume in number
, p_abv in number
, p_weight in number
, p_gender in varchar2
) return number deterministic
is
begin
return round(
100 * gram_alcohol(p_volume, p_abv)
/ gram_body_fluid(p_weight, p_gender)
, 3
);
end;
select
pa.product_id
, pa.sales_volume
, pa.abv
, bac(pa.sales_volume, pa.abv, 80, 'M') bac_m
, bac(pa.sales_volume, pa.abv, 60, 'F') bac_f
from product_alcohol pa
/
create or replace view purchases_with_dims
as
select
pu.id
, pu.purchased
, pu.brewery_id
, b.name as brewery_name
, pu.product_id
, p.name as product_name
, p.group_id
, pg.name as group_name
, pu.qty
, pu.cost
from purchases pu
join breweries b
on b.id = pu.brewery_id
join products p
on p.id = pu.product_id
join product_groups pg
on pg.id = p.group_id;
create or replace view brewery_products
as
select
b.id as brewery_id
, b.name as brewery_name
, p.id as product_id
, p.name as product_name
from breweries b
cross join products p
where exists (
select null
from purchases pu
where pu.brewery_id = b.id