-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindFacilitiesWithTotalRevenueLessThan1000.cs
78 lines (75 loc) · 3.55 KB
/
FindFacilitiesWithTotalRevenueLessThan1000.cs
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
using System.Linq;
using EFCorePgExercises.DataLayer;
using EFCorePgExercises.Utils;
using FluentAssertions;
namespace EFCorePgExercises.Exercises.Aggregation
{
[FullyQualifiedTestClass]
public class FindFacilitiesWithTotalRevenueLessThan1000
{
[FullyQualifiedTestMethod]
public void Test()
{
// https://pgexercises.com/questions/aggregates/facrev2.html
// Produce a list of facilities with a total revenue less than 1000.
// Produce an output table consisting of facility name and revenue, sorted by revenue.
// Remember that there's a different cost for guests and members!
//select name, revenue from (
// select facs.name, sum(case
// when memid = 0 then slots * facs.guestcost
// else slots * membercost
// end) as revenue
// from cd.bookings bks
// inner join cd.facilities facs
// on bks.facid = facs.facid
// group by facs.name
// ) as agg where revenue < 1000
//order by revenue;
EFServiceProvider.RunInContext(context =>
{
var facilities =
context.Bookings.Select(booking =>
new
{
booking.Facility.Name,
Revenue = booking.MemId == 0 ?
booking.Slots * booking.Facility.GuestCost
: booking.Slots * booking.Facility.MemberCost
})
.GroupBy(b => b.Name)
.Select(group => new
{
Name = group.Key,
TotalRevenue = group.Sum(b => b.Revenue)
})
.Where(result => result.TotalRevenue < 1000)
.OrderBy(result => result.TotalRevenue)
.ToList();
/*
SELECT [f].[Name], SUM(CASE
WHEN [b].[MemId] = 0 THEN CAST([b].[Slots] AS decimal(18,6)) * [f].[GuestCost]
ELSE CAST([b].[Slots] AS decimal(18,6)) * [f].[MemberCost]
END) AS [TotalRevenue]
FROM [Bookings] AS [b]
INNER JOIN [Facilities] AS [f] ON [b].[FacId] = [f].[FacId]
GROUP BY [f].[Name]
HAVING SUM(CASE
WHEN [b].[MemId] = 0 THEN CAST([b].[Slots] AS decimal(18,6)) * [f].[GuestCost]
ELSE CAST([b].[Slots] AS decimal(18,6)) * [f].[MemberCost]
END) < 1000.0
ORDER BY SUM(CASE
WHEN [b].[MemId] = 0 THEN CAST([b].[Slots] AS decimal(18,6)) * [f].[GuestCost]
ELSE CAST([b].[Slots] AS decimal(18,6)) * [f].[MemberCost]
END)
*/
var expectedResult = new[]
{
new { Name = "Table Tennis", TotalRevenue = 180M },
new { Name = "Snooker Table", TotalRevenue = 240M },
new { Name = "Pool Table", TotalRevenue = 270M }
};
facilities.Should().BeEquivalentTo(expectedResult);
});
}
}
}