# 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
###### 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.

Commented:
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
Author Commented:
Thanks, But i need Answer for that my formula
Delta means - It is just the difference between any two values.
0
Commented:
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
Author Commented:
so that mean my answer is correct?
0
freelancerCommented:
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
``````
0
Commented:
>>> so that mean my answer is correct?

0

Experts Exchange Solution brought to you by

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

###### 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.