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

Slides:



Advertisements
Similar presentations
The Sales/Collection Business Process Accounts Receivable Query Join & Null to Zero (NZ) considerations 1.
Advertisements

Functions S S T : S P R E A D S H E E T S SST 5 Spreadsheet 5 Function.
1 © Copyright Doug Hillman 1999 Short-Term Financing.
1 © Copyright Doug Hillman 2000 Accounts Receivable and Bad Debts Expense.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
Introduction to Oracle9i: SQL1 SQL Group Functions.
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
Structured Query Language Part I Chapter Three CIS 218.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Objects for Business Reporting MIS 497. Objective Learn about miscellaneous objects required for business reporting. Learn about miscellaneous objects.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
XP. Objectives Sort data and filter data Summarize an Excel table Insert subtotals into a range of data Outline buttons to show or hide details Create.
More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.
Sun Proprietary/Confidential Information © Sun Microsystems 2005 Intelligent Event Processing (IEP) Aggregation Partition Filtering Correlation.
Learning Objectives Understand the Business – LO1 Describe the issues in managing different types of inventory. Study the accounting methods – LO2 Explain.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
© Jalal Kawash Database Queries Peeking into Computer Science.
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 7-3 Accounts Receivable Turnover Ratio.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
Chapter 4 AVERAGE ATOMIC MASS. Atomic Mass… n The weighted average of the masses of all the naturally occurring isotopes of that element. n Is not a whole.
Background Lots of Demos(That’s it.)
Lecture 3 Appendix 1 Computation of the conditional entropy.
Aggregator  Performs aggregate calculations  Components of the Aggregator Transformation Aggregate expression Group by port Sorted Input option Aggregate.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Thinking in Sets and SQL Query Logical Processing.
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Example Materials Power BI Desktop File (4/16): 6WPbWY5UTRhaHYwdDA/view?usp=sharing.
T-SQL Power! Windows That Open Doors Adam
Structured Query Language
Analytic Window Functions
Queries.
Database Systems Subqueries, Aggregation
Data Analysis with SQL Window Functions
LESSON 7-3 Accounts Receivable Turnover Ratio
Using Window Ranking, Offset, and Aggregate Functions
CALCULATING THE ACCOUNTS RECEIVABLE TURNOVER RATIO
LESSON 7-3 Accounts Receivable Turnover Ratio
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Lecture 17: Distributed Transactions
T-SQL Window Functions in Microsoft SQL Server Denali
T-SQL Window Function Deep Dive part 1
Querying Database ISYS 363.
Oracle8i Analytical SQL Features
Chapter 8 Arrays Objectives
Access Quiz.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Physical Storage Indexes Partitions Materialized views March 2004
Physical Storage materialized views indexes partitions ETL CDC.
Introduction to Window Functions
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
Structured Query Language – The Fundamentals
Introduction to Spreadsheets
Query Functions.
Chapter 8 Arrays Objectives
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Aggregate Functions.
Build on-the-fly reporting with Dynamic SQL
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

DBI310

“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”

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

-- 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;

-- 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 < ' ';

-- 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;

-- 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;

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;

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

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

-- 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 < ' ';

-- 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 < ' ';

-- 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;

-- 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;

-- 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;

-- 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;

Questions? Thank You!

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

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

Scan the Tag to evaluate this session now on myTechEd Mobile