Arithmetic DIVIDE operator
SQL Server Query
SELECT TOP 25
od.OrderID,
od.ProductID,
od.UnitPrice,
(od.Quantity * od.UnitPrice) AS ProductSales,
SUM(od.Quantity * od.UnitPrice) OVER (PARTITION BY od.OrderID) AS TotalOrderSales,
od.UnitPrice / SUM(od.Quantity * od.UnitPrice) OVER (PARTITION BY od.OrderID) AS UnitPriceAsFractionOfOrder
FROM [Order Details] AS od
ORDER BY od.OrderID, od.ProductID;
Create SQL query with SqlQueryBuilder
var (sql, parameters) = new SqlQueryBuilder()
.Select().Top(25)
.Columns("od.OrderID", "od.ProductID", "od.UnitPrice")
.Column(new ColumnArithmatic("od.Quantity").MULTIPLY("od.UnitPrice"), "ProductSales")
.Column(new SUM(new ColumnArithmatic("od.Quantity").MULTIPLY("od.UnitPrice"))
.OVER(new OVER().PARTITION_BY(new Column("od.OrderID"))), "TotalOrderSales")
.Column(new ColumnArithmatic("od.UnitPrice").DIVIDE(
new SUM(new ColumnArithmatic("od.Quantity").MULTIPLY("od.UnitPrice"))
.OVER(new OVER().PARTITION_BY(new Column("od.OrderID")))
), "UnitPriceAsFractionOfOrder")
.From("[Order Details]", "od")
.OrderBy(new OrderBy()
.SetColumnAscending("od.OrderID")
.SetColumnAscending("od.ProductID"))
.Build();
Query build by SqlQueryBuilder
SELECT TOP 25 od.OrderID,
od.ProductID,
od.UnitPrice,
od.Quantity * od.UnitPrice AS ProductSales,
SUM(od.Quantity * od.UnitPrice) OVER (PARTITION BY od.OrderID) AS TotalOrderSales,
od.UnitPrice / SUM(od.Quantity * od.UnitPrice) OVER (PARTITION BY od.OrderID) AS UnitPriceAsFractionOfOrder
FROM [Order Details] AS od
ORDER BY od.OrderID ASC, od.ProductID ASC;
Parameters (If used)
Query Results:
|
OrderID |
ProductID |
UnitPrice |
ProductSales |
TotalOrderSales |
UnitPriceAsFractionOfOrder |
Quantity |
1 |
10248
|
11
|
14.0000
|
168.0000
|
440.0000
|
0.0318
|
0
|
2 |
10248
|
42
|
9.8000
|
98.0000
|
440.0000
|
0.0222
|
0
|
3 |
10248
|
72
|
34.8000
|
174.0000
|
440.0000
|
0.0790
|
0
|
4 |
10249
|
14
|
18.6000
|
167.4000
|
1863.4000
|
0.0099
|
0
|
5 |
10249
|
51
|
42.4000
|
1696.0000
|
1863.4000
|
0.0227
|
0
|
6 |
10250
|
41
|
7.7000
|
77.0000
|
1813.0000
|
0.0042
|
0
|
7 |
10250
|
51
|
42.4000
|
1484.0000
|
1813.0000
|
0.0233
|
0
|
8 |
10250
|
65
|
16.8000
|
252.0000
|
1813.0000
|
0.0092
|
0
|
9 |
10251
|
22
|
16.8000
|
100.8000
|
670.8000
|
0.0250
|
0
|
10 |
10251
|
57
|
15.6000
|
234.0000
|
670.8000
|
0.0232
|
0
|
11 |
10251
|
65
|
16.8000
|
336.0000
|
670.8000
|
0.0250
|
0
|
12 |
10252
|
20
|
64.8000
|
2592.0000
|
3730.0000
|
0.0173
|
0
|
13 |
10252
|
33
|
2.0000
|
50.0000
|
3730.0000
|
0.0005
|
0
|
14 |
10252
|
60
|
27.2000
|
1088.0000
|
3730.0000
|
0.0072
|
0
|
15 |
10253
|
31
|
10.0000
|
200.0000
|
1444.8000
|
0.0069
|
0
|
16 |
10253
|
39
|
14.4000
|
604.8000
|
1444.8000
|
0.0099
|
0
|
17 |
10253
|
49
|
16.0000
|
640.0000
|
1444.8000
|
0.0110
|
0
|
18 |
10254
|
24
|
3.6000
|
54.0000
|
625.2000
|
0.0057
|
0
|
19 |
10254
|
55
|
19.2000
|
403.2000
|
625.2000
|
0.0307
|
0
|
20 |
10254
|
74
|
8.0000
|
168.0000
|
625.2000
|
0.0127
|
0
|
21 |
10255
|
2
|
15.2000
|
304.0000
|
2490.5000
|
0.0061
|
0
|
22 |
10255
|
16
|
13.9000
|
486.5000
|
2490.5000
|
0.0055
|
0
|
23 |
10255
|
36
|
15.2000
|
380.0000
|
2490.5000
|
0.0061
|
0
|
24 |
10255
|
59
|
44.0000
|
1320.0000
|
2490.5000
|
0.0176
|
0
|
25 |
10256
|
53
|
26.2000
|
393.0000
|
517.8000
|
0.0505
|
0
|