
/************************************************************************
   These CREATE statements are used only for compiling the procedure, that
   is why they are contained Before the create procedure statement.  The
   temporary tables at execution time are created in p280_p006.
 ************************************************************************/

create table #t280_fypd	
( f_invtyp_c   char(14) null
, f_invtypnm_x char(30) null
, f_inv1_q     money null
, f_inv1_a     money null
, f_inv2_q     money null 
, f_inv2_a     money null
)      
go

create table #t280_sum	
( f_invtyp_c   char(14) null
, f_invtypnm_x char(30) null
, f_inv1_q     money null
, f_inv1_a     money null
, f_inv2_q     money null 
, f_inv2_a     money null
, f_inv3_q     money null 
, f_inv3_a     money null
, f_inv4_q     money null 
, f_inv4_a     money null
)
go

drop proc p280_p006d
go

create procedure p280_p006d
/*
  -----------------------------------------------------------------------
      P280_P006D - Inventory Analysis Report - PRINT REPORT from TEMPDB.
  -----------------------------------------------------------------------
*/

/*
    ------  INPUT VARIABLES   ------
*/
   @exp char(20)  = "01. None'"
 , @ord char(25)  = "01. Category"
 , @amt decimal(15,2) = 0
as

/***********************************************************************
   Summarize the period totals by the Report Group and insert them into
   the temporary talbe #t280_sum with default values for the Impact %'s.
 ***********************************************************************/

begin
insert #t280_sum
select f_invtyp_c  
     , f_invtypnm_x
     , sum(f_inv1_q) 
     , sum(f_inv1_a)
     , sum(f_inv2_q)
     , sum(f_inv2_a)
     ,(sum(f_inv2_q) - sum(f_inv1_q)) 
     ,(sum(f_inv2_a) - sum(f_inv1_a))
     , 100
     , 100
  from #t280_fypd
 group by f_invtyp_c
        , f_invtypnm_x
end


/***********************************************************************
   Update the temporary table to calculate the Qty Percentage for the  
   IMPACT values.
 ***********************************************************************/

begin
update #t280_sum
   set f_inv4_q = (((f_inv2_q - f_inv1_q) / f_inv1_q) * 100)
 where f_inv1_q != 0
end


/***********************************************************************
   Update the temporary table to calculate the Amt Percentage for the  
   IMPACT values.
 ***********************************************************************/

begin
update #t280_sum
   set f_inv4_a = (((f_inv2_a - f_inv1_a) / f_inv1_a) * 100)
 where f_inv1_a != 0
end


/***********************************************************************
   Control Section for Printing and Ordering the Reports
 ***********************************************************************/

if @ord = '01. Category'
   goto OrderByCategory

if @ord = '02. IMPACT QTY ASC'
   goto OrderByQtyA

if @ord = '03. IMPACT QTY DESC'
   goto OrderByQtyD

if @ord = '04. IMPACT AMT ASC'
   goto OrderByAmtA

if @ord = '05. IMPACT AMT DESC'
   goto OrderByAmtD

if @ord = '06. IMPACT QTY % ASC'
   goto OrderByQtyPctA

if @ord = '07. IMPACT QTY % DESC'
   goto OrderByQtyPctD

if @ord = '08. IMPACT AMT % ASC'
   goto OrderByAmtPctA

if @ord = '09. IMPACT AMT % DESC'
   goto OrderByAmtPctD

if @ord = '10. LEFT QTY ASC'
   goto OrderByLeftQtyA

if @ord = '11. LEFT QTY DESC'
   goto OrderByLeftQtyD

if @ord = '12. LEFT AMT ASC'
   goto OrderByLeftAmtA

if @ord = '13. LEFT AMT DESC'
   goto OrderByLeftAmtD

if @ord = '14. RIGHT QTY ASC'
   goto OrderByRightQtyA

if @ord = '15. RIGHT QTY DESC'
   goto OrderByRightQtyD

if @ord = '16. RIGHT AMT ASC'
   goto OrderByRightAmtA

if @ord = '17. RIGHT AMT DESC'
   goto OrderByRightAmtD


OrderByCategory:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by f_invtyp_c
        , f_invtypnm_x
end
   goto ENDIT    


OrderByAmtA:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 8 asc     
end
   goto ENDIT    


OrderByAmtD:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 8 desc    
end
   goto ENDIT    


OrderByQtyA:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 7 asc
end
   goto ENDIT    

OrderByQtyD:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 7 desc
end
   goto ENDIT    


OrderByAmtPctA:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 10 asc
end
   goto ENDIT    


OrderByAmtPctD:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 10 desc
end
   goto ENDIT    


OrderByQtyPctA:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 9 asc
end
   goto ENDIT    


OrderByQtyPctD:

begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 9 desc
end
   goto ENDIT    


OrderByLeftQtyA:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 3
end
   goto ENDIT    


OrderByLeftQtyD:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 3 desc
end
   goto ENDIT    


OrderByLeftAmtA:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 4
end
   goto ENDIT    


OrderByLeftAmtD:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 4 desc
end
   goto ENDIT    


OrderByRightQtyA:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 5
end
   goto ENDIT    


OrderByRightQtyD:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 5 desc
end
   goto ENDIT    


OrderByRightAmtA:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 6
end
   goto ENDIT    


OrderByRightAmtD:
begin
select f_invtyp_c  
     , f_invtypnm_x
     , f_inv1_q 
     , f_inv1_a
     , f_inv2_q
     , f_inv2_a
     , f_inv3_q
     , f_inv3_a
     , f_inv4_q
     , f_inv4_a
  from #t280_sum
 where (@exp = '01. None')	
    or (@exp = '02. IMPACT QTY > ?'   and f_inv3_q > @amt)
    or (@exp = '03. IMPACT AMT > ?'   and f_inv3_a > @amt)
    or (@exp = '04. IMPACT QTY % > ?' and f_inv4_q > @amt)
    or (@exp = '05. IMPACT AMT % > ?' and f_inv4_a > @amt)
    or (@exp = '06. LEFT QTY > ?'     and f_inv1_q > @amt)
    or (@exp = '07. LEFT AMT > ?'     and f_inv1_a > @amt)
    or (@exp = '08. RIGHT QTY > ?'    and f_inv2_q > @amt)
    or (@exp = '09. RIGHT AMT > ?'    and f_inv2_a > @amt)
    or (@exp = '10. IMPACT QTY < ?'   and f_inv3_q < @amt)
    or (@exp = '11. IMPACT AMT < ?'   and f_inv3_a < @amt)
    or (@exp = '12. IMPACT QTY % < ?' and f_inv4_q < @amt)
    or (@exp = '13. IMPACT AMT % < ?' and f_inv4_a < @amt)
    or (@exp = '14. LEFT QTY < ?'     and f_inv1_q < @amt)
    or (@exp = '15. LEFT AMT < ?'     and f_inv1_a < @amt)
    or (@exp = '16. RIGHT QTY < ?'    and f_inv2_q < @amt)
    or (@exp = '17. RIGHT AMT < ?'    and f_inv2_a < @amt)
            
 order by 6 desc
end
   goto ENDIT    


ENDIT: 

/************************************************************************
   Return to the calling environment
 ************************************************************************/

return ( 0 )
go
