0

是否可以使用案例来选择黑白两种类型的查询?我想选择其中任何一个。而且我还希望两个查询的列都应该是可见的。

(CASE WHEN mti.category_code = 'SUBS'
  THEN (SELECT u.circle_code, u.city,
      u.msisdn AS subs_msisdn,
      u.user_name || '' || u.last_name AS subs_name
      FROM mtx_party u
      WHERE msisdn LIKE :msisdn
      AND u.user_id = mti.party_id)
  ELSE (SELECT u1.circle_code, u1.city,
          u1.msisdn AS ret_msisdn,
          u1.user_name || '' || u1.last_name AS ret_name,
                             u2.user_name || '' || u2.last_name AS dist_name,
                             u2.msisdn AS dist_msisdn, ' ' AS dist_acc_no,
                             u1.agent_code
                        FROM users u1, users u2
                       WHERE msisdn LIKE :msisdn
                         AND u1.user_id = mti.party_id
                         AND u2.user_id = u1.parent_id)
             END
4

1 回答 1

0

您可以改用联合

SELECT u.circle_code, u.city,
      u.msisdn AS subs_msisdn,
      u.user_name || '' || u.last_name AS subs_name
      FROM mtx_party u
      WHERE msisdn LIKE :msisdn
      AND u.user_id = mti.party_id
      /*STATEMENT THAT EXCLUDES THIS RESULTS FROM THIS QUERY*/
      AND mti.category_code = 'SUBS'
union
SELECT u1.circle_code, u1.city,
          u1.msisdn AS ret_msisdn,
          u1.user_name || '' || u1.last_name AS ret_name,
          u2.user_name || '' || u2.last_name AS dist_name,
          u2.msisdn AS dist_msisdn, ' ' AS dist_acc_no,
          u1.agent_code
FROM users u1, users u2
WHERE msisdn LIKE :msisdn
AND u1.user_id = mti.party_id
AND u2.user_id = u1.parent_id
/*STATEMENT THAT EXLUDES RESULTS FROM THIS QUERY*/
AND mti.category_code <> 'SUBS'

或者您可以执行以下操作

select mti.*, 
  CASE WHEN mti.category_code = 'SUBS'
  THEN sub1.circle_code
  ELSE sub2.circle_code
  END as circle_code,
  CASE WHEN mti.category_code = 'SUBS'
  THEN sub1.city
  ELSE sub2.city
  END as city,
  CASE WHEN mti.category_code = 'SUBS'
  THEN sub1.subs_msisdn
  ELSE sub2.ret_msisdn
  END as msisdn,  
  etc......
from table mti
left outer join (
SELECT u.user_id, u.circle_code, u.city,
      u.msisdn AS subs_msisdn,
      u.user_name || '' || u.last_name AS subs_name
      FROM mtx_party u
      WHERE msisdn LIKE :msisdn
      /*STATEMENT THAT EXCLUDES THIS RESULTS FROM THIS QUERY*/
      AND mti.category_code = 'SUBS'
) sub1 on  sub1.user_id = mti.party_id
left outer join (
SELECT u1.user_id, u1.circle_code, u1.city,
          u1.msisdn AS ret_msisdn,
          u1.user_name || '' || u1.last_name AS ret_name,
          u2.user_name || '' || u2.last_name AS dist_name,
          u2.msisdn AS dist_msisdn, ' ' AS dist_acc_no,
          u1.agent_code
FROM users u1, users u2
WHERE msisdn LIKE :msisdn
AND u2.user_id = u1.parent_id
/*STATEMENT THAT EXLUDES RESULTS FROM THIS QUERY*/
AND mti.category_code <> 'SUBS'
) sub2 on sub2.user_id = mti.party_id
于 2013-03-08T09:44:36.710 回答