Solved

t-sql - how to without a cursor implementation

Posted on 2009-03-30
6
490 Views
Last Modified: 2012-05-06
Here is a very straightford sql to get me counts from the group by clause:

select
count(*) col1
from
tsys..tbl_base
group
by
base_month,
base_year

It returns:

col1
238875
239221
239454
239702
239965
240292
240708
241174
241624
241999
242426


Now without using a cursor I want to add a column to the result set to show the change in count.
Can you show me the sql that might accomplish this?
0
Comment
Question by:michaelrobertfrench
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:BryanMI
ID: 24020576
Perhaps I'm not understanding the question...  much like what you did, I built this sample.  Can you tell me what else you're needing to return?
CREATE TABLE #Test (Month int, Year int, data varchar(10))
 
SET NOCOUNT ON
 
INSERT #Test (Month, Year, data) VALUES (12, 2008, 'test1')
INSERT #Test (Month, Year, data) VALUES (12, 2008, 'test2')
INSERT #Test (Month, Year, data) VALUES (12, 2008, 'test3')
 
INSERT #Test (Month, Year, data) VALUES (11, 2007, 'test4')
INSERT #Test (Month, Year, data) VALUES (11, 2007, 'test5')
INSERT #Test (Month, Year, data) VALUES (11, 2007, 'test6')
INSERT #Test (Month, Year, data) VALUES (11, 2007, 'test7')
 
INSERT #Test (Month, Year, data) VALUES (10, 2007, 'test8')
INSERT #Test (Month, Year, data) VALUES (10, 2007, 'test9')
 
SET NOCOUNT OFF
 
--RETURNS RECORD COUNT BY YEAR
SELECT Count(*) As RecordTotal, Year FROM #Test GROUP BY Year
 
--RETURNS RECORD COUNT BY MONTH AND YEAR
SELECT Count(*) As RecordTotal, Year, Month FROM #Test GROUP BY Year, Month
 
DROP TABLE #Test

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24021177
what version of sql server are you using?
0
 

Author Comment

by:michaelrobertfrench
ID: 24022461
sql 2000
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:michaelrobertfrench
ID: 24022517
The result set would look like this:

col1      change_from_prior period
238875      
239221      346
239454      233
239702      248
239965      263
240292      327
240708      416
241174      466
241624      450
241999      375
242426      427

The change_from_prior_period is what I need the sql to provide.  An easy implementation with a cursor or a while loop but I want sql returning this result set.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24022590
this should do:
declare @t table ( col1 int, r int identity )
set nocount on
insert into @t (col1)
select count(*) col1
from tsys..tbl_base
group by base_year, base_month
order by base_year, base_month 
 
select t.col1, t.col1 - p.col1
  from @t t
  left join @t p
    on p.r = t.r-1
 order by t.r

Open in new window

0
 

Author Closing Comment

by:michaelrobertfrench
ID: 31564402
Yes - Thank You Very Much
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question