[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

% change between rows

Posted on 2002-04-12
6
Medium Priority
?
286 Views
Last Modified: 2006-11-17
I want to get something like the following.

week count % changed
10   100     0
11   200     100%
12   300     50%
13   100     -66.7%
14   500     500%


What's the simplest way to do this?
0
Comment
Question by:gotaquestion
[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 70

Expert Comment

by:Scott Pletcher
ID: 6937407
Perhaps something like this:

SELECT week, [count],
    (SELECT ISNULL(CAST((currwk.[count] - prevwk.[count]) AS DECIMAL(10,2)) /
            CAST(currwk.[count] AS DECIMAL(10,2)) * 100.00,0) AS '% changed'
     FROM tablename prevwk
     WHERE prevwk.week = currwk.week - 1)
FROM tablename currwk
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 6937423
This will find the percentage change in records. in a table provided there is a column named week.  Change Column1 to the name of any field in the table other than Week.

SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C1 [% Change]
From
     (select T1.[Week], Count(T1.Column1) C1,
         (Select IsNull(Count(T2.Column1) from MyTable T2 Where T2.[Week] = T1.[Week]-1),0) C2
     From MyTable T1
     Group By T1.[Week]) VT


If you don't have a week field, then you can use the datepart function to get the date from a create date:


SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C2 [% Change]
From
     (select DatePart(wk,T1.CreateDt) [Week],
         Count(T1.Column1) C1,
         (Select IsNull(Count(T2.Column1)
             from MyTable T2
             Where DatePart(wk,T2.CreateDt) =
                 DatePart(wk,T1.CreateDt)-1),0) C2
      From MyTable T1
      Group By DatePart(wk,T1.CreateDt)-1)) VT


0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 6937435
Minor Change:  Had to divide by C2 rather than C1:

SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C2 [% Change]
From
    (select T1.[Week], Count(T1.Column1) C1,
        (Select IsNull(Count(T2.Column1) from MyTable T2 Where T2.[Week] = T1.[Week]-1),0) C2
    From MyTable T1
    Group By T1.[Week]) VT


Or with the datepart function:

SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C2 [% Change]
From
    (select DatePart(wk,T1.CreateDt) [Week],
        Count(T1.Column1) C1,
        (Select IsNull(Count(T2.Column1)
            from MyTable T2
            Where DatePart(wk,T2.CreateDt) =
                DatePart(wk,T1.CreateDt)-1),0) C2
     From MyTable T1
     Group By DatePart(wk,T1.CreateDt)-1)) VT
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 1

Author Comment

by:gotaquestion
ID: 6937507
I've created a table to support your queries and with both the last ones your proposed I'm getting.  

Server: Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'from'.

I think it's missing a )

I can't seem to see it.


0
 
LVL 5

Accepted Solution

by:
spcmnspff earned 400 total points
ID: 6937548
Oops.  Fixed it.  I had to change the  postition of my isnull =)  :

SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C2 [% Change]
From
   (select T1.[Week], Count(T1.Column1) C1,
       IsNull((Select Count(T2.Column1) from MyTable T2 Where T2.[Week] = T1.[Week]-1),0) C2
   From MyTable T1
   Group By T1.[Week]) VT


Or with the datepart function:

SET ARITHABORT OFF
Select [Week], C1 [Count], (C1-C2)/C2 [% Change]
From
   (select DatePart(wk,T1.CreateDt) [Week],
       Count(T1.Column1) C1,
       IsNull((Select Count(T2.Column1)
           from MyTable T2
           Where DatePart(wk,T2.CreateDt) =
               DatePart(wk,T1.CreateDt)-1),0) C2
    From MyTable T1
    Group By DatePart(wk,T1.CreateDt)) VT
0
 
LVL 1

Author Comment

by:gotaquestion
ID: 6937619
Thanks, I had to fix the divide by zero error because the
SET ARITHIGNORE OFF
SET ARITHABORT OFF
were not working for some reason.

Select [Week], C1 [Count], CASE C2 WHEN 0 THEN 0 ELSE (C1-C2)/C2 END [% Change]
From
  (select T1.[Week], sum(T1.Column1) C1,
      IsNull((Select sum(T2.Column1) from MyTable T2 Where T2.[Week] = T1.[Week]-1),0) C2
  From MyTable T1
  Group By T1.[Week]) VT
0

Featured Post

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!

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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

656 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