Free Newsletters:
DatabaseJournal  
DBANews
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion Video
internet.com

» HOME
» NEWS
» VIDEO
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner


















Microsoft Issues Warnings on IE 8 Beta 2

Comcast Pushes Ahead on Broadband Throttling

The Microsoft-Novell Deal and Trust in Princes

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers



Senior Systems Analyst
I T Search
US-PA-Wilkes Barre

Justtechjobs.com Post A Job | Post A Resume
MS SQL
July 7, 2004
Auto-Number and Cumulative sum in SQL Server Query results
By Muthusamy Anantha Kumar aka The MAK

Example 3:

Generate unique sequence numbers for a table for every group.

Let us consider a table as shown below.

use tempdb
go
create table mytable3 (col1 int, col2 datetime, uniq int)

insert into mytable3 select 1111,getdate(),1
insert into mytable3 select 1111,getdate() ,2
insert into mytable3 select 1111,getdate(),3 
insert into mytable3 select 1111,getdate() ,4
insert into mytable3 select 1111,getdate() ,5
insert into mytable3 select 1111,getdate() ,10
insert into mytable3 select 2222,getdate() ,120
insert into mytable3 select 2222,getdate() ,123
insert into mytable3 select 2222,getdate() ,1234
insert into mytable3 select 2222,getdate() ,1566
insert into mytable3 select 3333,getdate() ,1567
insert into mytable3 select 3333,getdate() ,1588
go

Query

Select * from mytable3

Results

1111

4/19/04 1:42 PM

1

1111

4/19/04 1:42 PM

2

1111

4/19/04 1:42 PM

3

1111

4/19/04 1:42 PM

4

1111

4/19/04 1:42 PM

5

1111

4/19/04 1:42 PM

10

2222

4/19/04 1:42 PM

120

2222

4/19/04 1:42 PM

123

2222

4/19/04 1:42 PM

1234

2222

4/19/04 1:42 PM

1566

3333

4/19/04 1:42 PM

1567

3333

4/19/04 1:42 PM

1588

Query

select col1,col2,col3=
Case when col1 = col1 then 
(select count(*) from mytable3 a where 
a.Col1 = mytable3.Col1 and a.uniq < mytable3.uniq)+1 end 
from mytable3

Results

1111

4/19/04 1:42 PM

1

1111

4/19/04 1:42 PM

2

1111

4/19/04 1:42 PM

3

1111

4/19/04 1:42 PM

4

1111

4/19/04 1:42 PM

5

1111

4/19/04 1:42 PM

6

2222

4/19/04 1:42 PM

1

2222

4/19/04 1:42 PM

2

2222

4/19/04 1:42 PM

3

2222

4/19/04 1:42 PM

4

3333

4/19/04 1:42 PM

1

3333

4/19/04 1:42 PM

2

Note: "Uniq" is a unique column.

Example 4:

Generate cumulative sum for a table. Let us consider the table shown below.

USE TEMPDB
GO
CREATE TABLE Inventory
(
pno char(12) NOT NULL,
movedate datetime NOT NULL,
qty int NOT NULL
)
GO
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/1/2004',100)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/2/2004',120)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/4/2004',-150)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/5/2004',50)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/06/2004',-35)
GO

Query

select * from Inventory

Results

P01

3/1/04 12:00 AM

100

P01

3/2/04 12:00 AM

120

P01

3/4/04 12:00 AM

-150

P01

3/5/04 12:00 AM

50

P01

3/6/04 12:00 AM

-35

Query

SELECT PNO,movedate,qty,(SELECT SUM(qty)
FROM Inventory AS x
WHERE y.movedate >= x.movedate) AS
qtyinhand
FROM Inventory AS y

Results

P01

3/1/04 12:00 AM

100

100

P01

3/2/04 12:00 AM

120

220

P01

3/4/04 12:00 AM

-150

70

P01

3/5/04 12:00 AM

50

120

P01

3/6/04 12:00 AM

-35

85

Note: "MoveDate" is a unique column.

Example 5:

Generate a cumulative sum for a table for every group. Let us consider the table shown below.

USE TEMPDB
GO
CREATE TABLE Inventory
(
pno char(12) NOT NULL,
movedate datetime NOT NULL,
qty int NOT NULL
)
GO
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/1/2004',100)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/2/2004',120)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/4/2004',-150)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/5/2004',50)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P01','03/06/2004',-35)
GO
INSERT INTO Inventory(pno,movedate,qty) VALUES('P02','03/7/2004',-150)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P02','03/8/2004',50)
INSERT INTO Inventory(pno,movedate,qty) VALUES('P02','03/9/2004',-35)
Go

Query

select * from Inventory

Results

P01

3/1/04 12:00 AM

100

P01

3/2/04 12:00 AM

120

P01

3/4/04 12:00 AM

-150

P01

3/5/04 12:00 AM

50

P01

3/6/04 12:00 AM

-35

P02

3/7/04 12:00 AM

-150

P02

3/8/04 12:00 AM

50

P02

3/9/04 12:00 AM

-35

Query

select pno,movedate,qty,cumulative=
Case when pno= pno then 
(select sum(qty) from inventory a where a.pno = inventory .pno and a.movedate 
<= inventory.movedate) end 
from inventory

Results

P01

3/1/04 12:00 AM

100

100

P01

3/2/04 12:00 AM

120

220

P01

3/4/04 12:00 AM

-150

70

P01

3/5/04 12:00 AM

50

120

P01

3/6/04 12:00 AM

-35

85

P02

3/7/04 12:00 AM

-150

-150

P02

3/8/04 12:00 AM

50

-100

P02

3/9/04 12:00 AM

-35

-135

Note: "MoveDate" is a unique column.

Conclusion

As mentioned before, this article is meant to guide developers and database architects to use co-related sub-queries and/or identity functions to generate sequential numbers and cumulative summation in query results.

» See All Articles by Columnist MAK

Go to page: Prev  1  2  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives







Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL Hot back up halidziya 9 September 4th, 04:46 PM
Slow query execution with strange issue xena 3 September 3rd, 02:29 PM
SQL Server 2005 security settings neil-j 1 August 31st, 11:14 PM
MS SQL Server 2005 help.. jeffmc21 3 August 17th, 09:32 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES