Presentation is loading. Please wait.

Presentation is loading. Please wait.

DBI310. “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the.

Similar presentations


Presentation on theme: "DBI310. “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the."— Presentation transcript:

1 DBI310

2

3

4 “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the grand total, as well as the difference from the grand average” “Calculate bank account balances” “Calculate expected product stock levels/inventory” “Calculate how many days since the previous customer order and until the next” “Compute percentile ranks and percentiles to analyze the distribution of sales”

5

6 Set definition: By a "set" we mean any collection M into a whole of definite, distinct objects m (which are called the "elements" of M) of our perception or of our thought. -- Georg Cantor

7 -- percent of total and diff from average WITH CustAggregates AS ( SELECT custid, SUM(val) AS sumval, AVG(val) AS avgval FROM Sales.OrderValues GROUP BY custid ), GrandAggregates AS ( SELECT SUM(val) AS sumval, AVG(val) AS avgval FROM Sales.OrderValues ) SELECT O.orderid, O.custid, O.val, O.val / CA.sumval AS pctcust, O.val - CA.avgval AS diffcust, O.val / GA.sumval AS pctall, O.val - GA.avgval AS diffall FROM Sales.OrderValues AS O JOIN CustAggregates AS CA ON O.custid = CA.custid CROSS JOIN GrandAggregates AS GA;

8 -- percent of total and diff from average with filter SELECT orderid, custid, val, val / (SELECT SUM(O2.val) FROM Sales.OrderValues AS O2 WHERE O2.custid = O1.custid AND orderdate >= ' ' AND orderdate < ' ') AS pctcust, val - (SELECT AVG(O2.val) FROM Sales.OrderValues AS O2 WHERE O2.custid = O1.custid AND orderdate >= ' ' AND orderdate < ' ') AS diffcust, val / (SELECT SUM(O2.val) FROM Sales.OrderValues AS O2 WHERE orderdate >= ' ' AND orderdate < ' ') AS pctall, val - (SELECT AVG(O2.val) FROM Sales.OrderValues AS O2 WHERE orderdate >= ' ' AND orderdate < ' ') AS diffall FROM Sales.OrderValues AS O1 WHERE orderdate >= ' ' AND orderdate < ' ';

9 -- islands (e.g., consecutive periods of activity) SELECT MIN(col1) AS start_range, MAX(col1) AS end_range FROM (SELECT col1, (SELECT MIN(B.col1) FROM dbo.T1 AS B WHERE B.col1 >= A.col1 AND NOT EXISTS (SELECT * FROM dbo.T1 AS C WHERE C.col1 = B.col1 + 1)) AS grp FROM dbo.T1 AS A) AS D GROUP BY grp;

10 -- account balances SELECT S1.actid, S1.tranid, S1.val, SUM(S2.val) AS balance FROM dbo.Accounts AS S1 JOIN dbo.Accounts AS S2 ON S2.actid = S1.actid AND S2.tranid <= S1.tranid GROUP BY S1.actid, S1.tranid, S1.val;

11

