Update_AggregationCustomer.sql 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. update [dbo].[tb_SMSTemplates] set ST_IsEnabled=1 where ST_SendEvent=114 and ST_Type=1 and ST_Category=3
  2. GO
  3. update [dbo].[tb_SMSTemplates] set ST_Category=0 where ST_SendEvent=114 and ST_Type=1
  4. GO
  5. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_getvarchar200]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  6. DROP FUNCTION [dbo].[fn_getvarchar200]
  7. GO
  8. CREATE function [dbo].[fn_getvarchar200](@FID varchar(800))
  9. /******
  10. 字符转换为varchar 200
  11. 创建人:刘工
  12. 创建日期:2014-6-23 14:55:29
  13. 修改人:
  14. 修改说明:
  15. 修改日期:
  16. ******/
  17. Returns varchar(200)
  18. As
  19. Begin
  20. return @FID
  21. End
  22. GO
  23. --客户组成交时间
  24. if not exists
  25. (select * from syscolumns where id=object_id('tb_ErpCustomerGroup') and name='GP_TransactionTime')
  26. begin
  27. alter table tb_ErpCustomerGroup add GP_TransactionTime datetime NULL
  28. end
  29. GO
  30. --添加tb_ErpOrderDigital 中的 Ordv_SelectedPhotoType 字段 用于保存上次选片时用的什么相片选片
  31. if not exists
  32. (select * from syscolumns where id=object_id('tb_ErpOrderDigital') and name='Ordv_SelectedPhotoType')
  33. begin
  34. alter table tb_ErpOrderDigital add Ordv_SelectedPhotoType nvarchar(100) NULL
  35. end
  36. GO
  37. if not exists
  38. (select * from syscolumns where id=object_id('tb_ErpCustomerGroup') and name='GP_LossType')
  39. begin
  40. alter table tb_ErpCustomerGroup add GP_LossType nvarchar(100) NULL
  41. end
  42. GO
  43. if not exists
  44. (select * from syscolumns where id=object_id('tb_ErpOrderProductList') and name='OPlist_UniquelyIdentity')
  45. begin
  46. alter table tb_ErpOrderProductList add OPlist_UniquelyIdentity nvarchar(100) NULL
  47. end
  48. GO
  49. if not exists
  50. (select * from syscolumns where id=object_id('tb_SMSRecords') and name='SR_OpenID')
  51. begin
  52. alter table tb_SMSRecords add SR_OpenID varchar(100) NULL
  53. end
  54. GO
  55. if exists
  56. (select * from syscolumns where id=object_id('tb_ErpOrder') and name='Ord_PhotographyCategory' and [length]<=100)
  57. begin
  58. alter table tb_ErpOrder alter column Ord_PhotographyCategory nvarchar(100)
  59. end
  60. GO
  61. if exists
  62. (select * from syscolumns where id=object_id('tb_ErpCustomer') and name='Cus_NamePinyin' and [length]<=20)
  63. begin
  64. alter table tb_ErpCustomer alter column Cus_NamePinyin nvarchar(20)
  65. end
  66. GO
  67. if exists
  68. (select * from syscolumns where id=object_id('tb_ErpShopBulletin') and name='Bulletin_Accessory' and [length]>0)
  69. begin
  70. alter table tb_ErpShopBulletin alter column Bulletin_Accessory nvarchar(max)
  71. end
  72. GO
  73. if exists
  74. (select * from syscolumns where id=object_id('tb_SMSRecords') and name='SR_SMSSendIdentity' and [length]<=100)
  75. begin
  76. alter table [dbo].[tb_SMSRecords] alter column [SR_SMSSendIdentity] nvarchar(100)
  77. end
  78. GO
  79. if exists
  80. (select * from syscolumns where id=object_id('tb_SMSRecords') and name='SR_Content' and [length]<=1000)
  81. begin
  82. alter table [dbo].[tb_SMSRecords] alter column [SR_Content] nvarchar(800)
  83. end
  84. GO
  85. if exists
  86. (select * from syscolumns where id=object_id('tb_SMSRecords') and name='SR_RecipientNumber' and [length]>0)
  87. begin
  88. alter table [dbo].[tb_SMSRecords] alter column [SR_RecipientNumber] nvarchar(max)
  89. end
  90. GO
  91. if not exists
  92. (select * from syscolumns where id=object_id('tb_ErpUser') and name='User_OpenID')
  93. begin
  94. alter table tb_ErpUser add User_OpenID varchar(200) NULL
  95. end
  96. GO
  97. if not exists
  98. (select * from syscolumns where id=object_id('tb_ErpCustomer') and name='Cus_OpenID')
  99. begin
  100. alter table tb_ErpCustomer add Cus_OpenID varchar(200) NULL
  101. end
  102. GO
  103. IF EXISTS (SELECT * FROM tb_ErpSystemCategory WHERE Sc_ClassCode='BEBCADBFBCDFBGJHC')
  104. begin
  105. delete from tb_ErpSystemCategory where Sc_ClassCode='BEBCADBFBCDFBGJHC'
  106. end
  107. GO
  108. IF EXISTS (SELECT * FROM tb_ErpSystemCategory WHERE Sc_ClassCode='BEBCADBFBDDEGGABF')
  109. begin
  110. delete from tb_ErpSystemCategory where Sc_ClassCode='BEBCADBFBDDEGGABF'
  111. end
  112. GO
  113. IF EXISTS (SELECT * FROM tb_ErpSystemCategory WHERE Sc_ClassCode='BEBCBFBICGDHCGBEJ')
  114. begin
  115. delete from tb_ErpSystemCategory where Sc_ClassCode='BEBCBFBICGDHCGBEJ'
  116. end
  117. GO
  118. update [dbo].[tb_ErpCompanyInfo] set Company_MicroShareAccount='',Company_MicroSharePassword='' where Company_MicroShareAccount='windylel' and Company_MicroSharePassword='19880528'
  119. GO
  120. if exists
  121. (select * from syscolumns where id=object_id('tb_ErpSystemLogs') and name='Slogs_Content' and [length]>0)
  122. begin
  123. alter table [dbo].[tb_ErpSystemLogs] alter column [Slogs_Content] nvarchar(max)
  124. end
  125. GO
  126. if exists
  127. (select * from syscolumns where id=object_id('tb_ErpSystemLogs') and name='Slogs_CreateName' and [length]>0 and [length]<200)
  128. begin
  129. alter table [dbo].[tb_ErpSystemLogs] alter column [Slogs_CreateName] nvarchar(100)
  130. end
  131. GO
  132. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_ErpCustomer]'))
  133. DROP VIEW [dbo].[View_ErpCustomer]
  134. GO
  135. CREATE VIEW [dbo].[View_ErpCustomer]
  136. AS
  137. SELECT
  138. [Cus_CustomerNumber]
  139. ,[Cus_CustomizeNumber]
  140. --,[Cus_Type]
  141. --,[Cus_ServiceType]
  142. ,[Cus_Grade]
  143. ,[Cus_Name]
  144. ,Cus_NamePinyin as [Cus_Name_py]
  145. ,[Cus_Sex]
  146. ,dbo.Fun_ConvertSex([Cus_Sex]) as [Cus_Sex_cs]
  147. ,[Cus_Birthday]
  148. ,[Cus_BirthdayLunar]
  149. ,dbo.Fun_GetGregorianCalendar([Cus_Birthday],[Cus_BirthdayLunar]) as GregorianCalendar_Birthday
  150. ,dbo.Fun_GetGregorianCalendarNowYearBirthday([Cus_Birthday],[Cus_BirthdayLunar]) as GregorianCalendar_NowYearBirthday
  151. ,[Cus_DayForMarriage]
  152. ,[Cus_DayForMarriageLunar]
  153. ,dbo.Fun_GetGregorianCalendar([Cus_DayForMarriage],[Cus_DayForMarriageLunar]) as GregorianCalendar_DayForMarriage
  154. ,dbo.Fun_GetGregorianCalendarNowYearBirthday([Cus_DayForMarriage],[Cus_DayForMarriageLunar]) as GregorianCalendar_NowYearDayForMarriage
  155. --,[Cus_Relations] 关系
  156. ,[Cus_IDNumber]
  157. ,[Cus_QQ]
  158. ,[Cus_MicroSignal]
  159. ,[Cus_Telephone]
  160. ,[Cus_FixedPhone]
  161. ,[Cus_Region]
  162. ,[Cus_Address]
  163. ,[Cus_WorkUnit]
  164. ,[Cus_BabyWeight]
  165. ,[Cus_BornHospital]
  166. ,[Cus_Zodiac]
  167. ,[Cus_CustomerSource]
  168. ,[Cus_Status]
  169. -- ,[Cus_LossReason]客户流失原因
  170. ,[Cus_DegreeOfIntent]
  171. ,[Cus_TrackName]
  172. --,[Cus_Remark]
  173. ,[Cus_CreateDateTime]
  174. ,[Cus_CreateName]
  175. ,[Cus_UpdateDateTime]
  176. ,[Cus_UpdateName]
  177. ,[Cus_OpenID]
  178. FROM tb_ErpCustomer
  179. GO
  180. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_CustomerGroupAndCustomerGroupMembersAndErpCustomer]'))
  181. DROP VIEW [dbo].[View_CustomerGroupAndCustomerGroupMembersAndErpCustomer]
  182. GO
  183. CREATE VIEW [dbo].[View_CustomerGroupAndCustomerGroupMembersAndErpCustomer]
  184. AS
  185. SELECT [tb_ErpCustomerGroup].[id]
  186. ,[GP_OrderNumber]
  187. ,[GP_CustomerGroupID]
  188. ,[GP_GroupType]
  189. ,[GP_CustomerType]
  190. ,[GP_CustomerStatus]
  191. ,[GP_LossReason]
  192. ,[GP_DegreeOfIntent]
  193. ,[GP_CustomerSource]
  194. ,[GP_IntroducerCustomerID]
  195. ,[GP_Remark]
  196. ,[GP_BelongsPersonID]
  197. ,[GP_CreateDatetime]
  198. ,[GP_UpdateDatetime]
  199. ,[GM_CustomerGroupID]
  200. ,[GM_CustomerID]
  201. ,[GM_Master]
  202. ,[GM_ProtagonistCustomer]
  203. ,[GM_RelatedPersonID]
  204. ,[GM_Relation]
  205. ,[GM_IsOrder]
  206. ,[GM_IsOrderNumber]
  207. ,[Cus_CustomerNumber]
  208. ,[Cus_CustomizeNumber]
  209. ,[Cus_Grade]
  210. ,[Cus_Name]
  211. ,[Cus_Name_py]
  212. ,[Cus_Sex]
  213. ,[Cus_Sex_cs]
  214. ,[Cus_Birthday]
  215. ,[Cus_BirthdayLunar]
  216. ,[GregorianCalendar_Birthday]
  217. ,[GregorianCalendar_NowYearBirthday]
  218. ,[dbo].[Fun_GetAgeYear](GregorianCalendar_Birthday) as Age_Year
  219. ,[dbo].[Fun_GetAgeDay](GregorianCalendar_Birthday) as Age_Day
  220. ,dbo.Fun_GetAgeString(GregorianCalendar_Birthday) as Age_String
  221. ,[Cus_DayForMarriage]
  222. ,[Cus_DayForMarriageLunar]
  223. ,GregorianCalendar_DayForMarriage
  224. ,GregorianCalendar_NowYearDayForMarriage
  225. ,[Cus_IDNumber]
  226. ,[Cus_QQ]
  227. ,[Cus_MicroSignal]
  228. ,[Cus_Telephone]
  229. ,[Cus_FixedPhone]
  230. ,[Cus_Region]
  231. ,[Cus_Address]
  232. ,[Cus_WorkUnit]
  233. ,[Cus_BabyWeight]
  234. ,[Cus_BornHospital]
  235. ,[Cus_Zodiac]
  236. ,[Cus_CustomerSource]
  237. ,[Cus_Status]
  238. ,[Cus_DegreeOfIntent]
  239. ,[Cus_TrackName]
  240. ,[Cus_CreateDateTime]
  241. ,[Cus_CreateName]
  242. ,[Cus_UpdateDateTime]
  243. ,[Cus_UpdateName]
  244. ,[Cus_OpenID]
  245. FROM [tb_ErpCustomerGroup]
  246. left join
  247. dbo.tb_ErpCustomerGroupMembers
  248. on [tb_ErpCustomerGroup].[GP_CustomerGroupID]=dbo.tb_ErpCustomerGroupMembers.[GM_CustomerGroupID]
  249. left join
  250. [dbo].[View_ErpCustomer]
  251. on dbo.tb_ErpCustomerGroupMembers.GM_CustomerID=[dbo].[View_ErpCustomer].Cus_CustomerNumber
  252. GO
  253. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_CustomerGroupMembersAndErpCustomer]'))
  254. DROP VIEW [dbo].[View_CustomerGroupMembersAndErpCustomer]
  255. GO
  256. CREATE VIEW [dbo].[View_CustomerGroupMembersAndErpCustomer]
  257. AS
  258. SELECT [tb_ErpCustomerGroupMembers].[id]
  259. ,[GM_CustomerGroupID]
  260. ,[GM_CustomerID]
  261. ,[GM_Master]
  262. ,[GM_ProtagonistCustomer]
  263. ,[GM_RelatedPersonID]
  264. ,[GM_Relation]
  265. ,[GM_IsOrder]
  266. ,[GM_IsOrderNumber]
  267. ,[Cus_CustomerNumber]
  268. ,[Cus_CustomizeNumber]
  269. ,[Cus_Grade]
  270. ,[Cus_Name]
  271. ,[Cus_Name_py]
  272. ,[Cus_Sex]
  273. ,[Cus_Sex_cs]
  274. ,[Cus_Birthday]
  275. ,[Cus_BirthdayLunar]
  276. ,GregorianCalendar_Birthday
  277. ,GregorianCalendar_NowYearBirthday
  278. ,[dbo].[Fun_GetAgeYear](GregorianCalendar_Birthday) as Age_Year
  279. ,[dbo].[Fun_GetAgeDay](GregorianCalendar_Birthday) as Age_Day
  280. ,dbo.Fun_GetAgeString(GregorianCalendar_Birthday) as Age_String
  281. ,[Cus_DayForMarriage]
  282. ,[Cus_DayForMarriageLunar]
  283. ,GregorianCalendar_DayForMarriage
  284. ,GregorianCalendar_NowYearDayForMarriage
  285. ,[Cus_IDNumber]
  286. ,[Cus_QQ]
  287. ,[Cus_MicroSignal]
  288. ,[Cus_Telephone]
  289. ,[Cus_FixedPhone]
  290. ,[Cus_Region]
  291. ,[Cus_Address]
  292. ,[Cus_WorkUnit]
  293. ,[Cus_BabyWeight]
  294. ,[Cus_BornHospital]
  295. ,[Cus_Zodiac]
  296. ,[Cus_CustomerSource]
  297. ,[Cus_Status]
  298. ,[Cus_DegreeOfIntent]
  299. ,[Cus_TrackName]
  300. ,[Cus_CreateDateTime]
  301. ,[Cus_CreateName]
  302. ,[Cus_UpdateDateTime]
  303. ,[Cus_UpdateName]
  304. ,[Cus_OpenID]
  305. ,[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CradNumber]
  306. ,[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CardType]
  307. FROM [tb_ErpCustomerGroupMembers]
  308. left join [View_ErpCustomer]
  309. on [tb_ErpCustomerGroupMembers].[GM_CustomerID]=[View_ErpCustomer].Cus_CustomerNumber
  310. left join
  311. [dbo].[Vw_ErpMemberCardSimpleInfo]
  312. on [View_ErpCustomer].Cus_CustomerNumber=[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CustomerNumber]
  313. GO
  314. if not exists
  315. (select * from syscolumns where id=object_id('tb_ErpCustomerGroup') and name='GP_LossType')
  316. begin
  317. alter table tb_ErpCustomerGroup add GP_LossType nvarchar(100) NULL
  318. end
  319. GO
  320. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_AggregationCustomer]'))
  321. DROP VIEW [dbo].[Vw_AggregationCustomer]
  322. GO
  323. --[GM_CustomerID]==GM_RelatedPersonID 表示本人
  324. CREATE VIEW [dbo].[Vw_AggregationCustomer]
  325. as
  326. with t as(
  327. select
  328. GM_CustomerGroupID
  329. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_Name]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  330. +'/'+ dbo.AggregateString([Cus_Name]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Name
  331. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_Name_py]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  332. +'/'+ dbo.AggregateString([Cus_Name_py]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Name_py
  333. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_Sex_cs]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  334. +'/'+ dbo.AggregateString([Cus_Sex_cs]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Sex_cs
  335. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_Telephone]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  336. +'/'+ dbo.AggregateString([Cus_Telephone]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Telephone
  337. --OPenID
  338. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_OpenID]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  339. +'/'+ dbo.AggregateString([Cus_OpenID]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_OpenID
  340. --地区
  341. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_Region]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  342. +'/'+ dbo.AggregateString([Cus_Region]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Region
  343. --地址
  344. ,CONVERT(nvarchar(2000),dbo.FunTrim(dbo.AggregateString([Cus_Address]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  345. +'/'+ dbo.AggregateString([Cus_Address]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_Address
  346. --微信号
  347. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_MicroSignal]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  348. +'/'+ dbo.AggregateString([Cus_MicroSignal]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_MicroSignal
  349. --QQ号
  350. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_QQ]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  351. +'/'+ dbo.AggregateString([Cus_QQ]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_QQ
  352. --工作单位
  353. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_WorkUnit]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  354. +'/'+ dbo.AggregateString([Cus_WorkUnit]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_WorkUnit
  355. --客户来源
  356. ,CONVERT(nvarchar(200),dbo.FunTrim(dbo.AggregateString([Cus_CustomerSource]+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')
  357. +'/'+ dbo.AggregateString([Cus_CustomerSource]+'{$$}/{where}'+[GM_CustomerID]+'{$$}<>{$$}'+GM_RelatedPersonID+'{$$}s'),'/')) as Cus_CustomerSource
  358. --年龄字符串
  359. ,CONVERT(nvarchar(200),dbo.AggregateString(CONVERT(nvarchar(10),[Age_String])+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_ProtagonistCustomer])+'{$$}={$$}1{$$}i')) as Age_String
  360. --儿童年龄天数 int 型
  361. ,CONVERT(int,dbo.AggregateString(CONVERT(nvarchar(10),[Age_Day])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as Age_Day
  362. --儿童年龄岁数 int 型
  363. ,CONVERT(int,dbo.AggregateString(CONVERT(nvarchar(10),[Age_Year])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as Age_Year
  364. --儿童生日或主联系人生日 原始输入
  365. ,CONVERT(nvarchar(10),dbo.AggregateString(CONVERT(nvarchar(10),[Cus_Birthday])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as Cus_Birthday
  366. --儿童生日或主联系人生日 是否农历
  367. ,CONVERT(nvarchar(10),dbo.AggregateString(CONVERT(varchar(1),[Cus_BirthdayLunar])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as Cus_BirthdayLunar
  368. --儿童生日或主联系人生日 统一转为公历
  369. ,CONVERT(datetime,dbo.AggregateString(CONVERT(nvarchar(10),[GregorianCalendar_Birthday])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as GregorianCalendar_Birthday
  370. --儿童当前年生日日期或主联系人当前年生日日期 统一转为公历
  371. ,CONVERT(datetime,dbo.AggregateString(CONVERT(nvarchar(10),[GregorianCalendar_NowYearBirthday])+'{$$}/{where}'+[GM_CustomerID]+'{$$}={$$}'+GM_RelatedPersonID+'{$$}s')) as GregorianCalendar_NowYearBirthday
  372. --主联系人婚期 原始输入
  373. ,CONVERT(nvarchar(10),dbo.AggregateString(CONVERT(nvarchar(10),[Cus_DayForMarriage])+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as Cus_DayForMarriage
  374. --主联系人婚期 是否农历
  375. ,CONVERT(nvarchar(1),dbo.AggregateString(CONVERT(nvarchar(1),[Cus_DayForMarriageLunar])+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as Cus_DayForMarriageLunar
  376. --主联系人婚期 统一转为公历
  377. ,CONVERT(datetime,dbo.AggregateString(CONVERT(nvarchar(10),[GregorianCalendar_DayForMarriage])+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as GregorianCalendar_DayForMarriage
  378. --主联系人当前年婚期日期 统一转为公历
  379. ,CONVERT(datetime,dbo.AggregateString(CONVERT(nvarchar(10),[GregorianCalendar_NowYearDayForMarriage])+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as GregorianCalendar_NowYearDayForMarriage
  380. --以下是主要联系人
  381. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_CustomerNumber]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_CustomerNumber
  382. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_Name]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_Name
  383. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_Name_py]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_Name_py
  384. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_Sex_cs]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_Sex_cs
  385. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_Telephone]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_Telephone
  386. --主要联系人微信号
  387. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_MicroSignal]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_MicroSignal
  388. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_QQ]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_QQ
  389. --主要联系人地区
  390. ,CONVERT(nvarchar(200),dbo.AggregateString([Cus_Region]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i')) as M_Cus_Region
  391. --主要联系人地址
  392. ,dbo.AggregateString([Cus_Address]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i') as M_Cus_Address
  393. --主要联系人工作单位
  394. ,dbo.AggregateString([Cus_WorkUnit]+'{$$}/{where}'+CONVERT(nvarchar(1),[GM_Master])+'{$$}={$$}1{$$}i') as M_Cus_WorkUnit
  395. --客户组中成员个数
  396. ,count(Cus_CustomerNumber) as Ord_ViceOrderCount
  397. from
  398. dbo.View_CustomerGroupMembersAndErpCustomer
  399. group by GM_CustomerGroupID
  400. )
  401. select
  402. [tb_ErpCustomerGroup].id
  403. ,[GP_OrderNumber]
  404. ,[GP_CustomerGroupID]
  405. ,[GP_GroupType]
  406. ,[GP_CustomerType]
  407. ,[GP_CustomerStatus]
  408. ,[GP_LossType]
  409. ,[GP_LossReason]
  410. ,[GP_DegreeOfIntent]
  411. ,[GP_CustomerSource]
  412. ,GP_CreateDatetime
  413. ,GP_UpdateDatetime
  414. ,GP_ISIntentionCustomer
  415. ,GP_ReminderTime
  416. ,GP_RemindEvent
  417. ,[GP_IntroducerCustomerID],t.*
  418. ,[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CradNumber]
  419. ,[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CardType]
  420. ,1 as v160604
  421. from
  422. [tb_ErpCustomerGroup]
  423. left join
  424. t
  425. on [tb_ErpCustomerGroup].[GP_CustomerGroupID]=t.[GM_CustomerGroupID]
  426. left join
  427. [dbo].[Vw_ErpMemberCardSimpleInfo]
  428. on t.M_Cus_CustomerNumber=[dbo].[Vw_ErpMemberCardSimpleInfo].[Mc_CustomerNumber]
  429. GO
  430. --根据聚合后的客户视图创建聚合后的客户临时表
  431. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tempTB_AggregationCustomer]'))
  432. DROP table [dbo].[tempTB_AggregationCustomer]
  433. GO
  434. select * into tempTB_AggregationCustomer from [Vw_AggregationCustomer]
  435. GO
  436. --唯一索引
  437. CREATE UNIQUE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_GP_CustomerGroupID] ON [dbo].[tempTB_AggregationCustomer]
  438. (
  439. [GP_CustomerGroupID] desc
  440. )
  441. --索引
  442. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_GP_OrderNumber] ON [dbo].[tempTB_AggregationCustomer]
  443. (
  444. [GP_OrderNumber] desc
  445. )
  446. GO
  447. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_Cus_Name] ON [dbo].[tempTB_AggregationCustomer]
  448. (
  449. [Cus_Name] desc
  450. )
  451. GO
  452. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_Cus_Name_py] ON [dbo].[tempTB_AggregationCustomer]
  453. (
  454. Cus_Name_py desc
  455. )
  456. go
  457. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_Cus_Telephone] ON [dbo].[tempTB_AggregationCustomer]
  458. (
  459. Cus_Telephone desc
  460. )
  461. GO
  462. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_GP_CustomerSource] ON [dbo].[tempTB_AggregationCustomer]
  463. (
  464. [GP_CustomerSource] desc
  465. )
  466. GO
  467. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_M_Cus_Name] ON [dbo].[tempTB_AggregationCustomer]
  468. (
  469. [M_Cus_Name] desc
  470. )
  471. GO
  472. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_M_Cus_Name_py] ON [dbo].[tempTB_AggregationCustomer]
  473. (
  474. M_Cus_Name_py desc
  475. )
  476. GO
  477. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_M_Cus_Telephone] ON [dbo].[tempTB_AggregationCustomer]
  478. (
  479. M_Cus_Telephone desc
  480. )
  481. GO
  482. CREATE NONCLUSTERED INDEX [IX_tempTB_AggregationCustomer_Mc_CradNumber] ON [dbo].[tempTB_AggregationCustomer]
  483. (
  484. Mc_CradNumber desc
  485. )
  486. GO
  487. --创建视图 dbo.tempTB_AggregationCustomer 表的Vw_GroupByAggregationCustomer视图
  488. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_GroupByAggregationCustomer]'))
  489. DROP VIEW [dbo].[Vw_GroupByAggregationCustomer]
  490. GO
  491. CREATE VIEW [Vw_GroupByAggregationCustomer]
  492. AS
  493. with t as(
  494. SELECT
  495. max(GP_CustomerGroupID) as GP_CustomerGroupID
  496. from tempTB_AggregationCustomer
  497. group by Cus_Name,M_Cus_CustomerNumber
  498. )
  499. select * from tempTB_AggregationCustomer
  500. where GP_CustomerGroupID in (select GP_CustomerGroupID from t)
  501. GO
  502. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tempTB_GroupByAggregationCustomer]'))
  503. DROP table [dbo].[tempTB_GroupByAggregationCustomer]
  504. GO
  505. select * into tempTB_GroupByAggregationCustomer from [Vw_GroupByAggregationCustomer]
  506. GO
  507. --唯一索引
  508. CREATE UNIQUE NONCLUSTERED INDEX [IX_tempTB_GroupByAggregationCustomer_GP_CustomerGroupID] ON [dbo].[tempTB_GroupByAggregationCustomer]
  509. (
  510. [GP_CustomerGroupID] desc
  511. )
  512. --索引
  513. CREATE NONCLUSTERED INDEX [IX_tempTB_GroupByAggregationCustomer_GP_OrderNumber] ON [dbo].[tempTB_GroupByAggregationCustomer]
  514. (
  515. [GP_OrderNumber] desc
  516. )
  517. GO
  518. CREATE NONCLUSTERED INDEX [IX_tempTB_GroupByAggregationCustomer_Cus_Name] ON [dbo].[tempTB_GroupByAggregationCustomer]
  519. (
  520. [Cus_Name] desc
  521. )
  522. GO
  523. CREATE NONCLUSTERED INDEX [IX_tempTB_GroupByAggregationCustomer_Cus_Name_py] ON [dbo].[tempTB_GroupByAggregationCustomer]
  524. (
  525. Cus_Name_py desc
  526. )
  527. go
  528. CREATE NONCLUSTERED INDEX [IX_tempTB_GroupByAggregationCustomer_Cus_Telephone] ON [dbo].[tempTB_GroupByAggregationCustomer]
  529. (
  530. Cus_Telephone desc
  531. )
  532. GO
  533. --------