T-sql to find "peaks" and "mins" in set of numbers

I need  1 or 2  T-sql Queries ,  to find  peaks (in red)  ,  and the second  the mins (in blue) in a set of numbers like in the picture

 Peaks and Mins
the results  are:

peaks  7 ,10 , 7
mins  4 , 6
center1010Asked:
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.

PortletPaulfreelancerCommented:
what version of SQL Server please (it does matter here)
specifically it will be useful to have the LEAD/LAG functions (available from sql 2012)
0
PortletPaulfreelancerCommented:
/* for the peaks/troughs only */
select
       case when pt = 0 then anum end as troughs
     , case when pt = 1 then anum end as peaks
from (
      select
            aNum
          , case
                 when  lag(anum) over(order by (select 1)) < anum 
                  and lead(anum) over(order by (select 1)) < anum then 1 -- peak
                 when  lag(anum) over(order by (select 1)) > anum 
                  and lead(anum) over(order by (select 1)) > anum then 0 -- trough
            end as pt
      from sample
     ) sq
where pt is not null
;

| TROUGHS |  PEAKS |
|---------|--------|
|  (null) |      7 |
|       4 | (null) |
|  (null) |     10 |
|       6 | (null) |
|  (null) |      7 |

/* to see the detail */
select
*
, case
       when lagger < anum and leader < anum then 1 -- peak
       when lagger > anum and leader > anum then 0 -- trough
  end
from (
      select
            lag(anum) over(order by (select 1)) lagger
          , aNum
          , lead(anum) over(order by (select 1)) leader
      from sample
     ) sq
;

| LAGGER | ANUM | LEADER | COLUMN_3 |
|--------|------|--------|----------|
| (null) |    1 |      2 |   (null) |
|      1 |    2 |      3 |   (null) |
|      2 |    3 |      4 |   (null) |
|      3 |    4 |      5 |   (null) |
|      4 |    5 |      6 |   (null) |
|      5 |    6 |      7 |   (null) |
|      6 |    7 |      6 |        1 |
|      7 |    6 |      5 |   (null) |
|      6 |    5 |      4 |   (null) |
|      5 |    4 |      6 |        0 |
|      4 |    6 |      8 |   (null) |
|      6 |    8 |      9 |   (null) |
|      8 |    9 |     10 |   (null) |
|      9 |   10 |      9 |        1 |
|     10 |    9 |      8 |   (null) |
|      9 |    8 |      7 |   (null) |
|      8 |    7 |      6 |   (null) |
|      7 |    6 |      7 |        0 |
|      6 |    7 |      5 |        1 |
|      7 |    5 | (null) |   (null) |
		
http://sqlfiddle.com/#!6/e23f2/8

Open in new window

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
PortletPaulfreelancerCommented:
Thank you. Please indicate you use version 2012 in your questions - it helps us choose the most effective solutions. Cheers, Paul
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Olaf DoschkeSoftware DeveloperCommented:
The solution is fine, it just will give you many peaks just being a little bump in an otherwise rising or sinking trend. eg in a sequence 0,100,99,300,400 the 100 will be a max and 99 a min peak, though thei're not very distinctive peaks.

There's lot's of mathematical analysis software for stock exchange going in that direction.

You should perhaps calculate the diffs to get slopes and then find distinct peaks where slopes change their sign clearly and don't just slinger about zero.

You'd do such analysis with arrays, collections in memory much faster then via SQL, SQL is not the best language to use for mathematical analysis of mass data, though the source might be a database, that's not a problem of course.

Bye, Olaf.

@PortletPaul I agree knowing the SQL Server version will help in many cases. It's a pitty EE stopped introducing newer sections and 2008 is still the last section. On the other side it would scatter the scatter the questions too much, there should be sections and version specifications, that would be nicer, not only for MSSQL.
0
center1010Author Commented:
Thank you all, i am useing sql 2012 and 2014 but EE have only SQL 2005-2008  versions on the ASK  sections
0
PortletPaulfreelancerCommented:
Yes, why I cannot say, but do please use a tag or state the version in the question. It will save time.
0
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
Microsoft SQL Server 2008

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.