CASE STATEMENT中的语法错误

时间:2012-10-10 16:39:13

标签: sql sql-server syntax

这是我的select语句。我想要做的是,如果一个帐户有多个ID,我希望电话号码为NULL,我想要电话号码= 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**

1 个答案:

答案 0 :(得分: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