Solved

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

Posted on 2014-08-16
382 Views
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

the results  are:

peaks  7 ,10 , 7
mins  4 , 6
0
Question by:center1010

LVL 47

Expert Comment

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

LVL 47

Accepted Solution

``````/* 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
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
``````
0

LVL 47

Expert Comment

Thank you. Please indicate you use version 2012 in your questions - it helps us choose the most effective solutions. Cheers, Paul
0

LVL 29

Expert Comment

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

Author Comment

Thank you all, i am useing sql 2012 and 2014 but EE have only SQL 2005-2008  versions on the ASK  sections
0

LVL 47

Expert Comment

Yes, why I cannot say, but do please use a tag or state the version in the question. It will save time.
0

## Featured Post

### Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.