Calculate the Running Total(Everything)

Similar documents
Modern SQL: Evolution of a dinosaur

SQL Recursion, Window Queries

Still using. Windows 3.1? So why stick to -

Real-World Performance Training SQL Introduction

SQL Recursion, Window Queries

20461: Querying Microsoft SQL Server 2014 Databases

Postgres Window Magic

MySQL 8.0 Common Table Expressions & Windowing Functions

Insertions, Deletions, and Updates

DB2 SQL Class Outline

Introduction to SQL Server 2005/2008 and Transact SQL

Querying Microsoft SQL Server

Querying Microsoft SQL Server

Querying Data with Transact-SQL (761)

After completing this course, participants will be able to:

AVANTUS TRAINING PTE LTD

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Querying Data with Transact-SQL

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Sql Server Syllabus. Overview

Querying Microsoft SQL Server 2008/2012

COURSE OUTLINE: Querying Microsoft SQL Server

Querying Microsoft SQL Server

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Database Management Systems,

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

Course 20461C: Querying Microsoft SQL Server

Querying Microsoft SQL Server 2012/2014

20461: Querying Microsoft SQL Server

Querying Microsoft SQL Server (MOC 20461C)

20461D: Querying Microsoft SQL Server

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Slides by: Ms. Shree Jaswal

Data Manipulation Language (DML)

Querying Data with Transact SQL

20761 Querying Data with Transact SQL

Querying Microsoft SQL Server

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

More SQL in MySQL 8.0

Rows and Range, Preceding and Following

Microsoft Querying Data with Transact-SQL - Performance Course

Querying Microsoft SQL Server

SQL Data Query Language

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Greenplum SQL Class Outline

Querying Data with Transact-SQL

CMPT 354: Database System I. Lecture 4. SQL Advanced

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Querying Data with Transact-SQL

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

CS2 Current Technologies Lecture 2: SQL Programming Basics

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Advanced SQL Tribal Data Workshop Joe Nowinski

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

Simple SQL Queries (contd.)

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

Querying Data with Transact-SQL

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

20761B: QUERYING DATA WITH TRANSACT-SQL

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Cursors Christian S. Jensen, Richard T. Snodgrass, and T. Y. Cliff Leung

Database Systems Fundamentals

Lab # 6. Data Manipulation Language (DML)

Microsoft Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL

The Relational Algebra

Querying Data with Transact-SQL

Database Management Systems,

Querying Data with Transact-SQL

Database Technology. Topic 3: SQL. Olaf Hartig.

Querying Data with Transact-SQL

In This Lecture. More SQL Select. SQL SELECT Overview. Aliases. Example. Example. More SQL Select. Database Systems Lecture 8 Natasha Alechina

Querying Microsoft SQL Server 2014

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Duration Level Technology Delivery Method Training Credits. Classroom ILT 5 Days Intermediate SQL Server

Querying Data with Transact-SQL

A Window into Your Data. Using SQL Window Functions

Database Lab Queries. Fall Term 2017 Dr. Andreas Geppert

Introduction to SQL Server 2005/2008 and Transact SQL

Database design process

The Plan. What will we cover? - Review Some Basics - Set Operators - Subqueries - Aggregate Filter Clause - Window Functions Galore - CTE s - Lateral

Including missing data

20761C: Querying Data with Transact-SQL

Microsoft Power Tools for Data Analysis #7 Power Query 6 Types of Merges/ Joins 9 Examples Notes from Video:

Chapter 9 Windowed Tables and Window Functions in SQL. Recent Development for Data Models 2016 Stefan Deßloch

CSC Web Programming. Introduction to SQL

SQL STRUCTURED QUERY LANGUAGE

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

SQL functions fit into two broad categories: Data definition language Data manipulation language

Oracle Syllabus Course code-r10605 SQL

MIS NETWORK ADMINISTRATOR PROGRAM

Creating a Pivot Table

Copyright 2017, Oracle and/or its aff iliates. All rights reserved.

SQL, the underestimated Big Data technology

Aster Data SQL and MapReduce Class Outline

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Transcription:

Calculate the Running Total(Everything Objective: List the running total of the annual salaries. SELECT EmployeeName,AnnualSalary,SUM(AnnualSalary OVER(ORDER BY EmployeeName AS Running_Total Employee --Checking SELECT SUM(AnnualSalary AS total Employee EmployeeName AnnualSalary Running_Total Bruce 101000 101000 Chris 30000 131000 Eric 65000 196000 Jennifer 95000 291000 Joe 90000 381000 John 35000 416000 Mark 70000 486000 Mary 60000 546000 Mike 82000 628000 Paul 30000 658000 Steve 65000 723000

Calculate the Running Total (Like Items Only Note: The objective in this case is only to add the same items as a running total in another column. I have a table like this. Date Item BuyItem ---------------------------------------------- 20150101 Mouse 10 20150101 Keyboard 100 20150202 Mouse 20 20150202 Keyboard 200 I want to query like this. Date Item BuyItem RunningTotal ------------------------------------------------------------------------- 20150101 Mouse 10 10 20150202 Mouse 20 30 20150101 Keyboard 100 100 20150202 Keyboard 200 300 Try using CROSS APPLY (BEST WAY or Correlated sub-query. ;WITH cte AS ( SELECT * (VALUES (20150101,'Mouse',10, (20150101,'Keyboard',100, (20150202,'Mouse',20, (20150202,'Keyboard',200 tc([date], Item, BuyItem SELECT * cte a CROSS APPLY(SELECT SUM(BuyItem AS running_total cte b WHERE a.item = b.item AND a.[date] >= b.[date] cs Result: Date Item BuyItem Running_Total -------------------------------------------------------------- 20150101 Mouse 10 10 20150202 Mouse 20 30 20150101 Keyboard 100 100 20150202 Keyboard 200 300

Recursive CTE method using ROW_NUMBER function and PARTITION: ;WITH cte AS (SELECT ROW_NUMBER(OVER(PARTITION BY Item ORDER BY [date] AS rn,* (VALUES (20150101,'Mouse',10, (20150101,'Keyboard',100, (20150202,'Mouse',20, (20150202,'Keyboard',200 tc([date], Item, BuyItem, CTE_RunningTotal AS (SELECT [Date],Item,BuyItem,BuyItem AS running_total,rn cte WHERE rn = 1 UNION ALL SELECT T.[Date],T.Item,t.BuyItem, T.BuyItem + C.running_total AS running_total, t.rn CTE_RunningTotal AS C INNER JOIN cte AS T ON T.Item = c.item AND t.rn = C.rn + 1 SELECT [Date], Item, BuyItem, running_total CTE_RunningTotal AS C Note: Better to update your server to 2012 which can use sum( over(order by method to calculate running total which much faster than these methods --Using CROSS APPLY: SELECT [Date], item, running_total #yourtable a CROSS APPLY(SELECT SUM(BuyItem AS running_total #yourtable b WHERE a.item = b.item AND a.[date] >= b.[date] ca ORDER BY BuyItem

--Make a use of windowing function. in SQL 2012+ DECLARE @Items TABLE ( DATE NVARCHAR(MAX, Item NVARCHAR(MAX, BuyItem INT INSERT INTO @Items([DATE], Item, BuyItem VALUES('20150101', 'Mouse', 10 INSERT INTO @Items([DATE], Item, BuyItem VALUES('20150101', 'Keyboard', 100 INSERT INTO @Items([DATE], Item, BuyItem VALUES('20150202', 'Mouse', 20 INSERT INTO @Items([DATE], Item, BuyItem VALUES('20150202', 'Keyboard', 200 SELECT [DATE], Item, SUM(BuyItem OVER (PARTITION BY Item ORDER BY BuyItem AS RunningTotal @Items ORDER BY Item DESC Result: Date Item BuyItem Running_Total ------------------------------------------------------------------- 20150101 Mouse 10 10 20150202 Mouse 20 30 20150101 Keyboard 100 100 20150202 Keyboard 200 300 In aggregate function, with running total, using Window functions are very good performance In SQL 2012+: SELECT *, SUM( OVER(PARTITION BY Item, ORDER BY [Date] AS RunningTotal Your_Table ORDER BY Item DESC --Faster than when add window frame: SELECT *, SUM( OVER(PARTITION BY Item, ORDER BY [Date] ROWS UNBOUNDED PRECEDING AS RunningTotal Your_Table ORDER BY Item DESC

Reverse Cumulative Creates a reverse cumulative (minus the cumulative column on each row to the next I have this table: id ID [Date] Cumulative ------------------------------------ 1 x Jan-10 10 3 x Feb-10 40 7 x Apr-10 60 9 x May-10 100 2 y Jan-10 20 6 y Mar-10 40 8 y Apr-10 60 10 y May-10 100 I need to Reverse the "Cumulative" in MS SQL Server Query to be as the following: id ID [Date] Cumulative Reversed ------------------------------------ 1 x Jan-10 10 10 3 x Feb-10 40 30 -- 10 40 = 30 7 x Apr-10 60 20 -- 40 60 = 20 9 x May-10 100 40 -- 60 100 = 40 2 y Jan-10 20 20 -- 40 20 = 20 6 y Mar-10 40 20 -- 20 40 = 20 8 y Apr-10 60 20 -- 40 60 = 20 10 y May-10 100 40 -- 60 100 = 40 For below SQL server 2012 using recursive CTE for reverse running total. DECLARE @t TABLE(id INT,IDs VARCHAR(20,Dates VARCHAR(20,Cumulative INT INSERT INTO @t VALUES (1,'x','Jan-10', 10,(3,'x','Feb-10', 40,(7,'x','Apr-10', 60,(9,'x','May-10', 100,(2,'y','Jan-10', 20,(6,'y','Mar-10', 40,(8,'y','Apr-10', 60,(10,'y','May-10',100

;WITH CTE AS (SELECT *,row_number(over(partition BY ids ORDER BY idrn @t,cte1 AS (SELECT id,ids,dates, Cumulative,rn,Cumulative Reversed cte WHERE rn=1 UNION ALL SELECT c.id,c.ids,c.dates,c.cumulative,c.rn,c.cumulative - c1.cumulative cte c INNER JOIN cte c1 ON c.ids=c1.ids WHERE c.rn = c1.rn+1 SELECT * cte1 You can use lag to get the value in the previous row and subtract from the current row's value to get the reversed value. SELECT t.*, Cumulative - COALESCE(lag(Cumulative OVER(PARTITION BY id ORDER BY [Date],0 AS Reversed tablename t You can use lag(cumulative,1,0 instead of coalesce. SELECT t.*, Cumulative-lag(Cumulative,1,0 OVER(PARTITION BY id ORDER BY [Date] AS Reversed tablename t

Total of Each Day of Week Gets the total of each day of the week and then organizes the information with each day of the week in columns and the total weekly sum underneath using the pivot function. ;WITH cte AS ( SELECT DATENAME(dw,releasetime as DayOfWeekName,COUNT(* OVER ( as TotalCount @release WHERE releasetime >= DATEADD(DAY,- DATEPART(dw,GETDATE( + 1,CAST(GETDATE( AS DATE AND releasetime < DATEADD(DAY,7 - DATEPART(dw,GETDATE( + 1,CAST(GETDATE( AS DATE SELECT * cte PIVOT ( COUNT(DayOfWeekName FOR DayOfWeekName IN (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday p