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 でグループ化すると、すべてがメンバーにロールアップされます。したがって、複数の製品コードがある場合は、考慮したい個々の「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 に答える