| 1 | = Полезные запросы = |
| 2 | {{{ |
| 3 | #!access |
| 4 | #allow(TRAC_ADMIN) |
| 5 | * [#point1 Видалення товарів (без руху)] |
| 6 | * [#point2 Не вірні залишки по аналітичним карткам] |
| 7 | * [#point4 Скрипт очистки БД] |
| 8 | * [#point5 Скрипт очистки ЦБД] |
| 9 | * [#point6 Перевірка дублікатів записів у Ревізії] |
| 10 | |
| 11 | == [=#point1]Видалення товарів (без руху) == |
| 12 | Товаром без движения будем вважати той, на який не заведено жодної аналітичної картки (приходу). Наступна послідовність запитів, дозволить позбутись таких товарів: |
| 13 | {{{ |
| 14 | #!text/x-sql |
| 15 | delete from t_rests n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 16 | delete from t_nomen_bars n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 17 | delete from t_price_logs n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 18 | delete from t_markup_records n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 19 | delete from t_ao_records n where n.aor_nomen_id not in (select distinct nomen_id from t_goods g ) |
| 20 | delete from t_rev_records n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 21 | delete from t_nomens n where n.nomen_id not in (select distinct nomen_id from t_goods g ) |
| 22 | delete from t_barcodes b where b.barcode_id not in (select distinct barcode_id from t_nomen_bars) |
| 23 | }}} |
| 24 | ну або скриптом: |
| 25 | {{{ |
| 26 | #!text/x-sql |
| 27 | delete from t_rests n where n.nomen_id not in (select distinct nomen_id from t_goods g ) ; |
| 28 | commit work; |
| 29 | delete from t_nomen_bars n where n.nomen_id not in (select distinct nomen_id from t_goods g ); |
| 30 | commit work; |
| 31 | delete from t_price_logs n where n.nomen_id not in (select distinct nomen_id from t_goods g ); |
| 32 | commit work; |
| 33 | delete from t_markup_records n where n.nomen_id not in (select distinct nomen_id from t_goods g ); |
| 34 | commit work; |
| 35 | delete from t_ao_records n where n.aor_nomen_id not in (select distinct nomen_id from t_goods g ); |
| 36 | commit work; |
| 37 | delete from t_rev_records n where n.nomen_id not in (select distinct nomen_id from t_goods g ); |
| 38 | commit work; |
| 39 | delete from t_nomens n where n.nomen_id not in (select distinct nomen_id from t_goods g ); |
| 40 | commit work; |
| 41 | delete from t_barcodes b where b.barcode_id not in (select distinct barcode_id from t_nomen_bars); |
| 42 | commit work; |
| 43 | }}} |
| 44 | |
| 45 | == [=#point2]Не вірні залишки по аналітичним карткам == |
| 46 | При нормальних залишках в БД різниця всіх приходів та розходів по кожній аналітичній картці в точності співпадає із залишком по кожній з аналитичних карток. Якщо останній стовпчик в виборці не рівний нулю, то має місце проблема з залишками по цій аналітичній картці. |
| 47 | {{{ |
| 48 | #!text/x-sql |
| 49 | select aa.ogoods_id, |
| 50 | sum(aa.orest) as orest, |
| 51 | sum(aa.oprihod) as oprihod, |
| 52 | sum(aa.orashod) as orashod, |
| 53 | sum(aa.oprihod) - sum(aa.orashod) - sum(aa.orest) as orizn from |
| 54 | ( |
| 55 | select g.goods_id as ogoods_id, |
| 56 | g.goods_rest as orest, |
| 57 | 0.0 as oprihod, |
| 58 | 0.0 as orashod |
| 59 | from t_goods g |
| 60 | union all |
| 61 | select dg.goods_id as ogoods_id, |
| 62 | 0.0 as orest, |
| 63 | 0.0 as oprihod, |
| 64 | dg.kilk as orashod |
| 65 | from t_documents d left join t_doc_records dr on (d.document_id = dr.document_id and d.objects_id = 1) |
| 66 | left join t_doc_goods dg on (dr.docrec_id = dg.docrec_id) |
| 67 | union all |
| 68 | select dg.goods_id as ogoods_id, |
| 69 | 0.0 as orest, |
| 70 | dg.kilk as oprihod, |
| 71 | 0.0 as orashod |
| 72 | from t_documents d left join t_doc_records dr on (d.document_id = dr.document_id and d.clients_id = 1) |
| 73 | left join t_doc_goods dg on (dr.docrec_id = dg.docrec_id) |
| 74 | |
| 75 | ) aa |
| 76 | group by aa.ogoods_id |
| 77 | }}} |
| 78 | |
| 79 | Аналогічне справедливо і для залишку по товару та записів в документах (без врахування аналітичних карток): |
| 80 | {{{ |
| 81 | #!text/x-sql |
| 82 | select aa.onomen_id, |
| 83 | sum(aa.orest) as orest, |
| 84 | sum(aa.oprihod) as oprihod, |
| 85 | sum(aa.orashod) as orashod, |
| 86 | sum(aa.oprihod) - sum(aa.orashod) - sum(aa.orest) as orizn from |
| 87 | ( |
| 88 | select r.nomen_id as onomen_id, |
| 89 | r.rest as orest, |
| 90 | 0.0 as oprihod, |
| 91 | 0.0 as orashod |
| 92 | from t_rests r |
| 93 | union all |
| 94 | select dr.nomen_id as onomen_id, |
| 95 | 0.0 as orest, |
| 96 | 0.0 as oprihod, |
| 97 | dr.kilk as orashod |
| 98 | from t_documents d left join t_doc_records dr on (d.document_id = dr.document_id and d.objects_id = 1) |
| 99 | union all |
| 100 | select dr.nomen_id as onomen_id, |
| 101 | 0.0 as orest, |
| 102 | dr.kilk as oprihod, |
| 103 | 0.0 as orashod |
| 104 | from t_documents d left join t_doc_records dr on (d.document_id = dr.document_id and d.clients_id = 1) |
| 105 | |
| 106 | ) aa |
| 107 | group by aa.onomen_id |
| 108 | }}} |
| 109 | |
| 110 | Іще один варіант, при якому сумарна кількість по t_goods не відповідає залишку в t_rests. Після виконання наступного запиту, різниця в останньому стовпчику має бути нульовою: |
| 111 | {{{ |
| 112 | #!text/x-sql |
| 113 | select nomen_id, sum(orst_rest), sum(ogoods_rest), sum(orst_rest)-sum(ogoods_rest) from |
| 114 | ( |
| 115 | select r.nomen_id, r.rest as orst_rest, 0 as ogoods_rest from t_rests r |
| 116 | |
| 117 | union all |
| 118 | |
| 119 | select g.nomen_id, 0 as orst_res, sum(g.goods_rest) as ogoods_rest from t_goods g |
| 120 | group by g.nomen_id |
| 121 | ) |
| 122 | group by nomen_id |
| 123 | }}} |
| 124 | |
| 125 | == [=#point4]Скрипт очистки БД == |
| 126 | Загальний скрипт очищення бази (не зачіпає користувачів, клієнтів, дисконтників та товари) виглядає так: |
| 127 | {{{ |
| 128 | #!text/x-sql |
| 129 | SET SQL DIALECT 3; |
| 130 | |
| 131 | delete from t_document_autoorder; |
| 132 | delete from t_ao_records; |
| 133 | alter sequence gen_ao_record_id restart with 0; |
| 134 | delete from t_autoorders; |
| 135 | alter sequence gen_autoorder_id restart with 0; |
| 136 | commit work; |
| 137 | |
| 138 | delete from t_rev_records; |
| 139 | alter sequence gen_rev_records_id restart with 0; |
| 140 | delete from t_revisions; |
| 141 | alter sequence gen_revision_id restart with 0; |
| 142 | commit work; |
| 143 | |
| 144 | alter trigger docgoods_ad0 inactive; |
| 145 | delete from t_doc_goods; |
| 146 | alter trigger docgoods_ad0 active; |
| 147 | alter sequence gen_docgoods_id restart with 0; |
| 148 | commit work; |
| 149 | |
| 150 | alter trigger docrec_bd0 inactive; |
| 151 | alter trigger docrec_ad4 inactive; |
| 152 | alter trigger t_record_exts_ad0 inactive; |
| 153 | delete from t_record_exts; |
| 154 | alter trigger t_record_exts_ad0 active; |
| 155 | delete from t_doc_records; |
| 156 | alter trigger docrec_ad4 active; |
| 157 | alter trigger docrec_bd0 active; |
| 158 | alter sequence gen_docrec_id restart with 0; |
| 159 | commit work; |
| 160 | |
| 161 | |
| 162 | alter trigger GOODS_AD0 inactive; |
| 163 | alter trigger T_GOODS_AD0 inactive; |
| 164 | delete from t_goods; |
| 165 | alter sequence gen_goods_id restart with 0; |
| 166 | alter trigger GOODS_AD0 active; |
| 167 | alter trigger T_GOODS_AD0 active; |
| 168 | commit work; |
| 169 | update t_rests set rest = 0.0, rest_sum = 0.0; |
| 170 | commit work; |
| 171 | |
| 172 | delete from t_tax_correctings; |
| 173 | alter sequence gen_t_tax_correctings_id restart with 0; |
| 174 | delete from t_taxes; |
| 175 | alter sequence reestr_gen_id restart with 0; |
| 176 | commit work; |
| 177 | |
| 178 | alter trigger t_pays_ad1 inactive; |
| 179 | alter trigger t_pays_ad0 inactive; |
| 180 | delete from t_pays; |
| 181 | alter trigger t_pays_ad1 active; |
| 182 | alter trigger t_pays_ad0 active; |
| 183 | alter sequence gen_t_pays_id restart with 0; |
| 184 | commit work; |
| 185 | |
| 186 | update t_documents set prew_document_id = null where prew_document_id is not null; |
| 187 | alter trigger documents_bd0 inactive; |
| 188 | alter trigger documents_bd2 inactive; |
| 189 | alter trigger documents_bd3 inactive; |
| 190 | alter trigger documents_ad4 inactive; |
| 191 | delete from t_documents; |
| 192 | alter trigger documents_bd0 active; |
| 193 | alter trigger documents_bd2 active; |
| 194 | alter trigger documents_bd3 active; |
| 195 | alter trigger documents_ad4 active; |
| 196 | alter sequence gen_documents_id restart with 0; |
| 197 | commit work; |
| 198 | |
| 199 | delete from t_price_logs; |
| 200 | delete from t_del_docrecs; |
| 201 | delete from t_del_documents; |
| 202 | delete from docrec_del; |
| 203 | delete from docrec_log; |
| 204 | delete from docrec_tmp; |
| 205 | delete from doc_del; |
| 206 | delete from t_doc_records_log; |
| 207 | alter sequence gen_t_doc_records_log_id restart with 0; |
| 208 | commit work; |
| 209 | |
| 210 | delete from t_markup_records; |
| 211 | delete from t_markups; |
| 212 | commit work; |
| 213 | |
| 214 | delete from t_delete_logs; |
| 215 | commit work; |
| 216 | |
| 217 | delete from t_io_logs; |
| 218 | delete from t_z_reports; |
| 219 | delete from t_nomens_log; |
| 220 | commit work; |
| 221 | |
| 222 | delete from t_abc_lists; |
| 223 | delete from t_logs; |
| 224 | delete from t_nomen_bars_log; |
| 225 | commit work; |
| 226 | |
| 227 | update t_objects set uuid = gen_uuid(); |
| 228 | commit work; |
| 229 | }}} |
| 230 | Очистити товари можна, виконавши наступний блок: |
| 231 | {{{ |
| 232 | #!text/x-sql |
| 233 | execute block |
| 234 | as |
| 235 | declare variable TNOMEN_ID integer; |
| 236 | declare variable TCNT integer; |
| 237 | begin |
| 238 | for select n.nomen_id from t_nomens n |
| 239 | -- тут, за необхідності, може бути довыльна умова |
| 240 | into tnomen_id |
| 241 | do begin |
| 242 | select count(ocount) from PS_NOMEN_DEL(:tnomen_id) into :tcnt; |
| 243 | |
| 244 | delete from t_nomens_log where nomen_id = :tnomen_id; |
| 245 | delete from t_nomen_bars_log where nomen_id = :tnomen_id; |
| 246 | end |
| 247 | end |
| 248 | }}} |
| 249 | якщо в цей блок вставити певну умову (наприклад шаблон імен, чи приналежності до акцизу) то можна видаляти товар вибірково. |
| 250 | |
| 251 | == [=#point5]Скрипт очистки ЦБД == |
| 252 | Повне очищення центральної БД: |
| 253 | {{{ |
| 254 | #!text/x-sql |
| 255 | SET SQL DIALECT 3; |
| 256 | |
| 257 | SET NAMES WIN1251; |
| 258 | |
| 259 | delete from t_abc_lists; |
| 260 | commit work; |
| 261 | |
| 262 | delete from t_ao_records; |
| 263 | commit work; |
| 264 | alter sequence gen_t_ao_records_id restart with 0; |
| 265 | commit work; |
| 266 | delete from t_autoorders; |
| 267 | commit work; |
| 268 | alter sequence gen_t_autoorders_id restart with 0; |
| 269 | commit work; |
| 270 | |
| 271 | delete from t_goods; |
| 272 | commit work; |
| 273 | delete from t_rests; |
| 274 | commit work; |
| 275 | |
| 276 | delete from t_doc_record_bonuses; |
| 277 | delete from t_doc_records; |
| 278 | delete from t_pays; |
| 279 | delete from t_documents; |
| 280 | commit work; |
| 281 | |
| 282 | delete from t_kards_ralase; |
| 283 | delete from t_kards; |
| 284 | delete from t_kard_types where kard_type_id > 0; |
| 285 | commit work; |
| 286 | |
| 287 | delete from t_nomen_links; |
| 288 | commit work; |
| 289 | delete from t_nomen_bars; |
| 290 | commit work; |
| 291 | delete from t_nomen_bars_log; |
| 292 | commit work; |
| 293 | delete from t_recipe_records; |
| 294 | commit work; |
| 295 | delete from t_recipes; |
| 296 | commit work; |
| 297 | delete from t_nomens; |
| 298 | commit work; |
| 299 | delete from t_makers where maker_id > 0; |
| 300 | commit work; |
| 301 | delete from t_indicatives where indicative_id > 0; |
| 302 | commit work; |
| 303 | delete from t_price_logs; |
| 304 | commit work; |
| 305 | |
| 306 | delete from T_ACCOUNTS; |
| 307 | alter sequence gen_clientaccount_id restart with 0; |
| 308 | commit work; |
| 309 | delete from t_contacts; |
| 310 | alter sequence gen_t_contacts_id restart with 0; |
| 311 | commit work; |
| 312 | delete from t_client_links; |
| 313 | commit work; |
| 314 | delete from t_objects where objects_id > 0; |
| 315 | -- T_CLIENTS_BD0 not active |
| 316 | delete from t_clients where clients_id > 100 and clients_id < 1000000; |
| 317 | -- T_CLIENTS_BD0 active |
| 318 | commit work; |
| 319 | |
| 320 | -- T_USERS_BD0 off |
| 321 | delete from t_users where user_id > 2; |
| 322 | -- T_USERS_BD0 on |
| 323 | commit work; |
| 324 | |
| 325 | update t_objects set uuid = GEN_UUID() where objects_id = 1000000; |
| 326 | |
| 327 | delete from t_delete_logs; |
| 328 | commit work; |
| 329 | }}} |
| 330 | |
| 331 | == [=#point6]Перевірка дублікатів записів у Ревізії == |
| 332 | |
| 333 | {{{ |
| 334 | select rr.nomen_id, count(1) from t_revisions r join t_rev_records rr on r.revision_id = rr.revision_id |
| 335 | where r.revision_date = current_date - 1 |
| 336 | group by rr.nomen_id |
| 337 | }}} |
| 338 | |
| 339 | }}} |