SQL code to get phone number, email address, from On Account Tracking Report?

Hi! I'm trying to write an On Account Tracking report, but I'm having a serious problem with duplication. I'm getting one row for each unique customer/phone number/email combination. Any suggestions or ideas? Thanks! :)

  • A customer can have multiple phone numbers and email addresses, so you will want to have a strategy for choosing one for each customer.  For email address there should be a "primary" flag that you can leverage (although you may also need assess the email type, depending on how you have that set up).  Phones are unfortunately a lot harder, but you can try [dbo].[FT_GET_PHONES].  For email there is [dbo].[FT_GET_PRIMARY_EADDRESS].  I should warn that there is a definite performance cost for using these functions.

  • My suggestion is to create your own custom view using the logic you want to apply to determine an e-mail to use as FT_GET_Primary_Eaddress can be slow and doesn't return an e-mail if a primary was not assigned.

    Here is what we use and in a comparable run it ran 13 seconds faster than the function against the same data set.

      Create view [dbo].[LV_MBA_PrimaryEmail] as
      Select customer_no , Email , isPrimary,eaddress_no  from
      ( Select  customer_no , address as Email, isnull(primary_ind,'N') as isPrimary , ea.eaddress_no
      ,ROW_NUMBER () over (partition by Customer_No order by isnull(Primary_ind,'N') Desc,ea.eaddress_type, lastuse desc, ea.create_dt desc, ea.eaddress_no desc) as SeqNo
      from T_EADDRESS as EA inner join TR_EADDRESS_TYPE as ET on ea.eaddress_type  = et.id
      left outer join LV_MBA_EmailFreq as EF on ef.eaddress_no =ea.eaddress_no
      where et.email_ind = 'Y' and address !='<temporary>'  ) as E where SeqNo = 1

  • LV_MBA_EmailFreq has me intrigued

  • create view [dbo].[LV_MBA_EmailFreq] as
    select eaddress_no, count(*) as UseCount, min(order_dt) as FirstUse, max(order_dt) as LastUse from t_order
    where eaddress_no > 0
    group by eaddress_no