Simon
4Penny.net
Points: 7383

2/13/2019 11:17:43 AM

SQL Puzzler

* This article, and all our great SQL (General) documentation, Is available on the SQL (General) menu

Welcome to a new feature, SQL Puzzlers! Hope you enjoy them. Follow the link above to see all that we have. 

Here is a puzzler for the SQL types in the crowd

Given this dataset:

 

How would you code a running total?

 

I've gotten you started, here is the code that produces the first window, all you have to do is code the RunningTotal column. 

declare @t table (RowID int identity, Docdate date, Sales numeric(19,2))
insert into @t (docdate, Sales) values ('1/1/2019',100)
insert into @t (docdate, Sales) values ('1/2/2019',200)
insert into @t (docdate, Sales) values ('1/3/2019',300)
insert into @t (docdate, Sales) values ('1/4/2019',400)
insert into @t (docdate, Sales) values ('1/5/2019',500)
 
select *
    from @t
    order by docdate

The answer is below, peek at your own peril. 

 

 

4Penny.net
Version: All
Section: SQL Scripts
Table Definition Quick Links
All Tables
SOP Tables
RM Tables
GL Tables
POP Tables
HR Tables
PM Tables
UPR Tables
IV Tables
Olympic Tables
3