t10.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. select contractid ,count(*)c from part_repertory group by contractid
  2. having count(*)>1
  3. select contractid ,count(*)c from part_repertory group by partCode,locationId,contractId,providerId,price
  4. having count(*)>1
  5. show create table part_repertory
  6. drop table part_repertory2
  7. CREATE TABLE `part_repertory3` (
  8. `id` int(11) NOT NULL COMMENT '主键',
  9. `pastureId` int(11) NOT NULL COMMENT '牧场主键',
  10. `partId` int(11) DEFAULT NULL COMMENT '设备主键',
  11. `partName` varchar(100) DEFAULT NULL,
  12. `partCode` varchar(100) DEFAULT NULL,
  13. `specification` varchar(100) DEFAULT NULL COMMENT '规格',
  14. `location` varchar(100) DEFAULT NULL COMMENT '位置',
  15. `locationId` int(11) NOT NULL,
  16. `reportery` decimal(10,2) DEFAULT NULL COMMENT '库存',
  17. `minRepertory` decimal(11,0) NOT NULL DEFAULT 0 COMMENT '最低库存',
  18. `maxRepertory` decimal(11,0) NOT NULL DEFAULT 10 COMMENT '最高库存',
  19. `contractId` bigint(50) NOT NULL,
  20. `providerId` int(11) DEFAULT NULL,
  21. `providerNames` varchar(100) DEFAULT NULL,
  22. `brand` varchar(100) DEFAULT NULL COMMENT '品牌',
  23. `brandId` int(11) NOT NULL,
  24. `unit` varchar(20) DEFAULT NULL COMMENT '单位',
  25. `price` decimal(15,5) NOT NULL COMMENT '单价',
  26. `enable` tinyint(4) DEFAULT NULL,
  27. `precontractId` varchar(50) DEFAULT NULL,
  28. PRIMARY KEY (`id`),
  29. index `pastureId` (`partCode`,`precontractId`,`locationId`),
  30. index `contractId` (`contractId`),
  31. index `partcode` (`partCode`,`locationId`,`contractId`,`providerId`,`price`)
  32. ) ENGINE=InnoDB AUTO_INCREMENT=587461 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
  33. describe buydetail
  34. alter table buydetail modify column contractId bigint(50) ;
  35. describe checkpart
  36. alter table checkpart modify column contractId bigint(50) ;
  37. show index from part_repertory
  38. insert into part_repertory2
  39. alter table part_repertory add unique index (partCode,
  40. locationId,
  41. contractId,
  42. providerId,
  43. price)
  44. describe part_repertory
  45. select pr.partCode,
  46. pr.locationId,
  47. pr.contractId,
  48. pr.providerId,
  49. pr.price,
  50. pr.reportery from part_repertory pr
  51. join (
  52. select partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price
  53. having count(*)>1
  54. )t on
  55. pr.partCode= t.partCode and
  56. pr.locationId= t.locationId and
  57. pr.contractId= t.contractId and
  58. pr.providerId= t.providerId and
  59. pr.price= t.price
  60. order by pr.partCode,
  61. pr.locationId,
  62. pr.contractId,
  63. pr.providerId,
  64. pr.price
  65. insert into part_repertory2
  66. (
  67. select pr.* from part_repertory pr
  68. join (
  69. select partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price
  70. having count(*)>1
  71. )t on
  72. pr.partCode= t.partCode and
  73. pr.locationId= t.locationId and
  74. pr.contractId= t.contractId and
  75. pr.providerId= t.providerId and
  76. pr.price= t.price
  77. order by pr.partCode,
  78. pr.locationId,
  79. pr.contractId,
  80. pr.providerId,
  81. pr.price
  82. )
  83. describe contract
  84. describe part_repertory
  85. select pr.id, pr.partcode ,pr.contractId ,c.id ,c.partcode,c2.id,c2.partcode
  86. from part_repertory pr
  87. left join contract c on c.id =pr.contractId
  88. left join contract c2 on c2.partcode = pr.partcode
  89. join bigcontract bc on bc.id =c2.bigid
  90. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  91. and c.partcode <>c2.partcode
  92. select count(*)
  93. from part_repertory pr
  94. left join contract c on c.id =pr.contractId
  95. left join contract c2 on c2.partcode = pr.partcode
  96. join bigcontract bc on bc.id =c2.bigid
  97. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  98. and c.partcode <>c2.partcode
  99. delete from part_repertory3
  100. insert into part_repertory3
  101. (
  102. select distinct pr.*
  103. from part_repertory pr
  104. left join contract c on c.id =pr.contractId
  105. left join contract c2 on c2.partcode = pr.partcode
  106. join bigcontract bc on bc.id =c2.bigid
  107. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  108. and c.partcode <>c2.partcode
  109. )
  110. select t1.* from
  111. (
  112. select pr.id, pr.partcode ,pr.contractId ,c.id cid ,c.partcode ,c2.id c2id,c2.partcode
  113. from part_repertory pr
  114. left join contract c on c.id =pr.contractId
  115. left join contract c2 on c2.partcode = pr.partcode
  116. join bigcontract bc on bc.id =c2.bigid
  117. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  118. and c.partcode <>c2.partcode
  119. )t1 join
  120. (
  121. select pr.id
  122. from part_repertory pr
  123. left join contract c on c.id =pr.contractId
  124. left join contract c2 on c2.partcode = pr.partcode
  125. join bigcontract bc on bc.id =c2.bigid
  126. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  127. and c.partcode <>c2.partcode
  128. group by pr.id
  129. having count(*) >1
  130. )t2 on t1.id =t2.id
  131. update
  132. part_repertory pr
  133. left join contract c on c.id =pr.contractId
  134. left join contract c2 on c2.partcode = pr.partcode
  135. join bigcontract bc on bc.id =c2.bigid
  136. set pr.contractId = c2.id
  137. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  138. and c.partcode <>c2.partcode
  139. select contractid ,count(*)c from part_repertory group by contractid having count(*)>1
  140. show index from part_repertory
  141. alter table part_repertory add index(contractId)
  142. alter table part_repertory drop index contractId
  143. show create table part_repertory
  144. alter table part_repertory add UNIQUE index (`partCode`,`locationId`,`contractId`,`providerId`,`price`)
  145. select group_concat(id,',') from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  146. select * from part_repertory
  147. where instr ( (
  148. select group_concat(id,',') from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  149. ),id )
  150. select * from part_repertory where instr('123',id )
  151. delete pr from part_repertory pr
  152. left join contract c on c.id =pr.contractId
  153. left join contract c2 on c2.partcode = pr.partcode
  154. join bigcontract bc on bc.id =c2.bigid
  155. where pr.providerId = bc.providerId and pr.brandid =c2.brandid and pr.price =c2.price
  156. and c.partcode <>c2.partcode
  157. and pr.reportery <0
  158. alter table part_repertory add UNIQUE index (`partCode`,`locationId`,`contractId`,`providerId`,`price`)
  159. select pr.* from part_repertory pr
  160. join
  161. (select group_concat(id,',') gid ,partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  162. )t on
  163. pr.partCode= t.partCode and
  164. pr.locationId= t.locationId and
  165. pr.contractId= t.contractId and
  166. pr.providerId= t.providerId and
  167. pr.price= t.price and
  168. instr (t.gid,pr,pr.id) >1
  169. CREATE TABLE `part_repertory5` (
  170. `id` int(11) NOT NULL COMMENT '主键',
  171. `pastureId` int(11) NOT NULL COMMENT '牧场主键',
  172. `partId` int(11) DEFAULT NULL COMMENT '设备主键',
  173. `partName` varchar(100) DEFAULT NULL,
  174. `partCode` varchar(100) DEFAULT NULL,
  175. `specification` varchar(100) DEFAULT NULL COMMENT '规格',
  176. `location` varchar(100) DEFAULT NULL COMMENT '位置',
  177. `locationId` int(11) NOT NULL,
  178. `reportery` decimal(10,2) DEFAULT NULL COMMENT '库存',
  179. `minRepertory` decimal(11,0) NOT NULL DEFAULT 0 COMMENT '最低库存',
  180. `maxRepertory` decimal(11,0) NOT NULL DEFAULT 10 COMMENT '最高库存',
  181. `contractId` bigint(50) NOT NULL,
  182. `providerId` int(11) DEFAULT NULL,
  183. `providerNames` varchar(100) DEFAULT NULL,
  184. `brand` varchar(100) DEFAULT NULL COMMENT '品牌',
  185. `brandId` int(11) NOT NULL,
  186. `unit` varchar(20) DEFAULT NULL COMMENT '单位',
  187. `price` decimal(15,5) NOT NULL COMMENT '单价',
  188. `enable` tinyint(4) DEFAULT NULL,
  189. `precontractId` varchar(50) DEFAULT NULL,
  190. PRIMARY KEY (`id`),
  191. index `pastureId` (`partCode`,`precontractId`,`locationId`),
  192. index `contractId` (`contractId`),
  193. index `partcode` (`partCode`,`locationId`,`contractId`,`providerId`,`price`)
  194. ) ENGINE=InnoDB AUTO_INCREMENT=587461 DEFAULT CHARSET=utf8 ROW_FORMAT=dynamic
  195. insert into part_repertory4 (
  196. select * from part_repertory where reportery <0
  197. )
  198. delete from part_repertory where reportery <0
  199. select * from part_repertory4
  200. insert into part_repertory4
  201. select pr.*
  202. from part_repertory pr
  203. join
  204. (select partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  205. )t on
  206. pr.partCode= t.partCode and
  207. pr.locationId= t.locationId and
  208. pr.contractId= t.contractId and
  209. pr.providerId= t.providerId and
  210. pr.price= t.price
  211. select t2.ranking,pr.* from part_repertory pr join
  212. (
  213. select pr.id ,
  214. rank() over (partition by pr.partCode,
  215. pr.locationId,
  216. pr.contractId,
  217. pr.providerId,
  218. pr.price order by id) as Ranking
  219. from part_repertory pr
  220. join
  221. (select partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  222. )t on
  223. pr.partCode= t.partCode and
  224. pr.locationId= t.locationId and
  225. pr.contractId= t.contractId and
  226. pr.providerId= t.providerId and
  227. pr.price= t.price
  228. )t2 on pr.id =t2.id
  229. where t2.Ranking >1
  230. delete pr from part_repertory pr join
  231. (
  232. select pr.id ,
  233. rank() over (partition by pr.partCode,
  234. pr.locationId,
  235. pr.contractId,
  236. pr.providerId,
  237. pr.price order by id) as Ranking
  238. from part_repertory pr
  239. join
  240. (select partCode,locationId,contractId,providerId,price from part_repertory group by partCode,locationId,contractId,providerId,price having count(*)>1
  241. )t on
  242. pr.partCode= t.partCode and
  243. pr.locationId= t.locationId and
  244. pr.contractId= t.contractId and
  245. pr.providerId= t.providerId and
  246. pr.price= t.price
  247. )t2 on pr.id =t2.id
  248. where t2.Ranking >1
  249. alter table part_repertory add UNIQUE index (`partCode`,`locationId`,`contractId`,`providerId`,`price`)
  250. show index from part_repertory
  251. alter table part_repertory drop index partcode
  252. update part_repertory pr join (
  253. select sum(reportery)c,partCode,locationId,contractId,providerId,price from part_repertory4
  254. where reportery >0
  255. group by partCode,locationId,contractId,providerId,price
  256. )t on
  257. pr.partCode= t.partCode and
  258. pr.locationId= t.locationId and
  259. pr.contractId= t.contractId and
  260. pr.providerId= t.providerId and
  261. pr.price= t.price
  262. set pr.reportery =pr.reportery+t.c
  263. select rank() over (partition by
  264. partCode,
  265. locationId,
  266. contractId,
  267. providerId,
  268. price order by id) as Ranking,partCode,locationId,contractId,providerId,price ,group_concat(id,',')
  269. from part_repertory4
  270. where reportery >0
  271. select * from part_repertory4 where reportery >0
  272. select * from part_repertory pr join (
  273. select t.* from (
  274. select
  275. rank() over (partition by
  276. partCode,
  277. locationId,
  278. contractId,
  279. providerId,
  280. price order by id) as Ranking,p.*
  281. from part_repertory4 p
  282. where reportery >0
  283. )t where t.ranking =2
  284. )t2 on
  285. pr.partCode= t2.partCode and
  286. pr.locationId= t2.locationId and
  287. pr.contractId= t2.contractId and
  288. pr.providerId= t2.providerId and
  289. pr.price= t2.price
  290. update part_repertory pr join (
  291. select t.* from (
  292. select
  293. rank() over (partition by
  294. partCode,
  295. locationId,
  296. contractId,
  297. providerId,
  298. price order by id) as Ranking,p.*
  299. from part_repertory4 p
  300. where reportery >0
  301. )t where t.ranking =2
  302. )t2 on
  303. pr.partCode= t2.partCode and
  304. pr.locationId= t2.locationId and
  305. pr.contractId= t2.contractId and
  306. pr.providerId= t2.providerId and
  307. pr.price= t2.price
  308. set pr.reportery =pr.reportery +t2.reportery