Maths - Delta Query

Hi
I need to find this number series Total Delta
25,      24,      24,      25,      25

My answer is 0, i just wondering is correct or not.
24-25= -1
24-24  = 0
25-24 = 1
25-25 =0

After Add all together to find Delta i got = 0
LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
Not sure exactly why you want it as sql but it's fairly easy to do....


WITH yourdata
     AS (SELECT 1 id, 25 n FROM DUAL
         UNION ALL
         SELECT 2, 24 FROM DUAL
         UNION ALL
         SELECT 3, 24 FROM DUAL
         UNION ALL
         SELECT 4, 25 FROM DUAL
         UNION ALL
         SELECT 5, 25 FROM DUAL)
SELECT SUM(diff)
  FROM (SELECT n - LAG(n) OVER (ORDER BY id) diff FROM yourdata);



Assuming your data is in a table somewhere, simply remove the WITH clause and change yourdata to your table and the other columns as appropriate
0
ukerandiAuthor Commented:
Thanks, But i need Answer for that my formula
Delta means - It is just the difference between any two values.
0
sdstuberCommented:
I know what a delta is

n - LAG(n) OVER (ORDER BY id)   --- there's your delta

but you asked for the sum of them   so sum in the outer query

and that's 0
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ukerandiAuthor Commented:
so that mean my answer is correct?
0
PortletPaulfreelancerCommented:
yes

A variant on that sql that displays the details; the "LAG(N)OVER(ORDERBYID)" column is the n of the previous row.
The column "DIFF" when summed = 0

| LAG(N)OVER(ORDERBYID) |  N |   DIFF |
|-----------------------|----|--------|
|                (null) | 25 | (null) |
|                    25 | 24 |     -1 |
|                    24 | 24 |      0 |
|                    24 | 25 |      1 |
|                    25 | 25 |      0 |

WITH yourdata
      AS (
         SELECT 1 id, 25 n FROM DUAL
         UNION ALL
         SELECT 2, 24 FROM DUAL
         UNION ALL
         SELECT 3, 24 FROM DUAL
         UNION ALL
         SELECT 4, 25 FROM DUAL
         UNION ALL
         SELECT 5, 25 FROM DUAL
            )
SELECT
      LAG(n) OVER (ORDER BY id)
    , n
    , n - LAG(n) OVER (ORDER BY id) diff
FROM yourdata
;

http://sqlfiddle.com/#!4/d41d8/33755

Open in new window

0
sdstuberCommented:
>>> so that mean my answer is correct?

yes, your answer is correct per your description of the operations
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.