SQL conditional Statements

Viewed 783

I am attempting to get all the records from a table where trans_type='RM' but if there is no trans_type='RM' i want to return all the records where trans_type = 'AD'

Technically im using xtupls MetaSQL on a PostgreSQL server so a solution using either is great I can upload my metaSQL statement need be but I really just need a way to do

-- Group: lotserial 
-- Name: detail
-- Notes: 
-- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
-- See www.xtuple.com/EULA for the full text of the software license.

SELECT ls_number,
   ls_notes, 
   formatlotserialnumberbarcode(ls_number) AS lotserial_barcode,
   item_number, 
   item_descrip1, 
   item_descrip2,
   charass_char_id,
   charass_value,
   poitem_id,
   poitem_vend_item_descrip,
   char_name, 
   formatqty(itemloc_qty) as lotqty,
   lshist.*
FROM   
   itemloc, 
   ls
   JOIN item ON (item_id=ls_item_id)
   LEFT JOIN charass ON (charass_target_id=ls_id)
   LEFT JOIN "char" ON (char_id=charass_char_id),
   lshist (<? value("itemid") ?>,<? value("warehouseid") ?>,ls_number,
           <? value("pattern") ?>,<? value("transType") ?>,<? value("startDate") ?>,
           <? value("endDate") ?>,<? value("trace") ?>,1)


     LEFT JOIN pohead ON(pohead_number=(TRIM(SUBSTRING(lshist_ordernumber FROM '-.*-'),'-')))

     LEFT JOIN poitem ON(poitem_pohead_id=pohead_id)

   <? if exists('ls_id') ?>
   WHERE ls_id=<? value("ls_id") ?>
   <? endif ?>
   <? if exists('ls_number') ?>
   WHERE ls_number=<? value("ls_number") ?>
   <? endif ?>


   AND lshist_warehous_code='PS'

   <? if exists(TRIM(SUBSTRING(lshist_ordernumber FROM '.*-'),'-')='PO')?>
   AND poitem_linenumber = CAST(TRIM(SUBSTRING(lshist_ordernumber FROM '[^-]*$'),'-') AS INTEGER)
   <? endif ?>

   AND ls_id = itemloc_ls_id
   AND charass_target_type = 'LS'


 /*
   <? if exists(lshist_transtype='RM')?>
   AND lshist_transtype='RM'
   <? elseif exists(lshist_transtype='AD')?>
   AND lshist_transtype='AD'
   <? elseif exists(lshist_transtype='RL')?>
   AND lshist_transtype='RL' 
   <? elseif exists(lshist_transtype='SH')?>
   AND lshist_transtype='SH'  
   <? elseif exists(lshist_transtype='IM')?>
   AND lshist_transtype='IM'
    <? elseif exists(lshist_transtype='TR')?>
   AND lshist_transtype='TR'
   <? elseif exists(lshist_transtype='RP')?>
   AND lshist_transtype='RP' 
   <? endif ?>
2 Answers
Related