当前位置: 代码迷 >> Oracle管理 >> SQL查询中怎么对一个字段判断后再查询其他表
  详细解决方案

SQL查询中怎么对一个字段判断后再查询其他表

热度:154   发布时间:2016-04-24 04:06:56.0
SQL查询中如何对一个字段判断后再查询其他表?
因为要导出一些数据,现在出现这种情况:ffvv2.DESCRIPTION     线路, hv.lineway_name 需求申请线路,这两个字段中抓取的是不同的表,如何设计SQL代码实现比如在ffvv2.DESCRIPTION为空的时候,去查找 hv.lineway_name的信息,并且在导出的数据中是放在同一个字段中;不需要分开两个字段。



select distinct msib.segment1    编码,
                msib.description 描述,
                flv.MEANING      物资类型,
                mtt.transaction_type_name 事务处理类型,
                mmt.attribute2      as   领料单号 ,
                cm.workorder_type         工单类型,        
                cm.comments           描述,
                va.DESCRIPTION       部门, 
                ffvv2.DESCRIPTION     线路,
                hv.lineway_name 需求申请线路,
                mmt.transaction_date  事务处理时间,
                fu.user_name          操作人,
                mmt.subinventory_code 仓库,
  from apps.mtl_material_transactions    mmt,
       apps.mtl_system_items_b           msib,
       apps.FND_FLEX_VALUES_VL va ,
       apps.mtl_transaction_lot_numbers  mtln,
       apps.fnd_lookup_values_vl         flv,
       apps.fnd_user                     fu,
       inv.mtl_transaction_types         mtt,
       apps.mtl_secondary_inventories    msi,
       apps.ORG_ORGANIZATION_DEFINITIONS ou,
       apps.cux_inv_mo_headers           cm,-----工单
       apps.fnd_flex_values_vl           ffvv2,----线路
       apps.pa_tasks                     pt,----项目号
       apps.cux_inv_demand_headers_v     hv-----需求申请视图

 where mmt.inventory_item_id = msib.inventory_item_id
   and mmt.attribute2 = cm.mo_code(+)
   AND ffvv2.flex_value_id(+) = cm.lineway_id
   AND cm.task_id = pt.task_id(+)
   and mmt.organization_id = ou.organization_id
   and mtln.transaction_id(+) = mmt.transaction_id
   and msib.organization_id = mmt.organization_id
   and mmt.organization_id = msi.organization_id
   and mmt.transaction_type_id = mtt.transaction_type_id
   and mmt.subinventory_code = msi.secondary_inventory_name
   and fu.user_id = mmt.created_by
   and va.FLEX_VALUE_ID(+) = mmt.attribute6
   and msib.item_type = flv.LOOKUP_CODE
   and flv.LOOKUP_TYPE = 'ITEM_TYPE'
   and mmt.attribute2=hv.demand_code(+)
   and mmt.creation_date >=
       to_date('2015-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
   and mmt.creation_date <
       to_date('2015-03-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
   and msib.organization_id in
       (124, 140, 1923, 841, 861, 842, 1144, 139)

------解决思路----------------------
select distinct msib.segment1    编码,
                msib.description 描述,
                flv.MEANING      物资类型,
                mtt.transaction_type_name 事务处理类型,
                mmt.attribute2      as   领料单号 ,
                cm.workorder_type         工单类型,        
                cm.comments           描述,
                va.DESCRIPTION       部门, 
                nvl(ffvv2.DESCRIPTION,hv.lineway_name)  线路,
                hv.lineway_name 需求申请线路,
                mmt.transaction_date  事务处理时间,
                fu.user_name          操作人,
                mmt.subinventory_code 仓库,
  from apps.mtl_material_transactions    mmt,
       apps.mtl_system_items_b           msib,
       apps.FND_FLEX_VALUES_VL va ,
       apps.mtl_transaction_lot_numbers  mtln,
       apps.fnd_lookup_values_vl         flv,
       apps.fnd_user                     fu,
       inv.mtl_transaction_types         mtt,
       apps.mtl_secondary_inventories    msi,
       apps.ORG_ORGANIZATION_DEFINITIONS ou,
       apps.cux_inv_mo_headers           cm,-----工单
       apps.fnd_flex_values_vl           ffvv2,----线路
       apps.pa_tasks                     pt,----项目号
       apps.cux_inv_demand_headers_v     hv-----需求申请视图
 
 where mmt.inventory_item_id = msib.inventory_item_id
   and mmt.attribute2 = cm.mo_code(+)
   AND ffvv2.flex_value_id(+) = cm.lineway_id
   AND cm.task_id = pt.task_id(+)
   and mmt.organization_id = ou.organization_id
   and mtln.transaction_id(+) = mmt.transaction_id
   and msib.organization_id = mmt.organization_id
   and mmt.organization_id = msi.organization_id
   and mmt.transaction_type_id = mtt.transaction_type_id
   and mmt.subinventory_code = msi.secondary_inventory_name
   and fu.user_id = mmt.created_by
   and va.FLEX_VALUE_ID(+) = mmt.attribute6
   and msib.item_type = flv.LOOKUP_CODE
   and flv.LOOKUP_TYPE = 'ITEM_TYPE'
   and mmt.attribute2=hv.demand_code(+)
   and mmt.creation_date >=
       to_date('2015-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
   and mmt.creation_date <
       to_date('2015-03-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
   and msib.organization_id in
       (124, 140, 1923, 841, 861, 842, 1144, 139)
------解决思路----------------------
同意一楼,除了nvl还可以用coalesce
------解决思路----------------------
NVL(expr1,expr2)楼主你值得拥有!
  相关解决方案