12 Set Function -- account balances SELECT actid, tranid, val, SUM(val) OVER(PARTITION BY actid ORDER BY tranid ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance FROM dbo.Accounts;

13

14 orderid orderdate val rnk :00: :00: :00: :00: :00: SELECT orderid, orderdate, val, RANK() OVER(ORDER BY val DESC) AS rnk FROM Sales.OrderValues;

15 orderid orderdate val rnk :00: :00: :00: :00: :00: SELECT orderid, orderdate, val, RANK() OVER(ORDER BY val DESC) AS rnk FROM Sales.OrderValues;

16 -- percent of total and diff from average SELECT orderid, custid, val, val / SUM(val) OVER(PARTITION BY custid) AS pctcust, val - AVG(val) OVER(PARTITION BY custid) AS diffcust, val / SUM(val) OVER() AS pctall, val - AVG(val) OVER() AS diffall FROM Sales.OrderValues WHERE orderdate >= ' ' AND orderdate < ' ';

17 -- window functions SELECT orderid, custid, val, val / SUM(val) OVER(PARTITION BY custid) AS pctcust, val - AVG(val) OVER(PARTITION BY custid) AS diffcust, val / SUM(val) OVER() AS pctall, val - AVG(val) OVER() AS diffall FROM Sales.OrderValues WHERE orderdate >= ' ' AND orderdate < ' '; -- traditional SELECT orderid, custid, val, val / (SELECT SUM(O2.val) FROM Sales.OrderValues AS O2 WHERE O2.custid = O1.custid AND orderdate >= ' ' AND orderdate < ' ') AS pctcust, val - (SELECT AVG(O2.val) FROM Sales.OrderValues AS O2 WHERE O2.custid = O1.custid AND orderdate >= ' ' AND orderdate < ' ') AS diffcust, val / (SELECT SUM(O2.val) FROM Sales.OrderValues AS O2 WHERE orderdate >= ' ' AND orderdate < ' ') AS pctall, val - (SELECT AVG(O2.val) FROM Sales.OrderValues AS O2 WHERE orderdate >= ' ' AND orderdate < ' ') AS diffall FROM Sales.OrderValues AS O1 WHERE orderdate >= ' ' AND orderdate < ' ';

18 -- account balances SELECT actid, tranid, val, SUM(val) OVER(PARTITION BY actid ORDER BY tranid ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance FROM dbo.Accounts;

19

20

21 -- window functions SELECT actid, tranid, val, SUM(val) OVER(PARTITION BY actid ORDER BY tranid) AS balance FROM dbo.Accounts; -- traditional SELECT S1.actid, S1.tranid, S1.val, SUM(S2.val) AS balance FROM dbo.Accounts AS S1 JOIN dbo.Accounts AS S2 ON S2.actid = S1.actid AND S2.tranid <= S1.tranid GROUP BY S1.actid, S1.tranid, S1.val;

22 -- window functions SELECT custid, orderdate, orderid, DATEDIFF(day, LAG(orderdate) OVER(PARTITION BY custid ORDER BY orderdate, orderid), orderdate) AS diff FROM Sales.Orders; -- traditional SELECT custid, orderdate, orderid, DATEDIFF(day, (SELECT TOP (1) I.orderdate FROM Sales.Orders AS I WHERE I.custid = O.custid AND I.orderdate <= O.orderdate AND (I.orderdate < O.orderdate OR I.orderid < O.orderid) ORDER BY orderdate DESC, orderid DESC), orderdate) AS diff FROM Sales.Orders AS O;

23

24 -- traditional SELECT MIN(col1) AS start_range, MAX(col1) AS end_range FROM (SELECT col1, (SELECT MIN(B.col1) FROM dbo.T1 AS B WHERE B.col1 >= A.col1 AND NOT EXISTS (SELECT * FROM dbo.T1 AS C WHERE C.col1 = B.col1 + 1)) AS grp FROM dbo.T1 AS A) AS D GROUP BY grp; -- window functions SELECT MIN(col1) AS start_range, MAX(col1) AS end_range FROM (SELECT col1, col1 - ROW_NUMBER() OVER(ORDER BY col1) AS grp FROM dbo.T1) AS D GROUP BY grp;

25

26 Questions? Thank You!

27 Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Try the new SQL Server Mission Critical BareMetal Hand’s on-Labs Visit the updated website for SQL Server® Code Name “Denali” on and sign to be notified when the next CTP is available Follow Twitter account to watch for updates Visit the SQL Server Product Demo Stations in the DBI Track section of the Expo/TLC Hall. Bring your questions, ideas and conversations! Microsoft® SQL Server® Security & ManagementMicrosoft® SQL Server® Optimization and Scalability Microsoft® SQL Server® ProgrammabilityMicrosoft® SQL Server® Data Warehousing Microsoft® SQL Server® Mission Critical Microsoft® SQL Server® Data Integration

28 Resources Sessions On-Demand & CommunityMicrosoft Certification & Training Resources Resources for IT ProfessionalsResources for Developers Connect. Share. Discuss.

29

30 Scan the Tag to evaluate this session now on myTechEd Mobile

31


Download ppt "DBI310. “Rank orders by order value descending” “Return consecutive periods of activity” “Return the percent of the current order value out of the."

Similar presentations


Ads by Google