0

Here is my select statement. What I'm trying to do is if an account has more than one ID, I want the phone number to be NULL, ELSE I want the phone number to = phone_number_formatted:

SELECT
    v_returned_inventory.order_id,
    v_live_inventory.inet_event_description,
    v_live_inventory.event_time,
    v_cust_phone.phone_number_formatted,
    v_live_inventory.event_date,
    v_returned_inventory.section_name,
    v_returned_inventory.add_usr,
    v_live_inventory.num_seats,
    v_returned_inventory.acct_id,
    v_live_inventory.class_name,
    AT_trans_for_emailTrigger.email_addr,
    AT_trans_for_emailTrigger.cust_name_id,
    premclub.name_first + ' ' + premclub.name_last AS name      
FROM
    v_returned_inventory 
INNER JOIN
    v_live_inventory 
        ON 
LEFT(v_returned_inventory.event_name, 6) = LEFT(v_live_inventory.event_name, 6) 
AND                             v_returned_inventory.orderNumber = v_live_inventory.other_info_1 INNER JOIN
    AT_trans_for_emailTrigger 
        ON v_returned_inventory.order_id = AT_trans_for_emailTrigger.order_id 
LEFT OUTER JOIN
    v_cust_phone 
        on v_cust_phone.acct_id = v_returned_inventory.acct_id 
LEFT OUTER JOIN
    OPENQUERY(premclub, 'select name_first, name_last, cust_name_id from dba.v_cust_name') AS premclub 
        ON                             AT_trans_for_emailTrigger.cust_name_id = premclub.cust_name_id,
    **CASE 
        WHEN (select
            count(cust_name_id) 
        from
            v_cust_phone) > 1 then null 
        else v_cust_phone.phone_number_formatted 
    END**
4

1 回答 1

2

你有CASE错误的地方声明,它需要在SELECT. 根据您目前所拥有的,您似乎可以执行以下操作:

SELECT v_returned_inventory.order_id,
        v_live_inventory.inet_event_description,
        v_live_inventory.event_time,
        case when phone.cnt > 1 then null else v_cust_phone.phone_number_formatted end phone_number_formatted,
        v_live_inventory.event_date, 
        v_returned_inventory.section_name, 
        v_returned_inventory.add_usr, 
        v_live_inventory.num_seats, 
        v_returned_inventory.acct_id, 
        v_live_inventory.class_name, 
        AT_trans_for_emailTrigger.email_addr, 
        AT_trans_for_emailTrigger.cust_name_id, 
        premclub.name_first + ' ' + premclub.name_last AS name
FROM  v_returned_inventory 
INNER JOIN  v_live_inventory 
    ON LEFT(v_returned_inventory.event_name, 6) = LEFT(v_live_inventory.event_name, 6) 
    AND v_returned_inventory.orderNumber = v_live_inventory.other_info_1 
INNER JOIN AT_trans_for_emailTrigger 
    ON v_returned_inventory.order_id = AT_trans_for_emailTrigger.order_id 
LEFT OUTER JOIN v_cust_phone
    on v_cust_phone.acct_id = v_returned_inventory.acct_id 
LEFT OUTER JOIN
(
    select count(cust_name_id) cnt, cust_name_id
    from v_cust_phone 
    group by cust_name_id
) phone
    on v_cust_phone.cust_name_id = phone.cust_name_id 
LEFT OUTER JOIN  OPENQUERY(premclub, 'select name_first, name_last, cust_name_id from dba.v_cust_name') AS premclub 
    ON AT_trans_for_emailTrigger.cust_name_id = premclub.cust_name_id
于 2012-10-10T16:50:38.260 回答