如何组合两个具有相同列但不同case语句和where子句的结果集

时间:2017-09-06 18:59:14

标签: sql sql-server-2012 union

我如何使用' union'对于以下两个查询?或者还有其他解决方案吗?它们都成功运行并返回我期望的结果集。它们具有相同数量的列(使用相同的表和相同的数据类型),但在case语句和' where'条款。

  SELECT TOP 720
            ind_first_name AS [First Name],
            ind_last_name AS [Last Name],
            cst_recno AS [Member ID],
            cst_eml_address_dn AS [Email Address],
            adr_city AS City,
            adr_state AS [State],
            adr_country AS Country,
            cst_org_name_dn AS Company,
            cst_ixo_title_dn AS [Job Title],
CASE
WHEN mem_member_product LIKE '%Stud%' 
      THEN 'Associate Student'
ELSE 'Exclude'
END AS [Member Type]
    FROM co_individual 
    INNER JOIN co_individual_ext  ON ind_cst_key_ext = ind_cst_key 
    INNER JOIN co_customer ON cst_key = ind_cst_key 
        AND ind_delete_flag=0
        AND ind_deceased_flag=0 
    LEFT JOIN co_customer_x_address ON cst_cxa_key = cxa_key 
    LEFT JOIN co_address ON adr_key = cxa_adr_key 
    LEFT JOIN vw_client_uli_member_type ON cst_key = mem_cst_key 
    WHERE mem_member_type IS NOT NULL and mem_expire_date >= 9/7/2017 and mem_member_product LIKE '%Stud%'
    AND adr_country = N'UNITED STATES' 
    AND ind_deceased_flag != 1 
    AND ind_key_leader_flag_ext != 1 
    AND ind_int_code != N'Staff' 
    AND ind_int_code != N'Spouse' 
    AND ind_int_code != N'Press'
    order by newid()


SELECT TOP 80
            ind_first_name AS [First Name],
            ind_last_name AS [Last Name],
            cst_recno AS [Member ID],
            cst_eml_address_dn AS [Email Address],
            adr_city AS City,
            adr_state AS [State],
            adr_country AS Country,
            cst_org_name_dn AS Company,
            cst_ixo_title_dn AS [Job Title],
CASE
WHEN Mem_member_type in ('Full Under 35 Sustaining','Full Member') and ind_age_cp <= '35'
      THEN 'Full Member Under 35'
ELSE 'Exclude'
END AS [Member Type]
    FROM co_individual 
    INNER JOIN co_individual_ext  ON ind_cst_key_ext = ind_cst_key 
    INNER JOIN co_customer ON cst_key = ind_cst_key 
        AND ind_delete_flag=0
        AND ind_deceased_flag=0 
    LEFT JOIN co_customer_x_address ON cst_cxa_key = cxa_key 
    LEFT JOIN co_address ON adr_key = cxa_adr_key 
    LEFT JOIN vw_client_uli_member_type ON cst_key = mem_cst_key 
    WHERE mem_member_type IS NOT NULL and mem_expire_date >= 9/7/2017 and mem_member_type in ('Full Under 35 Sustaining','Full Member') and ind_age_cp <= '35'
    AND adr_country = N'UNITED STATES' 
    AND ind_deceased_flag != 1 
    AND ind_key_leader_flag_ext != 1 
    AND ind_int_code != N'Staff' 
    AND ind_int_code != N'Spouse' 
    AND ind_int_code != N'Press'
        order by newid ()

谢谢。

1 个答案:

答案 0 :(得分:0)

你应该能够在两个查询之间放置UNION,将整个事物包装在括号中,给它一个别名,然后从中选择:

if ( in_array( 'post', $show ) ){   
    if ( in_array( 'page', $show ) ){
        //in both post and page
        $post_types = is_singular( array ( 'post', 'page' ) );
    }else{
        //in only post
        $post_types = is_singular( array ( 'post' ) );
    }
}else if ( in_array( 'page', $show ) ){
    //in only page
    $post_types = is_singular( array ( 'page' ) );
}else{
    //undefined.
}