医院跟踪系统客户视图.sql 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. --医院跟踪系统客户视图
  2. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_HospitalCustomer]'))
  3. DROP VIEW [dbo].[View_HospitalCustomer]
  4. GO
  5. CREATE VIEW [dbo].[View_HospitalCustomer]
  6. AS
  7. with t as(
  8. SELECT
  9. [ID]
  10. ,[Hct_Oddnumber]
  11. ,[Hct_CustomerName]
  12. ,dbo.[Fun_GetPy]([Hct_CustomerName]) as [Hct_CustomerName_PY]
  13. ,[Hct_BabyName]
  14. ,dbo.[Fun_GetPy]([Hct_BabyName]) as [Hct_BabyName_PY]
  15. ,[Hct_BabySex]
  16. ,[Hct_BabyBirthday]
  17. ,[Hct_AreLunar]
  18. ,dbo.Fun_GetGregorianCalendar([Hct_BabyBirthday],[Hct_AreLunar]) as GregorianCalendar_Birthday
  19. ,dbo.Fun_GetGregorianCalendarNowYearBirthday([Hct_BabyBirthday],[Hct_AreLunar]) as GregorianCalendar_NowYearBirthday
  20. ,[Hct_BirthdayType]
  21. ,[Hct_MessageSingle]
  22. ,[Hct_CustomerOrders]
  23. ,[Hct_OrderNumber]
  24. ,[Hct_BabyFeeding]
  25. ,[Hct_CustomerRegions]
  26. ,[Hct_CustomerGroups]
  27. ,[Hct_DealStatus]
  28. ,[Hct_SingleHospital]
  29. ,[Hct_MyselfMobile]
  30. ,[Hct_HusbandMobile]
  31. ,[Hct_FamilyPhone]
  32. ,[Hct_Husbandphone]
  33. ,[Hct_Maidenphone]
  34. ,[Hct_QQ]
  35. ,[Hct_PregnantDay]
  36. ,[Hct_Retail]
  37. ,[Hct_Remarks]
  38. ,[Hct_HomeAddress]
  39. ,[Hct_HomePostcode]
  40. ,[Hct_HusbandAddress]
  41. ,[Hct_HusbandPostcode]
  42. ,[Hct_MaidenAddress]
  43. ,[Hct_MaidenPostcode]
  44. ,[Hct_SingleDatetime]
  45. ,[Hcr_BusinessType]
  46. ,[Hct_BusinessIntention]
  47. ,[Hct_CreateDatetime]
  48. ,[Hct_CreateName]
  49. ,[Hct_UpdateDatetime]
  50. ,[Hct_UpdateName]
  51. FROM tb_ErpHospitalClient
  52. )
  53. select
  54. [ID]
  55. ,[Hct_Oddnumber]
  56. ,[Hct_CustomerName]
  57. ,[Hct_CustomerName_PY]
  58. ,[Hct_BabyName]
  59. ,[Hct_BabyName_PY]
  60. ,[Hct_BabySex]
  61. ,[Hct_BabyBirthday]
  62. ,[Hct_AreLunar]
  63. ,GregorianCalendar_Birthday
  64. ,GregorianCalendar_NowYearBirthday
  65. ,[Hct_BirthdayType]
  66. ,FLOOR(datediff(day,GregorianCalendar_Birthday,getdate())/365.25) as Age_Year
  67. ,DATEDIFF(day, GregorianCalendar_Birthday,Getdate()) as Age_Day
  68. ,dbo.Fun_GetAgeString(GregorianCalendar_Birthday) as Age_String
  69. ,[Hct_MessageSingle]
  70. ,[Hct_CustomerOrders]
  71. ,[Hct_OrderNumber]
  72. ,[Hct_BabyFeeding]
  73. ,[Hct_CustomerRegions]
  74. ,[Hct_CustomerGroups]
  75. ,[Hct_DealStatus]
  76. ,[Hct_SingleHospital]
  77. ,[Hct_MyselfMobile]
  78. ,[Hct_HusbandMobile]
  79. ,[Hct_FamilyPhone]
  80. ,[Hct_Husbandphone]
  81. ,[Hct_Maidenphone]
  82. ,[Hct_QQ]
  83. ,[Hct_PregnantDay]
  84. ,[Hct_Retail]
  85. ,[Hct_Remarks]
  86. ,[Hct_HomeAddress]
  87. ,[Hct_HomePostcode]
  88. ,[Hct_HusbandAddress]
  89. ,[Hct_HusbandPostcode]
  90. ,[Hct_MaidenAddress]
  91. ,[Hct_MaidenPostcode]
  92. ,[Hct_SingleDatetime]
  93. ,[Hcr_BusinessType]
  94. ,[Hct_BusinessIntention]
  95. ,[Hct_CreateDatetime]
  96. ,[Hct_CreateName]
  97. ,[Hct_UpdateDatetime]
  98. ,[Hct_UpdateName]
  99. from t
  100. GO
  101. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetHospitalTrackingType]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  102. DROP FUNCTION [dbo].[fn_GetHospitalTrackingType]
  103. GO
  104. create function [dbo].[fn_GetHospitalTrackingType] (@FID int)
  105. Returns varchar(50)
  106. As
  107. Begin
  108. Declare @ReturnValue varchar(50)
  109. if @FID = 0
  110. set @ReturnValue='宝宝生日'
  111. else
  112. set @ReturnValue= '预产期'
  113. return @ReturnValue
  114. End
  115. GO
  116. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetHospitalTrackingDoor]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  117. DROP FUNCTION [dbo].[fn_GetHospitalTrackingDoor]
  118. GO
  119. create function [dbo].[fn_GetHospitalTrackingDoor] (@FID int)
  120. Returns varchar(50)
  121. As
  122. Begin
  123. Declare @ReturnValue varchar(50)
  124. if @FID = 1
  125. set @ReturnValue='OK'
  126. else
  127. set @ReturnValue=''
  128. return @ReturnValue
  129. End
  130. GO
  131. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_HospitalContact]'))
  132. DROP VIEW [dbo].[View_HospitalContact]
  133. GO
  134. CREATE view [dbo].[View_HospitalContact]
  135. as
  136. select
  137. id,Hct_Oddnumber as 来单批号,
  138. Hct_CustomerName as 客户姓名,
  139. Hct_BabyName as 宝宝姓名,
  140. Hct_BabySex as 宝宝性别,
  141. dbo.fn_GetHospitalTrackingType(Hct_BirthdayType) 生日类型,
  142. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hct_BabyBirthday)) as [宝宝生日/预产期],
  143. FLOOR(datediff(day,Hct_BabyBirthday,getdate())/365.25) as 宝宝年龄,
  144. DATEDIFF(day, Hct_BabyBirthday,Getdate()) as 宝宝天数,
  145. dbo.fn_CheckLunar(Hct_AreLunar) as 是否农历,
  146. Hct_PregnantDay as 怀孕天数,
  147. Hct_MyselfMobile as 本人手机,
  148. Hct_HusbandMobile as 老公手机,
  149. Hct_MessageSingle as 发送来单短信,
  150. Hct_BabyFeeding as 喂养方式,
  151. Hct_CustomerRegions as 客户区域,
  152. Hct_CustomerGroups as 客户类别,
  153. Hct_CustomerOrders as 订单客户,
  154. Hcr_BusinessType as 业务种类,
  155. Hct_BusinessIntention as 业务意向,
  156. Hct_SingleHospital as 来单医院,
  157. Hct_Retail as 门市,
  158. Hct_Remarks as 备注,
  159. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hct_CreateDatetime)) as 录入时间,
  160. Hct_CreateDatetime as 录入时间查询
  161. from tb_ErpHospitalClient
  162. GO
  163. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_HospitalDispatchDoor]'))
  164. DROP VIEW [dbo].[View_HospitalDispatchDoor]
  165. GO
  166. CREATE view [dbo].[View_HospitalDispatchDoor]
  167. as
  168. select
  169. tb_ErpHospitalDispatchDoor.id,
  170. Hct_Oddnumber as 来单批号,
  171. Hct_CustomerName as 客户姓名,
  172. Hct_BabyName as 宝宝姓名,
  173. FLOOR(datediff(day,Hct_BabyBirthday,getdate())/365.25) as 宝宝年龄,
  174. Hct_MyselfMobile as 本人手机,
  175. Hct_HusbandMobile as 老公手机,
  176. Hct_CustomerRegions as 客户区域,
  177. Hct_BusinessIntention as 业务意向,
  178. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hdr_DoorTime)) as 上门日期,
  179. Hdr_DoorPeriod as 上门时间,
  180. dbo.fn_GetHospitalTrackingDoor(Hdr_HomeState) as 上门状态,
  181. Hdr_Photographers as 摄影师,
  182. Hdr_BootDivision as 引导师,
  183. Hdr_Vehicle as 车辆,
  184. Hdr_Driver as 司机
  185. from
  186. tb_ErpHospitalClient
  187. left join
  188. tb_ErpHospitalDispatchDoor
  189. on Hct_Oddnumber=Hdr_Number
  190. GO
  191. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_HospitalRemindedToday]'))
  192. DROP VIEW [dbo].[View_HospitalRemindedToday]
  193. GO
  194. CREATE view [dbo].[View_HospitalRemindedToday]
  195. as
  196. select
  197. tb_ErpHospitalContactRecord.id,
  198. Hct_Oddnumber as 来单批号,
  199. Hct_CustomerName as 客户姓名,
  200. Hct_BabyName as 宝宝姓名,
  201. Hct_BabySex as 宝宝性别,
  202. Hct_BirthdayType 生日类型,
  203. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hct_BabyBirthday)) as [宝宝生日/预产期],
  204. FLOOR(datediff(day,Hct_BabyBirthday,getdate())/365.25) as 宝宝年龄,
  205. DATEDIFF(day, Hct_BabyBirthday,Getdate()) as 宝宝天数,
  206. dbo.fn_CheckLunar(Hct_AreLunar) as 是否农历,
  207. Hct_PregnantDay as 怀孕天数,
  208. Hct_MyselfMobile as 本人手机,
  209. Hct_HusbandMobile as 老公手机,
  210. Hct_MessageSingle as 发送来单短信,
  211. Hct_BabyFeeding as 喂养方式,
  212. Hct_CustomerRegions as 客户区域,
  213. Hct_CustomerGroups as 客户类别,
  214. Hct_CustomerOrders as 订单客户,
  215. Hcr_BusinessType as 业务种类,
  216. Hct_BusinessIntention as 业务意向,
  217. Hct_SingleHospital as 来单医院,
  218. Hct_Retail as 门市,
  219. Hct_Remarks as 备注,
  220. dbo.fn_CheckUserIDGetUserName(Hct_CreateName) as 录入员,
  221. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hcr_CallTime)) as 通话时间,
  222. Hcr_CallDuration as 通话时长,
  223. Hcr_CallType as 通话类型,
  224. Hcr_Homeaddress as 具体事项,
  225. Hcr_CallStaff as 通话人员,
  226. Hcr_CallCases as 通话详情,
  227. Hcr_CallMode as 通话方式,
  228. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Hcr_RemindTime)) as 提醒时间,
  229. Hcr_RemindTime as 提醒时间查询,
  230. Hcr_Remarks as 沟通备注
  231. from
  232. tb_ErpHospitalClient
  233. left join
  234. tb_ErpHospitalContactRecord
  235. on Hct_Oddnumber=Hcr_Number
  236. where Hcr_RemindTime<>''
  237. GO