工资统计订单信息 存储过程.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. USE [LYFZERPDB]
  2. GO
  3. EXEC [dbo].[PROCE_WageStatisticsOrder]
  4. @datatimeStar = N'2016-01-01',
  5. @datatimeEnd = N'2016-11-01',
  6. @storeID='''LDPHN_TW4M2R'',''SSN001'''
  7. GO
  8. EXEC [dbo].[PROCE_WageStatisticsOrderReceiptRecord]
  9. @datatimeStar = N'2016-10-01',
  10. @datatimeEnd = N'2016-10-31',
  11. @storeID=''
  12. GO
  13. EXEC [dbo].[PROCE_WageStatisticsSatisfactionScore]
  14. @datatimeStar = N'2016-09-01',
  15. @datatimeEnd = N'2016-11-01'
  16. GO
  17. --工资统计订单信息 存储过程
  18. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PROCE_WageStatisticsOrder]') AND type in (N'P', N'PC'))
  19. DROP PROCEDURE [dbo].[PROCE_WageStatisticsOrder]
  20. GO
  21. CREATE PROCEDURE [dbo].[PROCE_WageStatisticsOrder]
  22. (
  23. @datatimeStar datetime, --开始时间
  24. @datatimeEnd datetime, --结束时间
  25. @storeID varchar(1000) --门店iD
  26. )
  27. AS
  28. --工资统计订单信息
  29. BEGIN
  30. --处理开始点和结束点
  31. --Declare @MaxRecord int;
  32. --Declare @TotalCountSql nvarchar(2000);
  33. --8大时间查询
  34. --订单信息
  35. --联合查询
  36. Declare @sqlString varchar(max);
  37. set @sqlString=''
  38. if(len(@storeID)>0)
  39. set @sqlString='Ord_DividedShop in ('+@storeID+') and '
  40. --select @OrdersPerson=isnull(@OrdersPerson,'''0''')+','''+ cast(ordNumber as nvarchar(20))+'''' from orderList --数据大于1万条时效率太低
  41. Declare @OrdersPerson varchar(max);
  42. set @OrdersPerson='
  43. with ord as(
  44. --订单时间
  45. select Ord_Number as ordNumber,Ord_CreateDateTime as RDateTime from dbo.tb_ErpOrder where Ord_Class=1
  46. UNION
  47. --拍照时间
  48. select Ordpg_Number as ordNumber,max(Ordpg_PhotographyTime) as RDateTime from dbo.tb_ErpOrdersPhotography where Ordpg_PhotographyTime>0 group by Ordpg_Number
  49. UNION
  50. --初修时间
  51. select Ordv_Number as ordNumber,max(Ordv_EarlyRepairTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_EarlyRepairTime>0 group by Ordv_Number
  52. UNION
  53. --精修时间
  54. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  55. UNION
  56. --设计修时间
  57. select Ordv_Number as ordNumber,max(Ordv_DesignerTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_DesignerTime>0 group by Ordv_Number
  58. UNION
  59. --选片时间
  60. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  61. UNION
  62. --取件时间
  63. select OPlist_OrderNumber as ordNumber, max(OPlist_PickupTime) as RDateTime from [tb_ErpOrderProductList] where OPlist_Type =2 and OPlist_PickupTime>0 group by OPlist_OrderNumber
  64. UNION
  65. --收款时间
  66. select Pay_OrdNumber as ordNumber,Pay_CreateDatetime as RDateTime from [dbo].[tb_ErpPayment] where Pay_Type =0
  67. ),
  68. orderList as
  69. (
  70. select ordNumber from ord where RDateTime>='''+CONVERT(nvarchar(10),@datatimeStar)+''' and RDateTime<'''+CONVERT(nvarchar(10),@datatimeEnd)+''' group by ordNumber
  71. ), t as
  72. (
  73. select OrdPe_OrderNumber, dbo.AggregateString([OrdPe_OrdersPerson]+''{$$},{$$}s'') as OrdPe_OrdersPerson from dbo.tb_ErpOrdersPerson
  74. group by OrdPe_OrderNumber
  75. ),
  76. p as
  77. (
  78. select max(Ordpg_Number) as Ordpg_Number,Ordpg_ViceNumber,
  79. dbo.AggregateString(Ordpg_Sights+''{$$},{$$}s'') as Ordpg_Sights,
  80. dbo.AggregateString(Ordpg_Photographer+''{$$},{$$}s'') as Ordpg_Photographer,
  81. max(Ordpg_PhotographyTime) as Ordpg_PhotographyTime,
  82. (case when (sum(CONVERT(int,Ordpg_PhotographyStatus)))=(count(1)) then 1 else 0 end) as PhotographyStatus ,
  83. dbo.AggregateString(Ordpg_PhotographyAssistant+''{$$},{$$}s'') as Ordpg_PhotographyAssistant,
  84. dbo.AggregateString(Ordpg_MakeupArtist+''{$$},{$$}s'') as Ordpg_MakeupArtist,
  85. dbo.AggregateString(Ordpg_MakeupAssistant+''{$$},{$$}s'') as Ordpg_MakeupAssistant,
  86. dbo.AggregateString(Ordpg_BootDivision+''{$$},{$$}s'') as Ordpg_BootDivision,
  87. dbo.AggregateString(Ordpg_BootDivisionAssistant+''{$$},{$$}s'') as Ordpg_BootDivisionAssistant
  88. from dbo.tb_ErpOrdersPhotography
  89. group by Ordpg_ViceNumber
  90. )
  91. select Ord_DividedShop,Ord_Number,Ordv_ViceNumber,Ordpg_Sights,Ord_SinceOrderNumber,Ord_Type,Ord_Class,Ord_OrderClass,Ord_PhotographyCategory,Ord_SeriesName,Ord_SeriesPrice,Ord_CreateDateTime
  92. ,OrdPe_OrdersPerson
  93. ,Ordv_DigitalNumber
  94. ,Ordv_EarlyRepairName,Ordv_EarlyRepairTime,ordv_EarlyRepairStatus,
  95. Ordv_FilmSelectionName,Ordv_FilmSelectionTime,Ordv_FilmSelectionStatus,Ordv_DesignerName,
  96. Ordv_DesignerTime,Ordv_DesignerStatus,Ordv_RefinementName,Ordv_RefinementTime,Ordv_RefinementStatus,
  97. Ordv_LookDesignName,Ordv_LookDesignTime,Ordv_LookDesignStatus
  98. ,Ordpg_Photographer
  99. ,Ordpg_PhotographyTime,PhotographyStatus
  100. ,Ordpg_PhotographyAssistant,Ordpg_MakeupArtist
  101. ,Ordpg_MakeupAssistant,Ordpg_BootDivision,Ordpg_BootDivisionAssistant
  102. ,[GP_CustomerSource],[Cus_Name]
  103. ,[dbo].[fun_GetPickupStatusStatus]([OPlist_PickupStatus],productCount) AS PickupStatus,[OPlist_PickupTime]
  104. from dbo.tb_ErpOrder
  105. left join t on Ord_Number= OrdPe_OrderNumber
  106. left join dbo.tb_ErpOrderDigital on Ord_Number=Ordv_Number
  107. left join p on Ordv_ViceNumber=Ordpg_ViceNumber
  108. left join [dbo].[Vw_OrderProductPickupView] on Ordv_ViceNumber=OPlist_ViceNumber
  109. left join [dbo].[tempTB_AggregationCustomer] on Ord_Number=[GP_OrderNumber]
  110. where '+@sqlString+' Ord_Class=1 and Ord_Number in(select ordNumber from orderList)
  111. '
  112. exec(@OrdersPerson)
  113. END
  114. GO
  115. --工资统计订单收款记录 存储过程
  116. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PROCE_WageStatisticsOrderReceiptRecord]') AND type in (N'P', N'PC'))
  117. DROP PROCEDURE [dbo].[PROCE_WageStatisticsOrderReceiptRecord]
  118. GO
  119. CREATE PROCEDURE [dbo].[PROCE_WageStatisticsOrderReceiptRecord]
  120. (
  121. @datatimeStar datetime, --开始时间
  122. @datatimeEnd datetime, --结束时间
  123. @storeID varchar(1000) --门店iD
  124. )
  125. AS
  126. --工资统计订单收款记录
  127. BEGIN
  128. --处理开始点和结束点
  129. Declare @sqlString varchar(max);
  130. set @sqlString=''
  131. if(len(@storeID)>0)
  132. set @sqlString='Pay_DividedShop in ('+@storeID+') and '
  133. Declare @OrdersPerson varchar(max);
  134. set @OrdersPerson='
  135. with ord as(
  136. --订单时间
  137. select Ord_Number as ordNumber,Ord_CreateDateTime as RDateTime from dbo.tb_ErpOrder where Ord_Class=1
  138. UNION
  139. --拍照时间
  140. select Ordpg_Number as ordNumber,max(Ordpg_PhotographyTime) as RDateTime from dbo.tb_ErpOrdersPhotography where Ordpg_PhotographyTime>0 group by Ordpg_Number
  141. UNION
  142. --初修时间
  143. select Ordv_Number as ordNumber,max(Ordv_EarlyRepairTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_EarlyRepairTime>0 group by Ordv_Number
  144. UNION
  145. --精修时间
  146. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  147. UNION
  148. --设计修时间
  149. select Ordv_Number as ordNumber,max(Ordv_DesignerTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_DesignerTime>0 group by Ordv_Number
  150. UNION
  151. --选片时间
  152. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  153. UNION
  154. --取件时间
  155. select OPlist_OrderNumber as ordNumber, max(OPlist_PickupTime) as RDateTime from [tb_ErpOrderProductList] where OPlist_Type =2 and OPlist_PickupTime>0 group by OPlist_OrderNumber
  156. UNION
  157. --收款时间
  158. select Pay_OrdNumber as ordNumber,Pay_CreateDatetime as RDateTime from [dbo].[tb_ErpPayment] where Pay_Type =0
  159. ),
  160. orderList as
  161. (
  162. select ordNumber from ord where RDateTime>='''+CONVERT(nvarchar(10),@datatimeStar)+''' and RDateTime<'''+CONVERT(nvarchar(10),@datatimeEnd)+''' group by ordNumber
  163. )
  164. select Pay_OrdNumber,
  165. Pay_ViceNumber,
  166. Pay_ShootingName,
  167. Pay_Category,
  168. Pay_TwoPinsCategory,
  169. (Pay_AmountOf - Pay_DiscountAmount) as PaidAmount,
  170. Pay_AmountOf,
  171. Pay_PaymentDiscount,
  172. Pay_DiscountAmount,
  173. Pay_OpenSingle,
  174. Pay_ThePayee,
  175. Pay_Type,
  176. Pay_DividedShop,
  177. Pay_PaymentMethod,
  178. Pay_ReceivableProject,
  179. Pay_CreateDatetime from [dbo].[tb_ErpPayment] where '+@sqlString+' Pay_Type =0
  180. and Pay_OrdNumber in(select ordNumber from orderList)'
  181. exec(@OrdersPerson)
  182. END
  183. GO
  184. --工资统计满意度分值 存储过程
  185. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PROCE_WageStatisticsSatisfactionScore]') AND type in (N'P', N'PC'))
  186. DROP PROCEDURE [dbo].[PROCE_WageStatisticsSatisfactionScore]
  187. GO
  188. CREATE PROCEDURE [dbo].[PROCE_WageStatisticsSatisfactionScore]
  189. (
  190. @datatimeStar datetime, --开始时间
  191. @datatimeEnd datetime --结束时间
  192. )
  193. AS
  194. --工资统计满意度分值
  195. BEGIN
  196. --处理开始点和结束点
  197. --联合查询
  198. Declare @OrdersPerson varchar(max);
  199. set @OrdersPerson='
  200. with ord as(
  201. --订单时间
  202. select Ord_Number as ordNumber,Ord_CreateDateTime as RDateTime from dbo.tb_ErpOrder where Ord_Class=1
  203. UNION
  204. --拍照时间
  205. select Ordpg_Number as ordNumber,max(Ordpg_PhotographyTime) as RDateTime from dbo.tb_ErpOrdersPhotography where Ordpg_PhotographyTime>0 group by Ordpg_Number
  206. UNION
  207. --初修时间
  208. select Ordv_Number as ordNumber,max(Ordv_EarlyRepairTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_EarlyRepairTime>0 group by Ordv_Number
  209. UNION
  210. --精修时间
  211. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  212. UNION
  213. --设计修时间
  214. select Ordv_Number as ordNumber,max(Ordv_DesignerTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_DesignerTime>0 group by Ordv_Number
  215. UNION
  216. --选片时间
  217. select Ordv_Number as ordNumber,max(Ordv_FilmSelectionTime) as RDateTime from dbo.tb_ErpOrderDigital where Ordv_FilmSelectionTime>0 group by Ordv_Number
  218. UNION
  219. --取件时间
  220. select OPlist_OrderNumber as ordNumber, max(OPlist_PickupTime) as RDateTime from [tb_ErpOrderProductList] where OPlist_Type =2 and OPlist_PickupTime>0 group by OPlist_OrderNumber
  221. UNION
  222. --收款时间
  223. select Pay_OrdNumber as ordNumber,Pay_CreateDatetime as RDateTime from [dbo].[tb_ErpPayment] where Pay_Type =0
  224. )
  225. ,orderList as
  226. (
  227. select ordNumber from ord where RDateTime>='''+CONVERT(nvarchar(10),@datatimeStar)+''' and RDateTime<'''+CONVERT(nvarchar(10),@datatimeEnd)+''' group by ordNumber
  228. )
  229. --门市
  230. select Sfs_OrderNumber,'''' as Sfs_OrderNumberDeputy,
  231. (sum(Sfrs_Scores)/count(1)) as sumScores
  232. ,max(Sfss_MarkID) as MarkID
  233. from tb_ErpSatisfactionSurvey
  234. left join tb_ErpSatisfactionRatingSet on Sfs_RatingSetID=tb_ErpSatisfactionRatingSet.ID
  235. left join tb_ErpSatisfactionSurveySet on Sfs_SurveySetID=tb_ErpSatisfactionSurveySet.id
  236. where Sfs_Type=0 and Sfs_OrderNumber in (select ordNumber from orderList)
  237. group by Sfs_OrderNumber
  238. union
  239. --其它流程
  240. select max(Sfs_OrderNumber) as Sfs_OrderNumber,Sfs_OrderNumberDeputy,
  241. (sum(Sfrs_Scores)/count(1)) as sumScores
  242. ,max(Sfss_MarkID) as MarkID
  243. from tb_ErpSatisfactionSurvey
  244. left join tb_ErpSatisfactionRatingSet on Sfs_RatingSetID=tb_ErpSatisfactionRatingSet.ID
  245. left join tb_ErpSatisfactionSurveySet on Sfs_SurveySetID=tb_ErpSatisfactionSurveySet.id
  246. where (len(Sfs_OrderNumberDeputy)>0 and Sfs_Type>0 and Sfs_Type<7) and Sfs_OrderNumber in (select ordNumber from orderList)
  247. group by Sfs_OrderNumberDeputy'
  248. exec (@OrdersPerson)
  249. END
  250. GO