-
Notifications
You must be signed in to change notification settings - Fork 1
/
PaginationSubTotal.sql
76 lines (70 loc) · 1.83 KB
/
PaginationSubTotal.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
USE AdventureWorks2012
GO
DECLARE @PageSize INT = 20
DECLARE @PageNumber INT = 0
DECLARE @RowCount INT = @PageSize
DECLARE @LastPage INT
SELECT COUNT(DISTINCT p.Name)
FROM
Production.Product p
INNER JOIN [Production].[WorkOrder] w
ON p.ProductID = w.ProductID
WHERE StartDate BETWEEN '2012-12-01' AND '2013-12-01'
--SELECT @LastPage = CEILING(CONVERT(float, (SELECT COUNT(DISTINCT Name) FROM Production.Product) / @PageSize))
DROP TABLE IF EXISTS #ProductList
CREATE TABLE #ProductList
( ProductName varchar(100) NOT NULL PRIMARY KEY CLUSTERED,
ProductID INT NOT NULL
)
INSERT INTO #ProductList
SELECT DISTINCT
p.Name, w.ProductID
FROM
Production.Product p
INNER JOIN
[Production].[WorkOrder] w
ON p.ProductID = w.ProductID
WHERE StartDate BETWEEN '2012-12-01' AND '2013-12-01'
ORDER BY p.Name
;WITH CTE_PageData AS
(
SELECT
p.ProductID,
p.Name AS ProductName,
MIN(w.StartDate) AS MinStartDate,
MAX(w.StartDate) AS MaxStartDate,
SUM(w.OrderQty) AS OrderQty,
SUM(p.ListPrice * w.OrderQty) AS OrderValue
FROM
#ProductList pl
INNER JOIN
[Production].[WorkOrder] w
ON pl.ProductID = w.ProductID
INNER JOIN
Production.Product p
ON p.ProductID = w.ProductID
WHERE StartDate BETWEEN '2012-12-01' AND '2013-12-01'
GROUP BY p.ProductID, p.Name
ORDER BY ProductName
OFFSET (@PageSize * @PageNumber) rows fetch next @PageSize rows only
)
SELECT
@PageNumber AS PageNumber,
ProductID,
ProductName,
MinStartDate,
MaxStartDate,
OrderQty,
OrderValue
FROM
CTE_PageData
UNION
SELECT
@PageNumber AS PageNumber,
NULL AS ProductID,
'TOTAL' AS ProductName,
MIN(MinStartDate) AS MinStartDate,
MAX(MaxStartDate) AS MaxStartDate,
SUM(OrderQty) AS OrderQty,
SUM(OrderValue) AS OrderValue
from CTE_PageData