title: Greatest/least or max/min calculation in SQL
body: <p>I want to calculate <code>max(min(150,value returned by below query),50)</code>. How do I implement this in netezza? I need to pass the value returned by the below query into the above statement and compute the value of the expression.Appreciate your time and help.</p>
  
  <p>Note : By Min/Max I mean greatest/least SQL equivalent. Although in Neteeza there is no greatest/least functions.</p>
  
  <pre><code>  SELECT ( x / cast(y  as float)* 100 AS sales_ratio
  FROM   (SELECT a.sum(sales_amt) AS x
          FROM   table2 b, 
                 table1 a,
                 table 3 c
          WHERE  b.VEND_CD IN ($vendorCD1) AND
                 b.ITM_CD_1 = ($ITMCD) AND
                 b.area_num = ($area) AND
                 b.area_num = a.area_num AND
                 b.itm_cd_2 = a.itm_cd_2 AND
                 a.week_end = c.week_end AND
                 c.week_end BETWEEN ($startdate) AND ($enddate)) t1
  CROSS JOIN (SELECT a.sum(sales_amt) AS y
              FROM   table2 b, 
                     table1 a,
                     table3 c
              WHERE  b.VEND_CD IN ($vendorCD1) AND
                     b.ITM_CD_1 = ($ITMCD) AND
                     b.area_num = ($area) AND
                     b.area_num = a.area_num AND
                     b.itm_cd_2 = a.itm_cd_2 AND
                     a.week_end = c.week_end AND
                     c.week_end BETWEEN ($startdate2) AND ($enddate2)) t2
  </code></pre>