Home Page for the TeradataForum
 

Archives of the TeradataForum

Message Posted: Wed, 27 Nov 2002 @ 12:03:38 GMT


     
  <Prev Next>   <<First <Prev Next> Last>>  


Subj:   Re: MEDIAN - using SQL
 
From:   Claybourne L. Barrineau

Try this for a median function:

Select      Average(averageresult.ColumnA)
From  (
      Select      Distinct csumlist.ColumnA
      From  (
            Select      Case  When  TotalCount mod 2  =     0     Then TotalCount/2
                        Else  TotalCount/2 + 1
                  End
            ,     TotalCount/2 + 1
            From  (Select Count(ColumnA) From TableA) TblCount (TotalCount)
            ) csumrange (LowMiddle, HighMiddle)
            ,
            (
            Select      csum(1,ColumnA asc)
            ,     ColumnA
            From  TableA
            ) csumlist (CSUMvalue, ColumnA)
      Where csumlist.CsumValue      >=    csumrange.LowMiddle
      and   csumlist.CsumValue      <=    csumrange.HighMiddle
      ) AverageResult (ColumnA)

TableA = Your Table
ColumnA= Your Column

Hope this helps,

Claybourne Barrineau



     
  <Prev Next>   <<First <Prev Next> Last>>  
 
 
 
 
 
 
 
 
  
  Top Home Privacy Feedback  
 
 
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 15 Jun 2023