[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

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
0
center1010
Asked:
center1010
  • 4
1 Solution
 
PortletPaulCommented:
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
 
PortletPaulCommented:
/* 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
 
PortletPaulCommented:
Thank you. Please indicate you use version 2012 in your questions - it helps us choose the most effective solutions. Cheers, Paul
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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
 
PortletPaulCommented:
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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now