Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:41:18+00:00 2026-06-07T21:41:18+00:00

I have a table like this: | prodID | date | perm ——————————— |200

  • 0

I have a table like this:

| prodID  | date     |  perm
---------------------------------   
|200      |8/7/2011  | 81.742 
|200      |8/7/2011  | 81.644
|200      |8/7/2011  | 81.302
|200      |8/7/2011  | 81.057
|201      |8/7/2011  | 80.932
|201      |8/7/2011  | 80.839
|201      |8/7/2011  | 80.622
|201      |8/7/2011  | 80.557
|201      |8/7/2011  | 80.541

(except a bit bigger)
Breakdown of what happens: I want to take the average of the top 10 values (and bottom 10 values) where a prodid = somevalue in this case 200.

Code:

declare @myid int
set @myid = 200

;with high as  --top ten average
(
 select prodid, CONVERT(CHAR(10),  DATEADD(DAY, AVG(DATEDIFF(DAY, 0, CONVERT    
(SMALLDATETIME, [date]))), 0),     101) as date, max(perm)as max_perm, avg(perm) 
as   
high_perm from   
( select prodid, date, perm, 
 row_number() over(partition by date order by perm desc) as nt    
 from live_pilot_plant
 where prodid = @myid) as T 
 where nt <= 10
 group by prodid
),
low as   -- bottom ten average
(
select prodid, CONVERT(CHAR(10),  DATEADD(DAY, AVG(DATEDIFF(DAY, 0, CONVERT    
(SMALLDATETIME, [date]))), 0),101) as date, min(perm) as min_perm, avg(perm) 
as low_perm  from   
( select prodid, date, perm,  
  row_number() over(partition by date order by perm asc) as nt    
  from live_pilot_plant
  where prodid = @myid) as T 
  where nt <= 10
  group by prodid
)

select l.prodid, l.date, l.low_perm as low_avg, m.high_perm as high_avg,
(m.high_perm -    l.low_perm) as delta
from low l
left outer join high m
on l.prodid = m.prodid 

Which produces something like this:

|  prodID  |   date     |  low_avg   |  high_avg  |  delta   |
|   200    | 08/07/2011 |   68.752   |  79.1976   |  10.444  |

THESE NUMBERS ARE NOT ACCURATE —

This is all good and dandy – except not very versitle. I mean there are a lot of prodID, and to do one this based on prodID is too slow. How can I get the low_avg and the high_avg based on date (group by prodID)

Something like this:

| date       | prodID  | low_avg  | high_avg  |  delta  |
| 08/07/2011 | 200     |  60      |  80       |  20     |
| 08/07/2011 | 201     |  70      |  100      | 100     |

NOTE: You might have noticed a crazy convert infront of date. The reason is that some prodID overlap dates ie. 200 on 8/7/2011, and 8/8/2011, and I need to average the date (which is a varchar). So something like if there were 100 rows with 8/7/2011, and then 9 rows with 8/8/2011, the final query would produce the date being as /8/7/2011

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:41:20+00:00Added an answer on June 7, 2026 at 9:41 pm

    The following query does it for all products at once:

    select lpp.prod_id, lpp.date,
           AVG(case when seqnum_asc <= 10 then perm end) as avg_bottom10,
           AVG(case when seqnum_desc <= 10 then perm end) as avg_top10,
           (AVG(case when seqnum_desc <= 10 then perm end) - AVG(case when seqnum_asc <= 10 then perm end)) as delta
    from (select lpp.*,
                 ROW_NUMBER() over (partition by prodid, date order by perm) as seqnum_asc,
                 ROW_NUMBER() over (partition by prodid, date order by perm desc) as seqnum_desc
          from live_pilot_plan lpp
         ) lpp
    group by lpp.prod_id, lpp.ate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this.. S.No Name created_date 1 N1 2011-01-08 2 N2
i have a table like this id name date group_id 1 n1 1 1
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like this: id | id_parent | tag_name 0 | |
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have a table like this: <table> <tr> <td> A-1 </td> <td> A-2 </td>
I have UserComments table like this : 1 | Frank | hello world 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.