Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.25 KB | None | 0 0
  1. SELECT DISTINCT TOP 100
  2. ContactPerson.lukLoyaltyCardBusinessUnit,
  3. DirPartyTableLoyalty.OMOperatingUnitNumber,
  4. ContactPerson.ContactPersonId                                                   [ContactPersonId],
  5. -- WHERE-TO-SEND --
  6. /*(SELECT TOP 1 LogisticsElectronicAddress.Locator
  7.     FROM [AX12].[AX2012R3_PROD].[DBO].LogisticsElectronicAddress
  8.     JOIN [AX12].[AX2012R3_PROD].[DBO].DirPartyLocation
  9.     ON LogisticsElectronicAddress.Location                                      = DirPartyLocation.Location
  10.     AND DirPartyLocation.Party                                                  = ContactPerson.Party
  11.     WHERE LogisticsElectronicAddress.Type                                       = 2 --Email
  12.     ORDER BY LogisticsElectronicAddress.IsPrimary                               DESC
  13. ) as                                                                            [EmailAddress], -- NOT MANDATORY, CAN BE NULL
  14. ISNULL(
  15.         (SELECT TOP 1 LogisticsElectronicAddress.Locator
  16.          FROM [AX12].[AX2012R3_PROD].[DBO].[LogisticsElectronicAddress]        
  17.          JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyLocation]
  18.          ON [LogisticsElectronicAddress].Location                               = [DirPartyLocation].Location
  19.          AND [DirPartyLocation].Party                                           = ContactPerson.Party
  20.          WHERE [LogisticsElectronicAddress].IsMobilePhone                       = 1
  21.          AND [LogisticsElectronicAddress].Type                                  = 100 --lukSMS
  22.         ),
  23.         (SELECT TOP 1 LogisticsElectronicAddress.Locator
  24.          FROM [AX12].[AX2012R3_PROD].[DBO].[LogisticsElectronicAddress]
  25.          JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyLocation]
  26.          ON [LogisticsElectronicAddress].Location                               = [DirPartyLocation].Location
  27.          AND [DirPartyLocation].Party                                           = ContactPerson.Party
  28.          WHERE [LogisticsElectronicAddress].IsMobilePhone                       = 1
  29.          AND [LogisticsElectronicAddress].Type                                  = 1 --Phone
  30.         )
  31.       ) as                                                                      [MobilePhone],*/
  32. ContactPerson.lukRegistrationDateML                                             [RegistrationDate],
  33. ContactPerson.lukLoyaltyMemberSource                                            [LoyaltyMemberSource],
  34.  
  35. DirPartyTable.NameAlias                                                         [NameAlias],
  36. CASE ContactPerson.lukWebCity
  37.     WHEN 0 THEN NULL
  38.     ELSE CASE DirPartyTableLoyalty.OMOperatingUnitNumber
  39.          WHEN '' THEN NULL
  40.          ELSE CONCAT(DirPartyTableLoyalty.OMOperatingUnitNumber collate Cyrillic_General_CI_AS ,N's')
  41.     END
  42. END AS                                                                          [LoyaltyShopNum],
  43. ISNULL(lukTreatmentTable.CustAccount, 0) as                                     [TreatmentAccount],
  44. ContactPerson.CustAccount                                                       [CustAccount],
  45. ISNULL(DirPartyTable.lukBirthDate,
  46.        DATEFROMPARTS(   DirPartyTable.BirthYear,
  47.                         DirPartyTable.BirthMonth,
  48.                         DirPartyTable.BirthDay)
  49.        ) as                                                                     [BirthDate],
  50. DirPartyTable.Gender                                                            [GenderCode],
  51.  
  52.  
  53. -- SUBSCRIPTIONS --
  54. /*(SELECT TOP 1 Value
  55. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  56. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  57. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  58. AND lukDirPartyTableSubscribe.SubscribeType                                     = 0 --lukSubscribeType::AgreementOnSMS
  59. ) as                                                                            [SendSMS],
  60. (SELECT TOP 1 SubscribeDate
  61. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  62. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  63. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  64. AND lukDirPartyTableSubscribe.SubscribeType                                     = 0 --lukSubscribeType::AgreementOnSMS
  65. ) as                                                                            [SendSMSDate],
  66. CASE (SELECT TOP 1 Value
  67.       FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  68.       WHERE lukDirPartyTableSubscribe.RefTableId                                = 2303 --DirPartyTable.TableId
  69.       AND lukDirPartyTableSubscribe.RefRecId                                    = DirPartyTable.RecId
  70.       AND lukDirPartyTableSubscribe.SubscribeType                               = 1 --lukSubscribeType::AgreementOnEmail
  71.      )
  72.     WHEN NULL THEN 1
  73.     ELSE 0
  74. END AS                                                                          [DoNotSendEmail],
  75. (SELECT TOP 1 SubscribeDate
  76. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  77. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  78. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  79. AND lukDirPartyTableSubscribe.SubscribeType                                     = 1 --lukSubscribeType::AgreementOnEmail
  80. ) as                                                                            [SendEmailDate],
  81. (SELECT TOP 1 Value
  82. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  83. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  84. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  85. AND lukDirPartyTableSubscribe.SubscribeType                                     = 2 --lukSubscribeType::BlockSubscribe
  86. ) as                                                                            [DoNotSend],
  87. (SELECT TOP 1 SubscribeDate
  88. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  89. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  90. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  91. AND lukDirPartyTableSubscribe.SubscribeType                                     = 2 --lukSubscribeType::BlockSubscribe
  92. ) as                                                                            [DoNotSendDate],
  93. (SELECT TOP 1 PersonnelNumber
  94. FROM [AX12].[AX2012R3_PROD].[DBO].lukDirPartyTableSubscribe
  95. WHERE lukDirPartyTableSubscribe.RefTableId                                      = 2303 --DirPartyTable.TableId
  96. AND lukDirPartyTableSubscribe.RefRecId                                          = DirPartyTable.RecId
  97. AND lukDirPartyTableSubscribe.Value                                             = 1
  98. ) as                                                                            [PersonnelNumberSubscribe],*/
  99. ContactPerson.lukWebCity                                                        [WebCity]
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. FROM
  108. -- 3 main tables
  109. [AX12].[AX2012R3_PROD].[DBO].[ContactPerson]                                    ContactPerson
  110. JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPersonName]                               DirPersonName
  111. ON DirPersonName.Person                                                         = ContactPerson.Party
  112. JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyTable]                               DirPartyTable
  113. ON DirPartyTable.RecId                                                          = ContactPerson.Party
  114.  
  115. -- left join loyalty (may not exist)
  116. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyTable]                          DirPartyTableLoyalty
  117. ON DirPartyTableLoyalty.RecId                                                   = ContactPerson.lukLoyaltyCardBusinessUnit
  118.  
  119. -- left join treatmentaccount (may not exist)
  120. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[CustTable]                              CustTableTreatment
  121. ON CustTableTreatment.Party                                                     = ContactPerson.ContactForParty
  122. JOIN [AX12].[AX2012R3_PROD].[DBO].[lukTreatmentTable]                           lukTreatmentTable
  123. ON lukTreatmentTable.CustAccount                                                = CustTableTreatment.AccountNum
  124. AND EXISTS (SELECT *
  125.             FROM [AX12].[AX2012R3_PROD].[DBO].[lukTreatmentStatusTable]         lukTreatmentStatusTable
  126.             WHERE lukTreatmentStatusTable.RecId                                 = lukTreatmentTable.StatusRecId
  127.             AND lukTreatmentStatusTable.Status                                  = 0) --lukTreatmentStatusState::Opened
  128.             --order by 1 desc
  129. WHERE ContactPerson.lukLoyaltyCardBusinessUnit <> 0
  130. -- overwritten field
  131. /*
  132. -- outer apply first (earliest) address from RetailTransactionTable (may not exist)
  133. OUTER APPLY (SELECT TOP 1   RetailTransactionTable.Address,
  134.                             RetailTransactionTable.TransDate,
  135.                             RetailTransactionTable.RecId
  136.                 FROM [AX12].[AX2012R3_PROD].[DBO].[CustTable]                   CustTable
  137.                 JOIN [AX12].[AX2012R3_PROD].[DBO].[RetailTransactionTable]      RetailTransactionTable
  138.                 ON RetailTransactionTable.CustAccount                           = CustTable.AccountNum
  139.                 AND RetailTransactionTable.LoyaltyCardId                        <> ''
  140.                 WHERE CustTable.Party                                           = ContactPerson.ContactForParty
  141.                 ORDER BY    TransDate                                           ASC,
  142.                             RecId                                               ASC
  143.             ) as                                                                FinalAddress*/
  144.  
  145.  
  146.  
  147.  
  148. /*JOIN [AX12].[AX2012R3_PROD].[DBO].[CustTable]                         CustTable
  149. ON CustTable.Party                                                      = ContactPerson.ContactForParty
  150. JOIN [AX12].[AX2012R3_PROD].[DBO].[lukTreatmentTable]                   TreatmentTable
  151. ON TreatmentTable.CustAccount                                           = CustTable.AccountNum
  152. AND EXISTS (SELECT *
  153.             FROM [AX12].[AX2012R3_PROD].[DBO].[lukTreatmentStatusTable] TreatmentStatusTable
  154.             WHERE TreatmentStatusTable.RecId                            = TreatmentTable.StatusRecId
  155.             AND TreatmentStatusTable.State                              = 0) --lukTreatmentStatusState::Opened
  156. --TOP 1 RetailTransactionTable.Store ORDER BY RetailTransactiontable.TransDate ASC, RetailTransactionTable.RecId ASC
  157. JOIN [AX12].[AX2012R3_PROD].[DBO].[RetailTransactionTable]              RetailTransactionTable
  158. ON RetailTransactionTable.CustAccount                                   = CustTable.AccountNum
  159. AND RetailTransactionTable.LoyaltyCardId                                <> ''
  160.  
  161. JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyTable]                       DirPartyTableLoyalty
  162. ON DirPartyTableLoyalty.RecId                                           = ContactPerson.lukLoyaltyCardBusinessUnit
  163. JOIN [AX12].[AX2012R3_PROD].[DBO].[DirPartyLocation]                    DirPartyLocation
  164. ON DirPartyLocation.Party                                               = ContactPerson.Party
  165. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[lukDirPartyTableSubscribe]  DirPartyTableSubscribeEmail
  166. ON DirPartyTableSubscribeEmail.RefTableId                               = 2303 --DirPartyTable.TableId
  167. AND DirPartyTableSubscribeEmail.RefRecId                                = DirPartyTable.RecId
  168. AND DirPartyTableSubscribeEmail.SubscribeType                           = 1 --lukSubscribeType::AgreementOnEmail
  169. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[LogisticsElectronicAddress]         LogisticsElectronicAddressEmail
  170. ON LogisticsElectronicAddressEmail.Location                             = DirPartyLocation.Location
  171. AND LogisticsElectronicAddressEmail.Type                                    = 2 --LogisticsElectronicAddressMethodType::Email
  172. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[lukDirPartyTableSubscribe]              DirPartyTableSubscribeSMS
  173. ON DirPartyTableSubscribeSMS.RefTableId                                 = 2303 --DirPartyTable.TableId
  174. AND DirPartyTableSubscribeSMS.RefRecId                                  = DirPartyTable.RecId
  175. AND DirPartyTableSubscribeSMS.SubscribeType                             = 0 --lukSubscribeType::AgreementOnSMS
  176. JOIN [AX12].[AX2012R3_PROD].[DBO].[LogisticsElectronicAddress]          LogisticsElectronicAddressSMS
  177. ON LogisticsElectronicAddressSMS.Location                               = DirPartyLocation.Location
  178. AND LogisticsElectronicAddressSMS.Type                                  = 100 --LogisticsElectronicAddressMethodType::lukSMS
  179. LEFT JOIN [AX12].[AX2012R3_PROD].[DBO].[lukDirPartyTableSubscribe]              DirPartyTableSubscribeBlocked
  180. ON DirPartyTableSubscribeBlocked.RefTableId                             = 2303 --DirPartyTable.TableId
  181. AND DirPartyTableSubscribeBlocked.RefRecId                              = DirPartyTable.RecId
  182. AND DirPartyTableSubscribeBlocked.SubscribeType                         = 2 --lukSubscribeType::BlockSubscribe
  183. JOIN [AX12].[AX2012R3_PROD].[DBO].[LogisticsElectronicAddress]          LogisticsElectronicAddressPhone
  184. ON LogisticsElectronicAddressPhone.Location                             = DirPartyLocation.Location
  185. AND LogisticsElectronicAddressPhone.Type                                = 1 --LogisticsElectronicAddressMethodType::Phone*/
  186.  
  187.  
  188.  
  189. --WHERE ContactPerson.ContactPersonId = N'E000015197'
  190.             --ORDER BY  RetailTransactionTable.TransDate                ASC,
  191.             --          RetailTransactionTable.RecId                    ASC
  192. --          LogisticsElectronicAddress.IsPrimary                DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement