-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.sql
More file actions
694 lines (623 loc) · 22.3 KB
/
Functions.sql
File metadata and controls
694 lines (623 loc) · 22.3 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
--Functions
--zwraca liste uczestnikow danego dnia konferencji
create function function_participantsOfDayConference(@ConferenceDayID int)
returns table
as
return(
select ind.firstname, ind.lastname, '' as 'Company'
from IndividualClient as ind
inner join Person as p
on p.PersonID = ind.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join DayReservation as dr
on dr.DayReservationID = dp.DayReservationID
where dr.ConferenceDayID = @ConferenceDayID
union
select e.firstname, e.lastname, c.companyname as 'Company'
from Employee as e
inner join Company as c
on c.ClientID = e.ClientID
inner join Person as p
on p.PersonID = e.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join DayReservation as dr
on dr.DayReservationID = dp.DayReservationID
where dr.ConferenceDayID = @ConferenceDayID
)
go
--lista warsztatow dla konferencji
create function function_workshopsDuringConference(@conf_id int)
returns table
as
return (
select conf.ConferenceName, conf.ConferenceDescription,
conf.Limit as 'Conf. limit',
conf.BasePrice as 'Conf. price', conf.StudentDiscount, cd.ConferenceDate,
conf.StartDate, w.WorkshopID,
wd.WorkshopName, wd.WorkshopDescription, w.limit as 'W. limit',
w.StartTime, w.EndTime, w.Price as 'W. Price'
from Conferences as conf
inner join ConferenceDay as cd
on cd.ConferenceID = conf.ConferenceID
inner join Workshop as w
on w.ConferenceDayID = cd.ConferenceDayID
inner join WorkshopDictionary as wd
on wd.WorkshopDictionaryID = w.WorkshopDictionaryID
where conf.ConferenceID = @conf_id
)
go
--zwraca liste uczestnikow danego warsztatu
create function function_participantsOfWorkshop(@WorkshopID int)
returns table
as
return(
select ind.firstname, ind.lastname, '' as 'Company'
from IndividualClient as ind
inner join Person as p
on p.PersonID = ind.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join WorkshopParticipant as wp
on wp.DayParticipantID = dp.DayParticipantID
inner join WorkshopReservation as wr
on wr.WorkshopReservationID = wp.WorkshopReservationID
where wr.WorkshopID = @WorkshopID
union
select e.firstname, e.lastname, c.companyname as 'Company'
from Employee as e
inner join Company as c
on c.ClientID = e.ClientID
inner join Person as p
on p.PersonID = e.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join WorkshopParticipant as wp
on wp.DayParticipantID = dp.DayParticipantID
inner join WorkshopReservation as wr
on wr.WorkshopReservationID = wp.WorkshopReservationID
where wr.WorkshopID = @WorkshopID
)
go
--lista uczestnikow konferencji (do identyfikatorow)
create function function_participantListForConference(@conf_id int)
returns table
as
return (
select ind.firstname, ind.lastname, '' as 'Company'
from IndividualClient as ind
inner join Person as p
on p.PersonID = ind.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join DayReservation as dr
on dr.DayReservationID = dp.DayReservationID
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
where cd.ConferenceID = @conf_id
union
select e.firstname, e.lastname, c.CompanyName as 'Company'
from employee as e
inner join Company as c
on c.ClientID = e.ClientID
inner join Person as p
on p.PersonID = e.PersonID
inner join DayParticipant as dp
on dp.PersonID = p.PersonID
inner join DayReservation as dr
on dr.DayReservationID = dp.DayReservationID
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
where cd.ConferenceID = @conf_id
)
go
--funkcja zwracajaca top X aktywnych (wg ilosci
--oplaconych rezerwacji) klientow indywidualnych
create function function_topIndividualClients(@X int)
returns table
as
return (
select top(@x) with ties c.ClientID, ic.FirstName, ic.LastName,
count(r.ReservationID) as 'Liczba oplaconych rezerwacji'
from Clients as c
inner join IndividualClient as ic
on ic.ClientID = c.ClientID
inner join Person as p
on p.PersonID = ic.PersonID
inner join Reservation as r
on r.ClientID = c.ClientID
where r.PaymentDate is not null
group by c.ClientID, ic.FirstName, ic.LastName
order by 4 desc
)
go
--top X firm wg zakupionych biletow
create function function_topCompaniesByTickets(@x int)
returns table
as
return (
select top(@x) with ties com.CompanyName,
sum(dr.NormalTickets)
+
sum(dr.StudentTickets)
as 'Total number of tickets'
from Company as com
inner join Clients as cl
on cl.ClientID = com.ClientID
inner join Reservation as r
on r.ClientID = cl.ClientID
and r.PaymentDate is not null
inner join DayReservation as dr
on dr.ReservationID = r.ReservationID
group by com.CompanyName
order by 2 desc
)
go
--top x firm wg rezerwacji
create function function_topCompaniesByReservations(@x int)
returns table
as
return (
select top(1) with ties com.CompanyName,
count(r.ReservationID) as 'Liczba oplaconych rezerwacji'
from Company as com
inner join Clients as cl
on cl.ClientID = com.ClientID
inner join Reservation as r
on r.ClientID = cl.ClientID
and r.PaymentDate is not null
group by com.CompanyName
order by count(r.ReservationID) desc
)
go
--zwraca ID dnia konferencji (o podanej dacie)
create function function_returnConferenceDay(@ConferenceID int, @Date date)
returns int
as
begin
return (
select ConferenceDayID
from ConferenceDay
where ConferenceID = @ConferenceID and ConferenceDate = @Date
)
end
go
--zwraca cene warsztatu
create function function_returnValueOfWorkshop(@WorkshopDictionaryID int)
returns money
as
begin
return (
select Price from WorkshopDictionary
where WorkshopDictionaryID = @WorkshopDictionaryID
)
end
go
--zwraca ID osoby
create function function_returnPersonID(@ReservationID int)
returns int
as
begin
return (
select p.PersonID
from Clients as c
inner join IndividualClient as id
on id.ClientID = c.ClientID
inner join Person as p
on id.PersonID = p.PersonID
where (select ClientID from Reservation as r
where ReservationID = @ReservationID) = c.ClientID
)
end
go
--zwraca cene normalnego biletu w danej rezerwacji
create function function_returnNormalTicketCost(@reservationID int)
returns int
as
begin
declare @reservationDate date = (
select ReservationDate
from Reservation
where ReservationID = @reservationID
)
return (
select c.BasePrice*(1-isnull(p.Discount, 0))
from Conferences as c
left outer join Prices as p
on p.ConferenceID = c.ConferenceID and
p.StartDate in (
select p2.StartDate
from Prices as p2
where p2.ConferenceID = c.ConferenceID
group by p2.StartDate
having min(datediff(d, StartDate, @reservationDate)) >= 0
)
where c.ConferenceID = (
select distinct cd.ConferenceID
from DayReservation as dr
inner join Reservation as r
on r.ReservationID = dr.ReservationID
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
where r.ReservationID = @reservationID
)
)
end
go
--zwraca cene studenckiego biletu dla rezerwacji
create function function_returnStudentTicketCost(@reservationID int)
returns int
as
begin
declare @reservationDate date = (
select ReservationDate
from Reservation
where ReservationID = @reservationID
)
return (
select c.BasePrice*(1-isnull(p.Discount, 0))*(1-c.StudentDiscount)
from Conferences as c
left outer join Prices as p
on p.ConferenceID = c.ConferenceID and
p.StartDate in (
select p2.StartDate
from Prices as p2
where p2.ConferenceID = c.ConferenceID
group by p2.StartDate
having min(datediff(d, StartDate, @reservationDate)) >= 0
)
where c.ConferenceID = (
select distinct cd.ConferenceID
from DayReservation as dr
inner join Reservation as r
on r.ReservationID = dr.ReservationID
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
where r.ReservationID = @reservationID
)
)
end
go
--wyswetlanie szczegolow rezerwacji
create function function_reservationSummary(@reservationID int)
returns table
as
return (
select concat('Konferencja: ', c.ConferenceName, ', Data: ',
cd.ConferenceDate, ' - ', 'Liczba biletow normalnych: ', dr.NormalTickets)
as ReservationInfo
from DayReservation as dr
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
inner join Conferences as c
on c.ConferenceID = cd.ConferenceID
where dr.ReservationID = @reservationID
union all
select concat('Konferencja: ', c.ConferenceName, ', Data: ',
cd.ConferenceDate, ' - ', 'Liczba biletow studenckich: ', dr.StudentTickets)
as ReservationInfo
from DayReservation as dr
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
inner join Conferences as c
on c.ConferenceID = cd.ConferenceID
where dr.ReservationID = @reservationID
union all
select concat('Warsztat: ', wd.WorkshopName, ', Data: ',
cd.ConferenceDate, ' - ', 'Liczba biletow: ', wr.Tickets)
as ReservationInfo
from DayReservation as dr
inner join ConferenceDay as cd
on cd.ConferenceDayID = dr.ConferenceDayID
inner join WorkshopReservation as wr
on wr.DayReservationID = dr.DayReservationID
inner join Workshop as w
on w.WorkshopID = wr.WorkshopID
inner join WorkshopDictionary as wd
on wd.WorkshopDictionaryID = w.WorkshopDictionaryID
where dr.ReservationID = @reservationID
union all
select Concat('Cena: ',
sum(dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID))
+
sum(dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID))
+
sum(isnull(wr.Tickets*w.Price, 0))) as ReservationInfo
from DayReservation as dr
left outer join WorkshopReservation wr
on dr.DayReservationID = wr.DayReservationID
left outer join Workshop w
on wr.WorkshopID = w.WorkshopID
where dr.ReservationID = @reservationID
)
go
--generowanie faktury dla klienta indywidualnego
create function function_generateIndividualInvoice(@reservationID int)
returns table
as
return (
select concat('Rezerwujacy: ', ic.FirstName, ' ' , ic.LastName) as Faktura
from Reservation as r
inner join Clients C
on r.ClientID = C.ClientID
inner join IndividualClient IC
on C.ClientID = IC.ClientID
where r.ReservationID = @reservationID
union all
select concat('Data rezerwacji: ', r.ReservationDate)
from Reservation as r
where r.ReservationID = @reservationID
union all
select concat('Data platnosci: ', r.PaymentDate)
from Reservation as r
where r.ReservationID = @reservationID
union all
select Concat('Kwota: ',
sum(dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID))
+
sum(dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID))
+
sum(isnull(wr.Tickets*w.Price, 0))) as Faktura
from DayReservation as dr
left outer join WorkshopReservation wr
on dr.DayReservationID = wr.DayReservationID
left outer join Workshop w
on wr.WorkshopID = w.WorkshopID
where dr.ReservationID = @reservationID
union all
select '-----------------------------------'
union all
select 'Szczegoly'
union all
select ''
union all
select distinct concat('Konferencja: ', C2.ConferenceName) as Faktura
from DayReservation as DR
inner join ConferenceDay CD
on DR.ConferenceDayID = CD.ConferenceDayID
inner join Conferences C2
on CD.ConferenceID = C2.ConferenceID
where DR.ReservationID = @reservationID
union all
select concat('Cena biletu normalnego (N): ',
dbo.function_returnNormalTicketCost(@reservationID)) as Faktura
union all
select concat('Cena biletu studenckiego (S): ',
dbo.function_returnStudentTicketCost(@reservationID)) as Faktura
union all
select concat('Dzien konferencji: ', CD.ConferenceDate,
', N: ', dr.NormalTickets, ', S: ', dr.StudentTickets, ', Cena: ',
dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID)
+
dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID)) as Faktura
from DayReservation as dr
inner join ConferenceDay CD
on dr.ConferenceDayID = CD.ConferenceDayID
where dr.ReservationID = @reservationID
union all
select 'Zarezerwowane warsztaty w ramach koferencji' as Faktura
union all
select concat(WD.WorkshopName, ', Dzien: ', CD.ConferenceDate, ', ',
W.StartTime, ' - ', W.EndTime ,', Cena: ', wr.Tickets*W.Price) as Faktura
from DayReservation as dr
left outer join WorkshopReservation WR
on dr.DayReservationID = WR.DayReservationID
left outer join Workshop W
on WR.WorkshopID = W.WorkshopID
left outer join WorkshopDictionary WD
on W.WorkshopDictionaryID = WD.WorkshopDictionaryID
inner join ConferenceDay CD
on W.ConferenceDayID = CD.ConferenceDayID
where dr.ReservationID = @reservationID
)
go
--generowanie faktury dla firmy
create function function_generateCompanyInvoice(@reservationID int)
returns table
as
return (
select concat('Rezerwujacy: ', C3.CompanyName) as Faktura
from Reservation as r
inner join Clients C
on r.ClientID = C.ClientID
inner join Company C3
on C.ClientID = C3.ClientID
where r.ReservationID = @reservationID
union all
select concat('Data rezerwacji: ', r.ReservationDate)
from Reservation as r
where r.ReservationID = @reservationID
union all
select concat('Data platnosci: ', r.PaymentDate)
from Reservation as r
where r.ReservationID = @reservationID
union all
select Concat('Kwota: ',
sum(dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID))
+
sum(dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID))
+
sum(isnull(wr.Tickets*w.Price, 0))) as Faktura
from DayReservation as dr
left outer join WorkshopReservation wr
on dr.DayReservationID = wr.DayReservationID
left outer join Workshop w
on wr.WorkshopID = w.WorkshopID
where dr.ReservationID = @reservationID
union all
select '-----------------------------------'
union all
select ''
union all
select distinct concat('Konferencja: ', C2.ConferenceName) as Faktura
from DayReservation as DR
inner join ConferenceDay CD
on DR.ConferenceDayID = CD.ConferenceDayID
inner join Conferences C2
on CD.ConferenceID = C2.ConferenceID
where DR.ReservationID = @reservationID
union all
select concat('Cena biletu normalnego (N): ',
dbo.function_returnNormalTicketCost(@reservationID)) as Faktura
union all
select concat('Cena biletu studenckiego (S): ',
dbo.function_returnStudentTicketCost(@reservationID)) as Faktura
union all
select ''
union all
select concat('Dzien konferencji: ', CD.ConferenceDate,
', N: ', dr.NormalTickets, ', S: ', dr.StudentTickets, ', Cena: ',
dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID)
+
dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID)) as Faktura
from DayReservation as dr
inner join ConferenceDay CD
on dr.ConferenceDayID = CD.ConferenceDayID
where dr.ReservationID = @reservationID
union all
select ''
union all
select 'Bilet normalny'
union all
select concat('Dzien konferencji: ', cd.ConferenceDate, ', ',
E.FirstName, ' ', e.LastName) as Faktura
from DayReservation as dr
inner join ConferenceDay CD
on dr.ConferenceDayID = CD.ConferenceDayID
inner join DayParticipant DP
on dr.DayReservationID = DP.DayReservationID
inner join Person P
on DP.PersonID = P.PersonID
inner join Employee E
on P.PersonID = E.PersonID
where dr.ReservationID = @reservationID and
p.PersonID not in (select Student.PersonID from Student)
union all
select ''
union all
select 'Bilet studencki'
union all
select concat('Dzien konferencji: ', cd.ConferenceDate, ', ',
E.FirstName, ' ', e.LastName) as Faktura
from DayReservation as dr
inner join ConferenceDay CD
on dr.ConferenceDayID = CD.ConferenceDayID
inner join DayParticipant DP
on dr.DayReservationID = DP.DayReservationID
inner join Person P
on DP.PersonID = P.PersonID
inner join Employee E
on P.PersonID = E.PersonID
where dr.ReservationID = @reservationID and
p.PersonID in (select Student.PersonID from Student)
union all
select ''
union all
select 'Zarezerwowane warsztaty w ramach konferencji' as Faktura
union all
select concat(WD.WorkshopName, ', Dzien: ', CD.ConferenceDate, ', ',
W.StartTime, ' - ', W.EndTime , ', Cena: ', wr.Tickets*W.Price) as Faktura
from DayReservation as dr
left outer join WorkshopReservation WR
on dr.DayReservationID = WR.DayReservationID
left outer join Workshop W
on WR.WorkshopID = W.WorkshopID
left outer join WorkshopDictionary WD
on W.WorkshopDictionaryID = WD.WorkshopDictionaryID
inner join ConferenceDay CD
on W.ConferenceDayID = CD.ConferenceDayID
where dr.ReservationID = @reservationID
union all
select concat(WD.WorkshopName, ', Dzien: ', CD.ConferenceDate, ', ',
W.StartTime, ' - ', W.EndTime , e2.FirstName, ' ', e2.LastName) as Faktura
from DayReservation as dr
left outer join WorkshopReservation WR
on dr.DayReservationID = WR.DayReservationID
inner join Workshop W
on WR.WorkshopID = W.WorkshopID
inner join WorkshopDictionary WD
on W.WorkshopDictionaryID = WD.WorkshopDictionaryID
inner join ConferenceDay CD
on W.ConferenceDayID = CD.ConferenceDayID
inner join WorkshopParticipant as wp
on wp.WorkshopReservationID = wr.WorkshopReservationID
inner join DayParticipant as dp
on wp.DayParticipantID = dp.DayParticipantID
inner join Person P2
on dp.PersonID = P2.PersonID
inner join Employee E2
on P2.PersonID = E2.PersonID
where dr.ReservationID = @reservationID
)
go
--zwraca nieoplacone rezerwacje klienta
create function function_showUnpaidReservations(@ClientID int)
returns table
as
return (
select r.ReservationID, r.ReservationDate,
abs(7 - datediff(day, r.ReservationDate, getdate())) as 'days left',
sum(dr.NormalTickets*dbo.function_returnNormalTicketCost(r.ReservationID))
+
sum(dr.StudentTickets*dbo.function_returnStudentTicketCost(r.ReservationID))
as 'Kwota'
from Reservation as r
inner join DayReservation as dr
on dr.ReservationID = r.ReservationID
where r.ClientID = @ClientID and r.PaymentDate is null
group by r.ReservationID, r.ReservationDate
)
go
--wyswietla pracownikow firmy
create function function_showEmployees(@CompanyID int)
returns table
as
return (
select C.CompanyName, e.FirstName, e.LastName
from Employee as e
inner join Company C
on e.ClientID = C.ClientID
where c.ClientID = @CompanyID
)
go
--wolne miesjca na dana konferencje (na poszczegolne dni)
create function function_showFreeSeatsForConference(@conferenceID int)
returns table
as
return (
select c.ConferenceID, c.ConferenceName, c.Limit, cd.ConferenceDate,
c.Limit -
isnull(((select sum(dr.NormalTickets)
from DayReservation as dr
where dr.ConferenceDayID = cd.ConferenceDayID)
+
(select sum(dr.StudentTickets)
from DayReservation as dr
where dr.ConferenceDayID = cd.ConferenceDayID)), 0) as 'Wolne miejsca'
from Conferences as c
inner join ConferenceDay as cd
on cd.ConferenceID = c.ConferenceID
where c.ConferenceID = @conferenceID
)
go
--zwraca wartosc rezerwacji (tylko)
create function function_returnReservationCost(@reservationID int)
returns int
as
begin
return (
select sum(dr.NormalTickets*dbo.function_returnNormalTicketCost(@reservationID))
+
sum(dr.StudentTickets*dbo.function_returnStudentTicketCost(@reservationID))
+
sum(isnull(wr.Tickets*w.Price, 0)) as Cost
from DayReservation as dr
left outer join WorkshopReservation wr
on dr.DayReservationID = wr.DayReservationID
left outer join Workshop w
on wr.WorkshopID = w.WorkshopID
where dr.ReservationID = @reservationID
)
end
go