0

我试图在表中返回一堆值而不会导致“重复”输出。我认为 CASE 语句或派生表可能会有所帮助?任何输入都会很棒。

在 Product_code 列中有以下值

(AFF、E、H、PD、PDM、PDRL、PDRM 等)

这是我的 SQL:

SELECT DISTINCT
[Member Id] = c.master_customer_id,
[Full Name] = c.label_name,
[First Name] = c.first_name,
[Last Name] = c.last_name,
[Email] = ISNULL(c.primary_email_address,''),
[Annual Meeting] = MAX(ca.product_code)
CASE WHEN od.product_code IN  (AFF,E,H,PD,PDM,PDRL,PDRM) then ?? 
--[Membership Type] = od.product_code



 FROM order_detail od

 INNER JOIN customer c
 on c.master_customer_id = od.ship_master_customer_id 
    and c.sub_customer_id = od.ship_sub_customer_id 
    and od.subsystem = 'MBR'

INNER JOIN cus_activity ca

    on ca.master_customer_id = c.master_customer_id
    and ca.sub_customer_id = c.sub_customer_id
    and ca.subsystem = 'MTG'
    and ca.activity_subcode IN ('2012AM', '2011AM')
    and ca.product_code IN ('2012AM','2011AM')
INNER JOIN cus_address caddr
    on caddr.master_customer_id = c.master_customer_id
    and caddr.sub_customer_id = c.sub_customer_id
INNER JOIN cus_address_detail caddrd
    on caddrd.cus_address_id = caddr.cus_address_id    

 where c.customer_class_code NOT IN ('STAFF', 'TEST_MBR')
 and c.customer_status_code = 'ACTIVE'
 and c.primary_email_address IS NOT NULL
 and ca.master_customer_id  IN (select order_detail.ship_master_customer_id 
 from order_detail where order_detail.subsystem = 'MBR')    
 and caddrd.priority_seq = 0
 and caddrd.address_status_code = 'GOOD'
 and od.product_code in  
 ( 'AFF','E','H',    'PD','PDM','PDRL','PDRM','PDRU','R',
 'RM','RRL','RRM','RRU','S','SM','SRL','SRM','SRU','SU','SUM','SURL','SURM','SURU' )
 and od.cycle_end_date >= '01/01/2011' and od.cycle_end_date <= '12/31/2012'

 GROUP BY        c.master_customer_id,c.label_name,
 c.FIRST_NAME,c.LAST_NAME,c.primary_email_address,od.product_code,caddr.country_descr

 order by c.master_customer_id
4

1 回答 1

1

您正在寻找基于给定客户的交叉表结果(也称为数据透视表)。您想要所有可能的会员身份级别,因为一个人可能是多个级别(根据您的示例)。

使用客户 ID 上的 group by,所有内容都将汇总到成员。因此,如果有多个产品代码,我会根据您要考虑的每个单独的“product_code”应用 SUM()。

接下来,为了帮助优化您的查询,我将确保您的 Order_Detail 有一个索引( SubSystem、Product_Code、Cycle_End_Date、ship_master_customer_id )

我稍微改写了一下,以更好地让自己遵循您所获得的内容以及与每个表相关的标准。希望它对您开始的内容有意义。

SELECT 
      c.master_customer_id as [Member Id],
      c.label_name as [Full Name],
      c.first_name as [First Name],
      c.last_name as [Last Name],
      ISNULL(c.primary_email_address,'') as [Email],
      MAX(ca.product_code) as [Annual Meeting],
      SUM( CASE WHEN od.product_code = 'AFF' then 1 else 0 end ) as Membership_AFF,
      SUM( CASE WHEN od.product_code = 'E' then 1 else 0 end ) as Membership_E,
      SUM( CASE WHEN od.product_code = 'H' then 1 else 0 end ) as Membership_H,
      SUM( CASE WHEN od.product_code = 'PD' then 1 else 0 end ) as Membership_PD,
      SUM( CASE WHEN od.product_code = 'PDM' then 1 else 0 end ) as Membership_PDM,
      SUM( CASE WHEN od.product_code = 'PDRL' then 1 else 0 end ) as Membership_PDRL,
      SUM( CASE WHEN od.product_code = 'PDRM' then 1 else 0 end ) as Membership_PDRM
   FROM 
      order_detail od
         INNER JOIN customer c
            on od.ship_master_customer_id = c.master_customer_id
           and od.ship_sub_customer_id = c.sub_customer_id
           and c.customer_class_code NOT IN ('STAFF', 'TEST_MBR')
           and c.customer_status_code = 'ACTIVE'
           and c.primary_email_address IS NOT NULL

        INNER JOIN cus_activity ca
           on od.ship_master_customer_id = ca.master_customer_id
          and od.ship_sub_customer_id = ca.sub_customer_id
          and ca.subsystem = 'MTG'
          and ca.activity_subcode IN ('2012AM', '2011AM')
          and ca.product_code IN ('2012AM','2011AM')

        INNER JOIN cus_address caddr
           on od.ship_master_customer_id = caddr.master_customer_id
          and od.ship_sub_customer_id = caddr.sub_customer_id

           INNER JOIN cus_address_detail caddrd
              on caddr.cus_address_id = caddrd.cus_address_id
             and caddrd.priority_seq = 0
             and caddrd.address_status_code = 'GOOD'

   WHERE
          od.subsystem = 'MBR'
      and od.product_code in ( 'AFF','E','H','PD','PDM','PDRL','PDRM','PDRU',
                               'R','RM','RRL','RRM','RRU','S','SM','SRL',
                               'SRM','SRU','SU','SUM','SURL','SURM','SURU' )
      and od.cycle_end_date >= '01/01/2011' 
      and od.cycle_end_date <= '12/31/2012'

   GROUP BY        
      od.ship_master_customer_id,
      c.label_name,
      c.FIRST_NAME,
      c.LAST_NAME,
      c.primary_email_address,
      caddr.country_descr

   order by
      od.ship_master_customer_id
于 2013-05-23T15:13:10.587 回答