Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Stored Procedure to find emails

Stored Procedure to find emails

Scheduled Pinned Locked Moved Database
databasesharepointalgorithmssales
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tiger456
    wrote on last edited by
    #1

    Priority ways email finding I have following tables in project Customer => Customer details => Coloums are Customer Name, Customer Type (Gold, Silver, Platinum) Contact => Can have multiple contacts to a customer => Coloums are Customer Id, Name, email, Designation (D1 to D10). we need to create an SP (MS SQL 2005 ) which returns priority 1 and priority 2 emails of customer (Customer Id will be passed as input) based on following criteria . When Customer Type is "Gold" Customer, choose email based on following Designation 1. D1, D3, 2 D4, D6 3, D5, D7, D8

    Logic :
    a). if D1 exist select D1 and move to set 2,
    D1 NOT exist select D3 and Move to set 2
    D3 NOT exist move to set 2
    b) if D4 exist select D4 and move to set 3,
    D4 NOT exist select D6 and Move to set 3
    D6 NOT exist move to set 3
    Note : if we got an email in first step and this step retun two emails. No need to iterate step 3
    c) reapeat above logic with step 3 designations sequentially

    Continue searching until we get 2 mails.

    For "Silver" Customer choose email based on following designation 1. D1, D2, 2 D6, D7 3, D8, D9, D10 For "Platinum" Customer choose email based on following designation 1. D1, D2, 2 D8, D9 3, D3, D4, D10 I am not sure i explained logic very well. Is there any algorith for this type of logic.

    L 1 Reply Last reply
    0
    • T Tiger456

      Priority ways email finding I have following tables in project Customer => Customer details => Coloums are Customer Name, Customer Type (Gold, Silver, Platinum) Contact => Can have multiple contacts to a customer => Coloums are Customer Id, Name, email, Designation (D1 to D10). we need to create an SP (MS SQL 2005 ) which returns priority 1 and priority 2 emails of customer (Customer Id will be passed as input) based on following criteria . When Customer Type is "Gold" Customer, choose email based on following Designation 1. D1, D3, 2 D4, D6 3, D5, D7, D8

      Logic :
      a). if D1 exist select D1 and move to set 2,
      D1 NOT exist select D3 and Move to set 2
      D3 NOT exist move to set 2
      b) if D4 exist select D4 and move to set 3,
      D4 NOT exist select D6 and Move to set 3
      D6 NOT exist move to set 3
      Note : if we got an email in first step and this step retun two emails. No need to iterate step 3
      c) reapeat above logic with step 3 designations sequentially

      Continue searching until we get 2 mails.

      For "Silver" Customer choose email based on following designation 1. D1, D2, 2 D6, D7 3, D8, D9, D10 For "Platinum" Customer choose email based on following designation 1. D1, D2, 2 D8, D9 3, D3, D4, D10 I am not sure i explained logic very well. Is there any algorith for this type of logic.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Create a bridge table between CustomerType and EmailDesignation and fill it with data that represents the relationship you specified above. You can then create a query that joins these three tables to get the data that you require. You also need to create a new table with email designations and their priority.

      SELECT TOP 2 C.CustomerName, Co.CustomerType, E.Email, E.Designation
      FROM Customer C
      INNER JOIN CustomerEmailBridge B ON C.CustomerType = B.CustomerType
      INNER JOIN Email E ON B.EmailDesignation = E.EmailDesignation
      INNER JOIN EmailPriority P ON E.EmailDesignation = P.EmailDesignation AND B.CustomerType = P.CustomerType
      WHERE (C.CustomerID = @CustomerID)
      ORDER BY P.Priority

      modified on Tuesday, August 2, 2011 2:48 PM

      T 1 Reply Last reply
      0
      • L Lost User

        Create a bridge table between CustomerType and EmailDesignation and fill it with data that represents the relationship you specified above. You can then create a query that joins these three tables to get the data that you require. You also need to create a new table with email designations and their priority.

        SELECT TOP 2 C.CustomerName, Co.CustomerType, E.Email, E.Designation
        FROM Customer C
        INNER JOIN CustomerEmailBridge B ON C.CustomerType = B.CustomerType
        INNER JOIN Email E ON B.EmailDesignation = E.EmailDesignation
        INNER JOIN EmailPriority P ON E.EmailDesignation = P.EmailDesignation AND B.CustomerType = P.CustomerType
        WHERE (C.CustomerID = @CustomerID)
        ORDER BY P.Priority

        modified on Tuesday, August 2, 2011 2:48 PM

        T Offline
        T Offline
        Tiger456
        wrote on last edited by
        #3

        Shameel, Simple join will not work here. I need the output based on logic mentioned. It a priority based search. I am looking for best and optimized way to do it.

        L 1 Reply Last reply
        0
        • T Tiger456

          Shameel, Simple join will not work here. I need the output based on logic mentioned. It a priority based search. I am looking for best and optimized way to do it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Have a look at my updated answer. Sorry, I missed the priority thing.

          A 1 Reply Last reply
          0
          • L Lost User

            Have a look at my updated answer. Sorry, I missed the priority thing.

            A Offline
            A Offline
            Anish Gopi
            wrote on last edited by
            #5

            Hi shameel, this will not work . priority for same designation is different for different customer type

            L 1 Reply Last reply
            0
            • A Anish Gopi

              Hi shameel, this will not work . priority for same designation is different for different customer type

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              A little change makes it work. Just add a CustomerType column to the Priority table and include the CustomerType in the WHERE clause.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups