Solved

t-sql - how to without a cursor implementation

Posted on 2009-03-30
6
489 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how the fundamental information of how to create a table.

733 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