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

  • SEARCH
  • Home
  • 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 8657941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:38:44+00:00 2026-06-12T15:38:44+00:00

Code included is a simplified version of our situation; the production table equivalent to

  • 0

Code included is a simplified version of our situation; the production table equivalent to #MyExample has 20 fields all of which need medians calculating therefore the second part of the script becomes very long – not a huge hard-ship but is there a more compact solution?

I’ve no experience with APPLY or custom FUNCTIONs but is this a situation where we should create a FUNCTION for the median and then use APPLY I’m guessing not as apply is applied to each row?

/*
DROP TABLE #MyExample
DROP TABLE #mediantable
*/

CREATE TABLE #MyExample
        (
        customer char(5),
        amountPeriodA numeric(36,8),
        amountPeriodB numeric(36,8),
        amountPeriodC numeric(36,8)
        )
INSERT INTO #MyExample
        values
        ('a',10,20,30),
        ('b',5,10,15),
        ('c',500,100,150),
        ('d',5,1,1),
        ('e',5,1,15),
        ('f',5,10,150),
        ('g',5,100,1500)




SELECT 
        [Period] = 'amountPeriodA',             
        [Median] = AVG(x.amountPeriodA)         
INTO    #mediantable
FROM (
        SELECT 
                r.customer,
                r.amountPeriodA,
                [RowASC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA ASC, customer ASC),
                [RowDESC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA DESC, customer DESC)
        FROM #MyExample r 
    ) x
WHERE RowASC IN (RowDESC, ROWDESC-1, ROWDESC+1)

union
SELECT 
        [Period] = 'amountPeriodB',             
        [Median] = AVG(x.amountPeriodB)         
FROM (
        SELECT 
                r.customer,
                r.amountPeriodB,
                [RowASC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodB ASC, customer ASC),
                [RowDESC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodB DESC, customer DESC)
        FROM #MyExample r 
    ) x
WHERE RowASC IN (RowDESC, ROWDESC-1, ROWDESC+1)

union
SELECT 
        [Period] = 'amountPeriodC',             
        [Median] = AVG(x.amountPeriodC)         
FROM (
        SELECT 
                r.customer,
                r.amountPeriodC,
                [RowASC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodC ASC, customer ASC),
                [RowDESC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodC DESC, customer DESC)
        FROM #MyExample r 
    ) x
WHERE RowASC IN (RowDESC, ROWDESC-1, ROWDESC+1)


SELECT * 
FROM #mediantable
  • 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-12T15:38:45+00:00Added an answer on June 12, 2026 at 3:38 pm

    Building on my previous reply I arrived on this which is a lot easier (and shorter) to expand for the number of columns and even runs a bit faster (probably a lot faster in case of 20+ columns!). However, it returns the results horizontally instead of vertically. This can be ‘solved’ again using UNPIVOT.
    I’ve done the operation in 2 parts using an intermediate #result table; but you could easily do it in a single statement using a subquery or CTE.

    DECLARE @rowcount int
    DECLARE @first int
    DECLARE @last int
    DECLARE @divider numeric(36,8)
    
    SELECT @rowcount = COUNT(*) FROM #MyExample
    
    SELECT @first = (CASE WHEN @rowcount % 2 = 1 THEN (@rowcount + 1) / 2 ELSE (@rowcount / 2)     END),
           @last  = (CASE WHEN @rowcount % 2 = 1 THEN (@rowcount + 1) / 2 ELSE (@rowcount / 2) + 1 END),
           @divider = (CASE WHEN @rowcount % 2 = 1 THEN 1 ELSE 2 END)
    
     SELECT amountPeriodA = SUM(amountPeriodA) / @divider,
            amountPeriodB = SUM(amountPeriodB) / @divider,
            amountPeriodC = SUM(amountPeriodC) / @divider    
       INTO #result
       FROM
     (
     SELECT amountPeriodA = ((CASE WHEN ROW_NUMBER() OVER(ORDER BY amountPeriodA ASC, customer ASC) IN (@first, @last) THEN amountPeriodA ELSE 0.00 END)),
            amountPeriodB = ((CASE WHEN ROW_NUMBER() OVER(ORDER BY amountPeriodB ASC, customer ASC) IN (@first, @last) THEN amountPeriodB ELSE 0.00 END)),
            amountPeriodC = ((CASE WHEN ROW_NUMBER() OVER(ORDER BY amountPeriodC ASC, customer ASC) IN (@first, @last) THEN amountPeriodC ELSE 0.00 END)) 
       FROM #MyExample
      )t 
    

    and then

      SELECT [Period], [Amount] 
        FROM #result as x
        UNPIVOT ( [Amount] FOR Period IN (amountPeriodA, amountPeriodB, amountPeriodC)) As unpvt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code (this is a simplified version of what I need) does not
right now i am simply ftping everything (all of my source code included) but
I am getting LNK2001 error. The code has been included below. Can someone please
Consider the following simplified version of my code. I have a template class A
Here's a simplified version of my code that compiles: #include <iostream> class pos {
This is a very simplified version of some code I just ran into at
Very simplified version of my code: #include <iostream> #include <string> using namespace std; int
I was trying to make a simplified version of my code for this question
I wrote a simplified version of the code I'm working on to illustrate a
I've got some code in which I need to forward-declare the a template class

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.