From 79faeed8b31a91a01ba05302654cae7f6200dc9b Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 31 Jan 2023 14:59:57 +0100 Subject: [PATCH] test --- .grafana-find.json | 5 - main.js | 87 +- node_modules/.package-lock.json | 176 +- package-lock.json | 329 +- package.json | 2 + test.txt | 11663 ++++++++++++++++++++++++++++++ 6 files changed, 12235 insertions(+), 27 deletions(-) delete mode 100644 .grafana-find.json create mode 100644 test.txt diff --git a/.grafana-find.json b/.grafana-find.json deleted file mode 100644 index 620f5c1..0000000 --- a/.grafana-find.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "grafanaUrl": "https://grafana.example.com", - "user": "user", - "password": "1234" -} \ No newline at end of file diff --git a/main.js b/main.js index a2467bb..84c2f0b 100644 --- a/main.js +++ b/main.js @@ -6,6 +6,14 @@ const colors = require('colors'); const os = require('os'); const fs = require('fs'); const getopts = require('getopts'); +const mysql = require('mysql2'); + +const con = mysql.createConnection({ + host: "localhost", + user: "root", + password: "root", + port : 3306 +}); const error = `[ERROR]: `.bold; @@ -154,7 +162,7 @@ async function main(){ console.clear(); console.log( - `───────── Grafana-Find (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold + `───────── Grafana-Find LOCAL (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold ); console.log(colors.green.bold(`──────────────────── Starting process ────────────────────\n`)); @@ -212,29 +220,69 @@ async function main(){ for (const panel of dashboard.panels) { if (panel.targets) for (const target of panel.targets) { - isFound = regexRawSQL.test(target.rawSql); - if (isFound) { - if (panel.title) - if (panel.title==' ') - titlePanels.push(`(null)`.italic); - else - titlePanels.push(panel.title); - else - titlePanels.push(`(undefined)`.italic); - numberOfPanels++; - isFoundSomething=true; - } + let sql = target.rawSql; + + sql = sql.replaceAll('$__timeFilter', 'util.mock'); + sql = sql.replaceAll('$__timeFrom()', 1); + sql = sql.replaceAll('$__timeTo()', 1); + sql = sql.replaceAll(/\$\w+/g, 1); + + // sql = sql.replace(/$__timeFilter/g, 'util.mock'); + // sql = sql.replace(/$__timeFrom()/g, 1); + // sql = sql.replace(/$__timeTo()/g, 1); + // sql = sql.replace(/\$\w+/g, 1); + + // console.log(target.rawSql + '\n'); + // console.log(sql + '\n'); + + // fs.appendFile("./test.txt", sql + '\n', function (err) { + // if (err) { + // return console.log(err); + // } + // }); + + con.query(sql, function (err, result) { + if (err) throw err; + console.log("Result: " + result); + }); + + // isFound = regexRawSQL.test(target.rawSql); + // if (isFound) { + // if (panel.title) + // if (panel.title==' ') + // titlePanels.push(`(null)`.italic); + // else + // titlePanels.push(panel.title); + // else + // titlePanels.push(`(undefined)`.italic); + // numberOfPanels++; + // isFoundSomething=true; + // } } } if (dashboard.templating) for (const list of dashboard.templating.list) { - isFound = regexRawSQL.test(list.query); - if (isFound) { - nameVariables.push(list.name) - numberOfVariables++; - isFoundSomething=true; - } + let sql = list.query; + sql = sql.replaceAll('$__timeFilter', 'util.mock'); + sql = sql.replaceAll('$__timeFrom()', 1); + sql = sql.replaceAll('$__timeTo()', 1); + sql = sql.replaceAll(/\$\w+/g, 1); + + // console.log(sql + '\n'); + + // fs.appendFile("./test.txt", sql + '\n', function (err) { + // if (err) { + // return console.log(err); + // } + // }); + + // isFound = regexRawSQL.test(list.query); + // if (isFound) { + // nameVariables.push(list.name) + // numberOfVariables++; + // isFoundSomething=true; + // } } } @@ -287,4 +335,5 @@ async function main(){ process.exit(); } + main(); \ No newline at end of file diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 0d64c17..485c4d4 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -1,9 +1,17 @@ { "name": "grafana-find", - "version": "1.0", + "version": "1.0.3", "lockfileVersion": 2, "requires": true, "packages": { + "node_modules/bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==", + "engines": { + "node": "*" + } + }, "node_modules/colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", @@ -12,11 +20,122 @@ "node": ">=0.1.90" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "dependencies": { + "is-property": "^1.0.2" + } + }, "node_modules/getopts": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==" }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==" + }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/long": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.1.tgz", + "integrity": "sha512-GKSNGeNAtw8IryjjkhZxuKB3JzlcLTwjtiQCHKvqQet81I93kXslhDQruGI/QsddO83mcDToBVy7GqGS/zYf/A==" + }, + "node_modules/lru-cache": { + "version": "7.14.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz", + "integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "dependencies": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mysql2": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.1.0.tgz", + "integrity": "sha512-woMeIog21X72RcFLhE/xajhUdnwRMd6Oq16S7/O2qJ5lPUDfFwgSW+xhx7TjzcM8PU3q+dVsryIaDi+HyQR/0A==", + "dependencies": { + "denque": "^2.1.0", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.3", + "long": "^5.2.1", + "lru-cache": "^7.14.1", + "named-placeholders": "^1.1.3", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/mysql2/node_modules/sqlstring": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz", + "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/named-placeholders": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz", + "integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==", + "dependencies": { + "lru-cache": "^7.14.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -44,11 +163,66 @@ "node": ">= 6" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "node_modules/readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==" + }, + "node_modules/sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/package-lock.json b/package-lock.json index 98031cd..da5f1e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,18 @@ { "name": "grafana-find", - "version": "1.0", + "version": "1.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "grafana-find", - "version": "1.0", + "version": "1.0.3", "license": "GPL-3.0", "dependencies": { "colors": "^1.4.0", "getopts": "^2.3.0", + "mysql": "^2.18.1", + "mysql2": "^3.1.0", "node-fetch": "^2.6.7", "object-hash": "^3.0.0" }, @@ -19,6 +21,14 @@ "grafana-find": "main.js" } }, + "node_modules/bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==", + "engines": { + "node": "*" + } + }, "node_modules/colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", @@ -27,11 +37,122 @@ "node": ">=0.1.90" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "dependencies": { + "is-property": "^1.0.2" + } + }, "node_modules/getopts": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==" }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==" + }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/long": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.1.tgz", + "integrity": "sha512-GKSNGeNAtw8IryjjkhZxuKB3JzlcLTwjtiQCHKvqQet81I93kXslhDQruGI/QsddO83mcDToBVy7GqGS/zYf/A==" + }, + "node_modules/lru-cache": { + "version": "7.14.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz", + "integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "dependencies": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mysql2": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.1.0.tgz", + "integrity": "sha512-woMeIog21X72RcFLhE/xajhUdnwRMd6Oq16S7/O2qJ5lPUDfFwgSW+xhx7TjzcM8PU3q+dVsryIaDi+HyQR/0A==", + "dependencies": { + "denque": "^2.1.0", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.3", + "long": "^5.2.1", + "lru-cache": "^7.14.1", + "named-placeholders": "^1.1.3", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/mysql2/node_modules/sqlstring": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz", + "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/named-placeholders": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz", + "integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==", + "dependencies": { + "lru-cache": "^7.14.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -59,11 +180,66 @@ "node": ">= 6" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "node_modules/readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==" + }, + "node_modules/sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", @@ -80,16 +256,113 @@ } }, "dependencies": { + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, "colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" }, + "core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==" + }, + "generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "requires": { + "is-property": "^1.0.2" + } + }, "getopts": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==" }, + "iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "long": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.1.tgz", + "integrity": "sha512-GKSNGeNAtw8IryjjkhZxuKB3JzlcLTwjtiQCHKvqQet81I93kXslhDQruGI/QsddO83mcDToBVy7GqGS/zYf/A==" + }, + "lru-cache": { + "version": "7.14.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz", + "integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==" + }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "requires": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + } + }, + "mysql2": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.1.0.tgz", + "integrity": "sha512-woMeIog21X72RcFLhE/xajhUdnwRMd6Oq16S7/O2qJ5lPUDfFwgSW+xhx7TjzcM8PU3q+dVsryIaDi+HyQR/0A==", + "requires": { + "denque": "^2.1.0", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.3", + "long": "^5.2.1", + "lru-cache": "^7.14.1", + "named-placeholders": "^1.1.3", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "dependencies": { + "sqlstring": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz", + "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==" + } + } + }, + "named-placeholders": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz", + "integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==", + "requires": { + "lru-cache": "^7.14.1" + } + }, "node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -103,11 +376,63 @@ "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==" }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==" + }, + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, "tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/package.json b/package.json index 3bce800..c84f88e 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "dependencies": { "colors": "^1.4.0", "getopts": "^2.3.0", + "mysql": "^2.18.1", + "mysql2": "^3.1.0", "node-fetch": "^2.6.7", "object-hash": "^3.0.0" }, diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..7614fd4 --- /dev/null +++ b/test.txt @@ -0,0 +1,11663 @@ +SELECT dated time, SUM(amount) + FROM bs.salesPersonEvolution + WHERE 1(dated) + GROUP BY dated + +SELECT s.dated time, sum(s.amount) CLAUDI + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO CLAUDI' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) MIRGIR + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO MIRIAM MAR' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) JVALLES + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO J VALLES' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) JBROCAL + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO J BROCAL' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) ELENA + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO ELENA BASCUNANA' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) CZAMBRANO + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO C ZAMBRANO' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO CLAUDI' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO CLAUDI' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO C LOPEZ' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO J BROCAL' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO J BROCAL' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO J BROCAL' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO J VALLES' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO J VALLES' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO ELENA BASCUNANA' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO C ZAMBRANO' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO CLAUDI' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO CLAUDI' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO CLAUDI' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO CLAUDI' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO MIRIAM MAR' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO J BROCAL' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO J BROCAL' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO J BROCAL' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='EQUIPO J VALLES' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO J VALLES' + GROUP BY t.week + ORDER BY s.dated +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT s.dated + INTERVAL 3 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='EQUIPO J BROCAL' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='EQUIPO J VALLES' + GROUP BY t.week + ORDER BY s.dated +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT s.dated time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='EQUIPO J VALLES' + GROUP BY t.week + ORDER BY s.dated +SELECT SUM(sub2.year1) '2020',SUM(sub2.year2) '2021',SUM(sub2.year3) '2022',SUM(sub2.year4) '2023' + FROM(SELECT + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2022 THEN sub.amount + ELSE 0 + END year3, + CASE + WHEN sub.years=2023 THEN sub.amount + ELSE 0 + END year4 + FROM(SELECT sum(amount) amount, year(dated) years + FROM bs.salesByclientSalesPerson + WHERE 1(dated) + UNION ALL + SELECT sum(amount), year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(amount) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(amount) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(sub2.year1) '2019', SUM(sub2.year2) '2020', SUM(sub2.year3) '2021', SUM(sub2.year4) '2022' + FROM(SELECT + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3, + CASE + WHEN sub.years=2022 THEN sub.amount + ELSE 0 + END year4 + FROM(SELECT SUM(amountNewBorn) amount, year(dated) years + FROM bs.salesByclientSalesPerson + WHERE 1(dated) + UNION ALL + SELECT sum(amountNewBorn), year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(amountNewBorn) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(amountNewBorn) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(s.amount), d.name EQUIPO + FROM bs.salesByclientSalesPerson s + JOIN vn.worker w ON w.id = s.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + AND s.dated BETWEEN 1() AND 1() +GROUP BY d.name +ORDER BY SUM(s.amount) DESC +SELECT SUM(s.amountNewBorn), d.name EQUIPO + FROM bs.salesByclientSalesPerson s + JOIN vn.worker w ON w.id = s.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + AND s.dated BETWEEN 1() AND 1() +GROUP BY d.name +ORDER BY SUM(s.amountNewBorn) DESC +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT Fecha time, + (SUM(Greuge)/SUM(Ventas))*100 'Ratio ventas/greuge' + FROM bi.Greuge_Evolution g + JOIN vn.client c ON c.id = g.Id_Cliente + JOIN vn.worker w ON w.id = c.salesPersonFk + GROUP BY Fecha +SELECT Fecha time, + SUM(Greuge) Greuge + FROM bi.Greuge_Evolution g + JOIN vn.client c ON c.id = g.Id_Cliente + JOIN vn.worker w ON w.id = c.salesPersonFk + GROUP BY Fecha; +SELECT Fecha time, + SUM(Ventas) Ventas + FROM bi.Greuge_Evolution g + JOIN vn.client c ON c.id = g.Id_Cliente + JOIN vn.worker w ON w.id = c.salesPersonFk + GROUP BY Fecha +SELECT Fecha time, + SUM(Ventas) Ventas, + SUM(Greuge) Greuge + FROM bi.Greuge_Evolution g + JOIN vn.client c ON c.id = g.Id_Cliente + JOIN vn.worker w ON w.id = c.salesPersonFk + GROUP BY Fecha +SELECT c.id, a.name 'Comercial', c.name , SUM(s.amount) + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + LEFT JOIN account.user a on a.id = s.salesPersonFk + WHERE dated BETWEEN 1() and 1() + AND a.id = 1731 + GROUP BY s.clientFk +SELECT c.id, c.name, a.name 'Comercial', SUM(s.amount) + FROM vn.client c + LEFT JOIN bs.salesByclientSalesPerson s ON s.clientFk = c.id + LEFT JOIN account.user a ON a.id = c.salesPersonFk + WHERE a.id = 1731 + AND s.dated BETWEEN 1() AND 1() + GROUP BY c.id + +SELECT sub2.clientFk AS "ID Cliente", + c.name AS "Nombre Cliente", + IF(c.isActive,'Sí','No') Activo, + u.name Comercial, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT sub2.clientFk AS "ID Cliente", + c.name AS "Nombre Cliente", + IF(c.isActive= TRUE,'Sí','No') Activo, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT t.dated time, IFNULL(sub.amount,0) 'Actual' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT t.dated time, IFNULL(sub.amount,0) 'Anterior' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) + INTERVAL 1 YEAR dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()- INTERVAL 1 YEAR) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' + + +SELECT sub2.clientFk + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk + +SELECT firstname Nombre, + lastname Apellidos, + SUM(ticketCount) 'Total tickets', + SUM(saleCount) 'Total lineas', + SUM(error) Errores, + 100 * SUM(error)/ SUM(ticketCount) Ratio, + SUM(volume) m3, + SUM(hourWorked) 'Total horas', + SUM(volume) / SUM(hourWorked) M3Hora + FROM vn.sectorProductivity + WHERE sector = '1' + AND dated BETWEEN 1() AND 1() + GROUP BY workerFk, sector + ORDER BY firstname; +SELECT left(DATE(t.shipped), 10) time, + CAST(sum(IF(s.isPicked , 0,sv.volume)) AS DECIMAL(10,1)) m3Free, + CAST(sum(IF(s.isPicked ,sv.volume,0)) AS DECIMAL(10,1)) m3Ready + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.sale s ON s.id = sv.saleFk + JOIN vn.itemShelvingStock iss ON iss.itemFk = s.itemFk + WHERE t.shipped >= timestampadd(DAY,-2,curdate()) AND sector = '1' + GROUP BY time, iss.sector +SELECT description FROM vn.sector +SELECT sub2.clientFk Cliente, + c.name Descripcion, + IF(c.isActive= TRUE,'Sí','No') Activo, + u.name Comercial, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT sub2.clientFk Cliente, + c.name Descripcion, + u.name Comercial, + IF(c.isActive= TRUE,'Sí','No') Activo, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT t.dated time, IFNULL(sub.amount,0) 'Actual' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT t.dated time, IFNULL(sub.amount,0) 'Anterior' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) + INTERVAL 1 YEAR dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()- INTERVAL 1 YEAR) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' + + +SELECT sub2.clientFk + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk + +SELECT sub2.clientFk Cliente, + c.name Descripcion, + IF(c.isActive= TRUE,'Sí','No') Activo, + u.name Comercial, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT sub2.clientFk Cliente, + c.name Descripcion, + u.name Comercial, + IF(c.isActive= TRUE,'Sí','No') Activo, + CAST((((sub2.currentYear*100)/sub2.lastYear )-100)*-1 AS DECIMAL (10,2)) 'Porcentaje % ', + sub2.currentYear 'Ventas año actual €', + sub2.lastYear 'Ventas año pasado €', + sub2.lastYear- sub2.currentYear 'Perdida €' + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount,YEAR(s.dated) year, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk +SELECT t.dated time, IFNULL(sub.amount,0) 'Actual' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT t.dated time, IFNULL(sub.amount,0) 'Anterior' + FROM vn.time t + LEFT JOIN (SELECT (s.dated - INTERVAL (DAY(s.dated)-1) DAY) + INTERVAL 1 YEAR dated, SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()- INTERVAL 1 YEAR) AND + s.clientFk = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated + )sub ON sub.dated = t.dated + WHERE t.dated BETWEEN 1() AND LAST_DAY(1()) + GROUP BY MONTH(t.dated) + ORDER BY t.dated +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' + + +SELECT sub2.clientFk + FROM (SELECT sub.clientFk,COUNT(*)count, SUM(IFNULL(sub1.amount,0)) currentYear, SUM(sub.amount) lastYear + FROM (SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND LAST_DAY(1()) - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + ) sub LEFT JOIN(SELECT s.clientFk, SUM(s.amount) amount, MONTH(s.dated)month + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND LAST_DAY(1()) AND + u.name = '1' + GROUP BY s.clientFk,YEAR(s.dated), MONTH(s.dated) + )sub1 ON sub1.clientFk= sub.clientFK AND sub1.month=sub.month + WHERE IFNULL(sub1.amount,0)-sub.amount < 0 + GROUP BY sub.clientFk + HAVING count = 3 + )sub2 JOIN vn.client c ON c.id = sub2.clientFk + JOIN account.user u ON u.id= c.salesPersonFk + +SELECT et.description Destino, + CAST(t.routeFk MOD 1000 AS DECIMAL(5,0)) Ruta, + DATE_FORMAT(et.ETD,'%H:%i') Hora, + CAST(sum(sv.volume * IF(tp.paletizadas,(1 - tp.paletizadas / tp.expediciones),1)) AS DECIMAL(5,1)) m3Restan + FROM vn.expeditionTruck et + JOIN vn.routesMonitor rm ON rm.expeditionTruckFk = et.id + JOIN vn.ticket t ON t.routeFk = rm.routeFk + JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.state st ON st.id = ts.stateFk + LEFT JOIN srt.routePalletized rp ON rp.routeFk = t.routeFk + LEFT JOIN srt.ticketPalletized tp ON tp.ticketFk = t.id + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleVolume sv ON sv.saleFk = s.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.config c + WHERE DATE(et.etd) = IF(HOUR(NOW()) > c.dayStartingHour, CURDATE(), util.yesterday()) + AND i.itemPackingTypeFk = 'H' + AND rm.bufferFk = '1' + GROUP BY Ruta + ORDER BY Hora,Ruta; +SELECT et.description Destino, + CAST(t.routeFk MOD 1000 AS DECIMAL(5,0)) Ruta, + DATE_FORMAT(et.ETD,'%H:%i') Hora, + CAST(sum(sv.volume * IF(tp.paletizadas,(1 - tp.paletizadas / tp.expediciones),1)) AS DECIMAL(5,1)) m3Restan + FROM vn.expeditionTruck et + JOIN vn.routesMonitor rm ON rm.expeditionTruckFk = et.id + JOIN vn.ticket t ON t.routeFk = rm.routeFk + JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.state st ON st.id = ts.stateFk + LEFT JOIN srt.routePalletized rp ON rp.routeFk = t.routeFk + LEFT JOIN srt.ticketPalletized tp ON tp.ticketFk = t.id + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleVolume sv ON sv.saleFk = s.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.config c + WHERE DATE(et.etd) = IF(HOUR(NOW()) > c.dayStartingHour, CURDATE(), util.yesterday()) + AND i.itemPackingTypeFk = 'H' + AND rm.bufferFk = '1' + GROUP BY Ruta + ORDER BY Hora,Ruta; +SELECT t.id ticket, e.id expedition, IFNULL(et.description, am.name) destino, DATE_FORMAT(e.created, '%H:%i:%s - %d/%m') "fecha impresión", e.bufferFk buffer +FROM srt.expedition e +JOIN vn.expedition e2 ON e2.id = e.id +JOIN vn.ticket t ON t.id = e2.ticketFk +JOIN vn.agencyMode am ON am.id = t.agencyModeFk +JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk +JOIN vn.expeditionTruck et ON et.id = rm.expeditionTruckFk +WHERE e2.created BETWEEN CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59'); +SELECT et.description Destino, + CAST(t.routeFk MOD 1000 AS DECIMAL(5,0)) Ruta, + DATE_FORMAT(et.ETD,'%H:%i') Hora, + CAST(sum(sv.volume * IF(tp.paletizadas,(1 - tp.paletizadas / tp.expediciones),1)) AS DECIMAL(5,1)) m3Restan + FROM vn.expeditionTruck et + JOIN vn.routesMonitor rm ON rm.expeditionTruckFk = et.id + JOIN vn.ticket t ON t.routeFk = rm.routeFk + JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.state st ON st.id = ts.stateFk + LEFT JOIN srt.routePalletized rp ON rp.routeFk = t.routeFk + LEFT JOIN srt.ticketPalletized tp ON tp.ticketFk = t.id + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleVolume sv ON sv.saleFk = s.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.config c + WHERE DATE(et.etd) = IF(HOUR(NOW()) > c.dayStartingHour, CURDATE(), util.yesterday()) + AND i.itemPackingTypeFk = 'H' + AND rm.bufferFk = '1' + GROUP BY Ruta + ORDER BY Hora,Ruta; +SELECT b.id + FROM srt.buffer b + JOIN vn.routesMonitor rm ON rm.bufferFk = b.id + WHERE rm.bufferFk +SELECT b.id + FROM srt.buffer b + JOIN vn.routesMonitor rm ON rm.bufferFk = b.id + WHERE rm.bufferFk +SELECT b.id + FROM srt.buffer b + JOIN vn.routesMonitor rm ON rm.bufferFk = b.id + WHERE rm.bufferFk +SELECT b.code, COUNT(e.bufferFk) as stock, NOW() time_sec +FROM srt.buffer b +LEFT JOIN srt.expedition e ON e.bufferFk = b.id +WHERE b.id > 1 +AND b.id MOD 2 = 0 +GROUP BY b.id +; +SELECT b.code, COUNT(e.bufferFk) as stock, NOW() time_sec +FROM srt.buffer b +LEFT JOIN srt.expedition e ON e.bufferFk = b.id +WHERE b.id > 1 +AND b.id MOD 2 != 0 +GROUP BY b.id +; +SELECT expeditionFk MOD 1000 expedition, bufferFromFk 'From', bufferToFk 'To' +FROM srt.movingLog m +ORDER BY m.id desc +LIMIT 10 +; +SELECT 0 'Calendario de Volumen 1 - 1' +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '1' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-01') + INTERVAL 1 DAY, '1-1-01') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '2' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-02') + INTERVAL 1 DAY, '1-1-02') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '3' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-03') + INTERVAL 1 DAY, '1-1-03') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '4' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-04') + INTERVAL 1 DAY, '1-1-04') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '5' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-05') + INTERVAL 1 DAY, '1-1-05') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '6' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-06') + INTERVAL 1 DAY, '1-1-06') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '7' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-07') + INTERVAL 1 DAY, '1-1-07') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '8' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-08') + INTERVAL 1 DAY, '1-1-08') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '9' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-09') + INTERVAL 1 DAY, '1-1-09') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '10' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-10') + INTERVAL 1 DAY, '1-1-10') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '11' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-11') + INTERVAL 1 DAY, '1-1-11') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '12' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-12') + INTERVAL 1 DAY, '1-1-12') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '13' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-1') + INTERVAL 1 DAY, '1-1-13') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '15' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-15') + INTERVAL 1 DAY, '1-1-15') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '14' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-14') + INTERVAL 1 DAY, '1-1-14') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '16' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-16') + INTERVAL 1 DAY, '1-1-16') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '17' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-17') + INTERVAL 1 DAY, '1-1-17') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '18' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-18') + INTERVAL 1 DAY, '1-1-18') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '19' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-19') + INTERVAL 1 DAY, '1-1-19') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '20' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-20') + INTERVAL 1 DAY, '1-1-20') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '21' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-21') + INTERVAL 1 DAY, '1-1-21') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '22' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-22') + INTERVAL 1 DAY, '1-1-22') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '23' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-23') + INTERVAL 1 DAY, '1-1-23') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '24' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-24') + INTERVAL 1 DAY, '1-1-24') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '25' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-25') + INTERVAL 1 DAY, '1-1-25') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '26' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-26') + INTERVAL 1 DAY, '1-1-26') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '27' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-27') + INTERVAL 1 DAY, '1-1-27') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '28' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-28') + INTERVAL 1 DAY, '1-1-28') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '29' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-29') + INTERVAL 1 DAY, '1-1-29') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '30' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-30') + INTERVAL 1 DAY, '1-1-30') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '31' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-31') + INTERVAL 1 DAY, '1-1-31') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT DISTINCT `year` + FROM vn.`time` t + WHERE `year` BETWEEN 2017 AND YEAR(CURDATE()) +UNION ALL +SELECT CONCAT('Previsión ', YEAR(CURDATE())) +1 - Enero, 2 - Febrero, 3 - Marzo, 4 - Abril, 5 - Mayo, 6 - Junio, 7 - Junio, 8 - Agosto, 9 - Septiembre, 10 - Octubre, 11 - Noviembre, 12 - Diciembre +SELECT SUBSTRING('1', 5) +SELECT TRIM(LEFT('1', 2)) +SELECT name + FROM vn.province + SELECT + COUNT(*), + ot.created time + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + WHERE ot.created > DATE_SUB(curdate(),interval 2 MONTH) + AND od.code = 'IT' + GROUP BY week(ot.created) + SELECT + COUNT(*), + DATE_ADD(otsTime.updated,interval -1 day) time + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + JOIN ost_ticket_status ots ON ot.status_id = ots.id + JOIN (SELECT DISTINCT DATE(DATE_ADD(ots.updated,interval 1 day)) updated + FROM ost_ticket ots + WHERE ots.updated BETWEEN 1() AND 1()) otsTime + ON otsTime.updated BETWEEN ot.created AND IFNULL(ot.closed,otsTime.updated) + LEFT JOIN ost_staff otsf ON ot.staff_id = otsf.staff_id + WHERE ot.ticket_pid IS NULL + AND od.code = 'IT' + GROUP BY otsTime.updated + SELECT + COUNT(*), + ot.created time + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + JOIN ost_ticket__cdata otc ON otc.ticket_id = ot.ticket_id + JOIN ost_ticket_priority otp ON otp.priority_id = otc.priority + WHERE ot.created > DATE_SUB(curdate(),interval 2 MONTH) + AND od.code = 'IT' + AND otp.priority = 'emergency' + GROUP BY week(ot.created) + SELECT + COUNT(*) total, + otsf.username AS metric + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + JOIN ost_ticket_status ots ON ot.status_id = ots.id + LEFT JOIN ost_staff otsf ON ot.staff_id = otsf.staff_id + WHERE ots.state = 'closed' + AND ot.ticket_pid IS NULL + AND od.code = 'IT' + AND closed BETWEEN 1() AND 1() + GROUP BY otsf.username +SELECT + c.id AS 'ID Cliente', + c.name AS 'Cliente', + u.name AS 'Comercial', + cr.amount 'Balance', + c.dueDay 'Día de Vencimiento', + cC.invoiced AS 'Consumo medio', + c.email AS 'Email', + c.phone AS 'Teléfono', + c.mobile AS 'Móvil', + a.postalCode AS 'CP', + a.city AS 'Localidad', + p.name AS 'Provincia', + pm.name AS 'Forma de pago', + bt.description AS 'Tipo de negocio' + + FROM vn.client c + JOIN bs.clientAnnualConsumption cC ON cC.clientFk = c.id + LEFT JOIN vn.businessType bt ON bt.code = c.businessTypeFk + LEFT JOIN vn.clientRisk cr ON cr.clientFk = c.id + JOIN vn.payMethod pm ON pm.id = c.payMethodFk + JOIN vn.address a ON a.id = c.defaultAddressFk + JOIN vn.province p ON p.id = a.provinceFk + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = c.salesPersonFk + JOIN vn.department d ON d.id = wd.departmentFk + WHERE c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('enric','jordi','juanvi','pako','salbarrilla','sandra','pepe','susana','jstegarescu') + AND c.isFreezed + AND cr.companyFk = 442 + + + +SELECT YEAR(s.dated) Año, MONTH(s.dated) Mes, s.clientFk Cliente, SUM(s.amount) 'Base Comisionable' +FROM bs.salesByclientSalesPerson s + WHERE 1(s.dated) + GROUP by MONTH(s.dated), s.clientFk +SELECT + SUM(IF (invoiced >= 10000, TRUE, FALSE)) '>10.000€', + SUM(IF (invoiced BETWEEN 7000 AND 9999, TRUE, FALSE)) '7.000 a 10.000', + SUM(IF (invoiced BETWEEN 5000 AND 6999, TRUE, FALSE)) '5.000 a 7.000', + SUM(IF (invoiced BETWEEN 3000 AND 4999, TRUE, FALSE)) '3.000 a 5.000', + SUM(IF (invoiced BETWEEN 1000 AND 2999, TRUE, FALSE)) '1.000 a 3.000', + SUM(IF (invoiced BETWEEN 500 AND 999, TRUE, FALSE)) '500 a 1.000', + SUM(IF (invoiced BETWEEN 300 AND 499, TRUE, FALSE)) '300 a 500', + SUM(IF (invoiced <= 299, TRUE, FALSE)) '<300' +FROM bs.clientAnnualConsumption +SELECT + c.id AS 'ID Cliente', + c.name AS 'Cliente', + u.name AS 'Comercial', + cC.invoiced AS 'Consumo medio', + c.email AS 'Email', + c.phone AS 'Teléfono', + c.mobile AS 'Móvil', + a.postalCode AS 'CP', + a.city AS 'Localidad', + p.name AS 'Provincia', + pm.name AS 'Forma de pago', + bt.description AS 'Tipo de negocio', + sub.dated 'Último Ticket confirmado' + + FROM vn.client c + LEFT JOIN( + SELECT clientFk, MAX(shipped) dated + FROM vn.ticket + GROUP BY clientFk) sub ON sub.clientFk = c.id + LEFT JOIN bs.clientAnnualConsumption cC ON cC.clientFk = c.id + LEFT JOIN vn.businessType bt ON bt.code = c.businessTypeFk + JOIN vn.payMethod pm ON pm.id = c.payMethodFk + JOIN vn.address a ON a.id = c.defaultAddressFk + JOIN vn.province p ON p.id = a.provinceFk + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = c.salesPersonFk + JOIN vn.department d ON d.id = wd.departmentFk + WHERE c.isActive + AND c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('enric','jordi','juanvi','pako','salbarrilla','sandra','pepe','susana','jstegarescu') + +SELECT + fecha 'Fecha último pedido', Id_Cliente 'ID Cliente', c.name 'Nombre Cliente',u.name Comercial +FROM bs.ventas v + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN bs.clientNewBorn cn ON cn.clientFk = c.id AND date_aDD(firstShipped, interval 1 year) > fecha +WHERE fecha >= util.quarterFirstDay(YEAR(CURDATE()),QUARTER(CURDATE())) +GROUP BY fecha , Id_Cliente +SELECT COUNT(DISTINCT(c.id)) 'Clientes nuevos con pedido' + FROM vn.client c + JOIN vn.ticket t ON t.clientFk = c.id + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + AND c.created BETWEEN 1() AND 1() + AND t.shipped BETWEEN 1() AND 1() + AND c.transferorFk IS NULL + +SELECT d.name Equipo, COUNT(DISTINCT(c.id)) Clientes + FROM vn.client c + JOIN vn.ticket t ON t.clientFk = c.id + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + AND c.created BETWEEN 1() AND 1() + AND t.shipped BETWEEN 1() AND 1() + AND c.transferorFk IS NULL +GROUP BY d.name +ORDER BY Clientes DESC +SELECT + co.code company, + cou.country, + c.id, + c.socialName, + SUM(s.quantity * s.price * ( 100 - s.discount ) / 100) amount, + IF(negativeBase.clientFk, 'Sí', 'No'), + negativeBase.base, + negativeBase.code, + negativeBase.ticketFk, + IF(c.isActive, 'Sí', 'No'), + IF(c.hasToInvoice, 'Sí', 'No'), + IF(c.isTaxDataChecked, 'Sí', 'No'), + w.id Comercial, + CONCAT(w.firstName, ' ', w.lastName) + FROM vn.ticket t + JOIN vn.company co ON co.id = t.companyFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.country cou ON cou.id = c.countryFk + LEFT JOIN vn.worker w ON w.id = c.salesPersonFk + LEFT JOIN ( + SELECT t.clientFk, + t.id ticketFk, + SUM(s.quantity * s.price * ( 100 - s.discount ) / 100) base, + tc.code + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.company co ON co.id = t.companyFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.itemTaxCountry itc ON itc.itemFk = s.itemFk + JOIN vn.country cou ON cou.id = itc.countryFk + JOIN vn.taxClass tc ON tc.id = itc.taxClassFk + LEFT JOIN vn.worker w ON w.id = c.salesPersonFk + WHERE 1(t.shipped) + AND t.refFk IS NULL + AND c.typeFk IN ('normal','trust') + GROUP BY t.clientFk, tc.code + HAVING base < 0)negativeBase ON negativeBase.clientFk = c.id + WHERE 1(t.shipped) + AND t.refFk IS NULL + AND c.typeFk IN ('normal','trust') + GROUP BY t.clientFk + HAVING amount <> 0 +SELECT * + FROM vn.contactChannel +SELECT * + FROM vn.businessType +SELECT id, name + FROM vn.department + /*WHERE name LIKE 'EQUIPO%'*/ +SELECT w.id, u.name + FROM vn.worker w + JOIN vn.client c ON c.salesPersonFk = w.id + JOIN account.user u ON u.id = w.id + GROUP BY w.id; + +SELECT id, name + FROM vn.payMethod + +SELECT + c.id 'ID Cliente', + c.fi 'NIF/CIF', + c.socialName 'Razón Social', + c.street 'Dirección Fiscal', + c.city 'Localidad', + p.name 'Provincia', + c.postCode 'CP', + c.payMethodFk 'Forma de pago', + c.contactChannelFk 'Canal de Contacto', + bt.description 'Tipo de Negocio', + c.credit 'Crédito', + c.creditInsurance 'Crédito Asegurado', + cC.invoiced AS 'Consumo medio', + c.salesPersonFk 'Comercial', + d.id 'Equipo', + sub.dated 'Último ticket confirmado' + + FROM vn.client c + LEFT JOIN( + SELECT clientFk, MAX(shipped) dated + FROM vn.ticket + GROUP BY clientFk) sub ON sub.clientFk = c.id + LEFT JOIN vn.worker w ON w.id = c.id + JOIN bs.clientAnnualConsumption cC ON cC.clientFk = c.id + LEFT JOIN vn.businessType bt ON bt.code = c.businessTypeFk + JOIN vn.province p ON p.id = c.provinceFk + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = c.salesPersonFk + JOIN vn.department d ON d.id = wd.departmentFk + WHERE c.isActive + AND w.id IS NULL +SELECT color, año, +SUM(tallos * (semana = 1)) 'Semana 1', +SUM(tallos * (semana = 1)) 'Semana 1', +SUM(tallos * (semana = 1)) 'Semana 1' +FROM ( +SELECT + i.id, + ik.name color, + i.longName articulo, + tt.`year` año, + CAST(SUM(s.quantity) AS int) tallos , + tt.`week` semana + FROM vn.`time` tt + LEFT JOIN bs.sale bs ON bs.dated = tt.dated + LEFT JOIN vn.itemType it ON it.id = bs.typeFk + STRAIGHT_JOIN vn.sale s ON s.id = bs.saleFk + LEFT JOIN vn.item i ON i.id = s.itemFk + LEFT JOIN vn.ink ik ON ik.id = i.inkFk + WHERE it.code IN(1) + AND tt.`week` BETWEEN 1 AND 1 + 2 + AND tt.`year` >= YEAR(curdate())- 1 + AND ('1' = '' OR i.name LIKE '1') + GROUP BY color, semana, año + ORDER BY color, semana, año) sub + GROUP BY color, año +SELECT DISTINCT week FROM vn.time; +SELECT code FROM vn.itemType ; +SELECT DISTINCT (week -1) FROM vn.time WHERE week <=10; + +SELECT 1 + 1 +SELECT 1 + 2 +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND 1() + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + d.name='1' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='1' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='1' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() AND 1() AND + u.name = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + u.name = '1' + GROUP BY MONTH(s.dated) + ORDER BY s.dated +SELECT s.dated time, SUM(s.amount) '2021' + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.time t on t.dated = s.dated + WHERE s.dated BETWEEN 1() AND 1() AND + u.name = '1' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2020' + FROM bs.salesByclientSalesPerson s + JOIN vn.time t on t.dated = s.dated + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + u.name = '1' + GROUP BY t.week + ORDER BY s.dated +SELECT s.dated + INTERVAL 2 YEAR time, SUM(s.amount) '2019' + FROM bs.salesByclientSalesPerson s + JOIN vn.time t on t.dated = s.dated + JOIN account.user u ON u.id = s.salesPersonFk + WHERE s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + u.name = '1' + GROUP BY t.week + ORDER BY s.dated +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT dated time, SUM(amount) + FROM bs.salesPersonEvolution + WHERE dated BETWEEN 1() AND 1() + GROUP BY dated + +SELECT s.dated time, SUM(s.amount) +FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk +WHERE u.name = '1' AND s.dated BETWEEN 1() AND 1() +GROUP BY s.dated + +SELECT s.dated time, sum(s.amount) + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='1' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT sum(amount) amount, year(dated) years + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() AND 1() + UNION ALL + SELECT sum(amount), year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(amount) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(amount) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT sum(amountNewBorn) amount, year(dated) years + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() AND 1() + UNION ALL + SELECT sum(amountNewBorn), year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(amountNewBorn) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(amountNewBorn) , year(dated) + FROM bs.salesByclientSalesPerson + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT SUM(s.amount) amount, year(s.dated) years + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() AND 1() AND + d.name='1' + UNION ALL + SELECT SUM(s.amount), year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='1' + )sub + )sub2 +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT SUM(s.amountNewBorn) amount, year(s.dated) years + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() AND 1() AND + d.name='1' + UNION ALL + SELECT SUM(s.amountNewBorn), year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amountNewBorn) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amountNewBorn) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='1' + )sub + )sub2 + +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT sum(s.amount) amount, year(s.dated) years + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() AND 1() + UNION ALL + SELECT sum(s.amount), year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT sum(s.amountNewBorn) amount, year(s.dated) years + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() AND 1() + UNION ALL + SELECT sum(s.amountNewBorn), year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + UNION ALL + SELECT sum(s.amountNewBorn) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR + UNION ALL + SELECT sum(s.amountNewBorn) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + WHERE u.name = '1' AND + s.dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR + )sub + )sub2 +SELECT SUM(sub2.year0) '2018',SUM(sub2.year1) '2019',SUM(sub2.year2) '2020',SUM(sub2.year3) '2021' + FROM(SELECT CASE + WHEN sub.years=2018 THEN sub.amount + ELSE 0 + END year0, + CASE + WHEN sub.years=2019 THEN sub.amount + ELSE 0 + END year1, + CASE + WHEN sub.years=2020 THEN sub.amount + ELSE 0 + END year2, + CASE + WHEN sub.years=2021 THEN sub.amount + ELSE 0 + END year3 + FROM(SELECT SUM(s.amount) amount, year(s.dated) years + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() AND 1() AND + d.name='1' + UNION ALL + SELECT SUM(s.amount), year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 2 YEAR AND 1() - INTERVAL 2 YEAR AND + d.name='1' + UNION ALL + SELECT SUM(s.amount) , year(s.dated) + FROM bs.salesByclientSalesPerson s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE dated BETWEEN 1() - INTERVAL 3 YEAR AND 1() - INTERVAL 3 YEAR AND + d.name='1' + )sub + )sub2 +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT salesPersonName, teamName, year, month, currentSale, commissionSale, individualPlus, teamPlus, teamScore, newClientPlus, newClientScore, teamBossPlus + FROM bs.salesMonthlySnapshot + WHERE year = YEAR(1()) AND month = MONTH(1()) +SELECT d.name Departamento, + 1 - SUM(s.quantity * s.price) - + (SELECT floor(sum(1.37 * b.amount * 12 / 365)) 'Coste Laboral Semanal' + FROM time t + JOIN business b ON b.started <= t.dated AND (b.ended >= t.dated OR ISNULL(b.ended)) + JOIN department d ON d.id = b.departmentFk + WHERE t.dated BETWEEN IF(1 = 0, '1-01-01', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31', lastDayOfWeek(1, 1)) + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name) 'Coste Material' + FROM sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN department d ON d.clientFk = c.id + JOIN item i ON i.id = s.itemFk + WHERE t.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND NOT i.name = 'Producto a IVA Reducido' + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name +SELECT (SELECT 1) '1', + (SELECT 1) '1' +SELECT (SELECT 1) '1', + (SELECT 1) '1' +SELECT d.name Departamento, + 1 - + SUM(s.quantity * s.price) - + (SELECT floor(sum(1.37 * b.amount * 12 / 365)) 'Coste Laboral Semanal' + FROM time t + JOIN business b ON b.started <= t.dated AND (b.ended >= t.dated OR ISNULL(b.ended)) + JOIN department d ON d.id = b.departmentFk + JOIN itemCategory ic ON ic.departmentFk = d.id + WHERE t.dated BETWEEN IF(1 = 0, '1-01-01', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31', lastDayOfWeek(1, 1)) + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name) 'Coste Material' +FROM sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN department d ON d.clientFk = c.id + JOIN item i ON i.id = s.itemFk +WHERE t.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND NOT i.name = 'Producto a IVA Reducido' + AND d.name = '1' +GROUP BY d.id +ORDER BY d.name +SELECT (SELECT 1) 'Unidades 1', + (SELECT -((1 - 1)/1 * 100)) 'Diferencia', + (SELECT 1) 'Unidades 1' +SELECT (SELECT 1) 'Ganancias 1', + (SELECT -((1 - 1)/1 * 100)) 'Diferencia', + (SELECT 1) 'Ganancias 1' +SELECT CONCAT('Año 1', IF(1 = 0, '', ' - Semana 1')) +SELECT d.name Departamento, + SUM(s.quantity * sc.value) 'Coste Material' + FROM sale s + STRAIGHT_JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN component cm ON cm.id = sc.componentFk + JOIN componentType ct ON ct.id = cm.typeFk + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN department d ON d.clientFk = c.id + JOIN item i ON i.id = s.itemFk + WHERE ct.code = 'COST' + AND t.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND NOT i.name = 'Producto a IVA Reducido' + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name +SELECT CONCAT('Año 1', IF(1 = 0, '', ' - Semana 1')) +SELECT d.name Departamento, + SUM(s.quantity * sc.value) 'Coste Material' + FROM sale s + STRAIGHT_JOIN saleComponent sc ON sc.saleFk = s.id + JOIN component cm ON cm.id = sc.componentFk + JOIN componentType ct ON ct.id = cm.typeFk + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN department d ON d.clientFk = c.id + JOIN item i ON i.id = s.itemFk + WHERE ct.code = 'COST' + AND t.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND NOT i.name = 'Producto a IVA Reducido' + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name +SELECT it.name 'Nombre de la Familia', + SUM(s.quantity) 'Cantidad', + SUM((s.quantity * s.price) * (100 - s.discount) / 100) 'Precio' + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) + GROUP BY it.name +SELECT it.name 'Nombre de la Familia', + SUM(s.quantity) 'Cantidad', + SUM((s.quantity * s.price) * (100 - s.discount) / 100) 'Precio' + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) + GROUP BY it.name +SELECT d.name Department, + FLOOR(SUM(1.37 * b.amount * 12 / 365)) 'Coste Laboral' +FROM time t + JOIN business b ON b.started <= t.dated AND (b.ended >= t.dated OR ISNULL(b.ended)) + JOIN department d ON d.id = b.departmentFk + JOIN itemCategory ic ON ic.departmentFk = d.id +WHERE t.dated BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND d.name = '1' +GROUP BY d.id +ORDER BY d.name +SELECT d.name Department, + FLOOR(SUM(1.37 * b.amount * 12 / 365)) 'Coste Laboral' + FROM time t + JOIN business b ON b.started <= t.dated AND (b.ended >= t.dated OR ISNULL(b.ended)) + JOIN department d ON d.id = b.departmentFk + JOIN itemCategory ic ON ic.departmentFk = d.id + WHERE t.dated BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND d.name = '1' + GROUP BY d.id + ORDER BY d.name +SELECT IF(d.name, d.name, IF(ic.shortLife, 'TALLER NATURAL','TALLER ARTIFICIAL')) Departmento, + FLOOR(sum(s.amount + s.margin)) 'Venta sin Recargo' + FROM bs.sale s + JOIN sale ss ON ss.id = s.saleFk + JOIN item i ON i.id = ss.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + LEFT JOIN producer p ON p.id = i.producerFk AND p.name = 'Confección' + LEFT JOIN department d ON d.id = ic.departmentFk + WHERE s.dated BETWEEN firstDayOfWeek(1, 1) AND lastDayOfWeek(1, 1) + AND (d.id OR p.id) + GROUP BY departmento +SELECT IF(d.name, d.name, IF(ic.shortLife, 'TALLER NATURAL','TALLER ARTIFICIAL')) Departmento, + FLOOR(sum(s.amount + s.margin)) 'Venta sin Recargo' + FROM bs.sale s + JOIN sale ss ON ss.id = s.saleFk + JOIN item i ON i.id = ss.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + LEFT JOIN producer p ON p.id = i.producerFk AND p.name = 'Confección' + LEFT JOIN department d ON d.id = ic.departmentFk + WHERE s.dated BETWEEN firstDayOfWeek(1, 1) AND lastDayOfWeek(1, 1) + AND (d.id OR p.id) + GROUP BY departmento + +2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 +SELECT DISTINCT CONCAT(week, IF(week = week(curdate()), ' - Actual', '')) + FROM time +UNION ALL +SELECT 0 +SELECT substring_index('1', '-', 1) +2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 +SELECT DISTINCT CONCAT(week, IF(week = week(curdate()), ' - Actual', '')) + FROM time +UNION ALL +SELECT 0 +SELECT substring_index('1', '-', 1) +SELECT IFNULL(SUM(s.quantity), 0) + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) +SELECT IFNULL(SUM(s.quantity), 0) + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) +SELECT IFNULL(SUM((s.quantity * s.price) * (100 - s.discount) / 100), 0) + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) +SELECT IFNULL(SUM((s.quantity * s.price) * (100 - s.discount) / 100), 0) + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN account.`user` u ON u.id = it.workerFk + JOIN ticket ti ON ti.id = s.ticketFk + WHERE u.name = '1' + AND NOT it.code = 'RST' + AND ti.shipped BETWEEN IF(1 = 0, '1-01-01 00:00:00', firstDayOfWeek(1, 1)) AND IF(1 = 0, '1-12-31 23:59:59', lastDayOfWeek(1, 1)) + AND ic.name = IF('1' = 'TALLER ARTIFICIAL', 'Confección artificial', ic.name) +SELECT CONCAT(u.name, ' - ', d.name) + FROM account.user u + JOIN itemType it ON it.workerFk = u.id + JOIN department d ON d.workerFk = u.id + GROUP BY it.workerFk +SELECT substring_index('1', '-', 1) +SELECT d.name + FROM department d + JOIN client c ON c.id = d.clientFk + JOIN account.user u ON u.id = d.workerFk + WHERE u.name = '1' +SELECT CONCAT(UPPER(LEFT(MONTHNAME(m.fecha), 1)), SUBSTR(MONTHNAME(m.fecha), 2)) Mes, + year1.1, + year2.1, + year3.1 + FROM bs.m3 m + LEFT JOIN (SELECT CONCAT(UPPER(LEFT(MONTHNAME(m.fecha), 1)), SUBSTR(MONTHNAME(m.fecha), 2)) Mes, + SUM(m3) '1', + m.`month` + FROM bs.m3 m + WHERE m.`year` = 1 + AND warehouseFk = 1 + GROUP BY m.`year`, MONTH(m.fecha), warehouseFk + ORDER BY MONTH(m.fecha)) year1 ON year1.`month` = m.`month` + LEFT JOIN (SELECT CONCAT(UPPER(LEFT(MONTHNAME(m.fecha), 1)), SUBSTR(MONTHNAME(m.fecha), 2)) Mes, + SUM(m3) '1', + m.`month` + FROM bs.m3 m + WHERE m.`year` = 1 + AND warehouseFk = 1 + GROUP BY m.`year`, MONTH(m.fecha), warehouseFk + ORDER BY MONTH(m.fecha)) year2 ON year2.`month` = m.`month` + LEFT JOIN (SELECT CONCAT(UPPER(LEFT(MONTHNAME(m.fecha), 1)), SUBSTR(MONTHNAME(m.fecha), 2)) Mes, + SUM(m3) '1', + m.`month` + FROM bs.m3 m + WHERE m.`year` = 1 + AND warehouseFk = 1 + GROUP BY m.`year`, MONTH(m.fecha), warehouseFk + ORDER BY MONTH(m.fecha)) year3 ON year3.`month` = m.`month` + GROUP BY MONTH(m.fecha) +SELECT CONCAT(m.warehouseFk, ' - ', w.name) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + GROUP BY m.warehouseFk +SELECT `year` + FROM bs.m3 + GROUP BY `year` +SELECT `year` + FROM bs.m3 + GROUP BY `year` +SELECT `year` + FROM bs.m3 + GROUP BY `year` +SELECT TRIM(LEFT('1', 2)) +WITH + clientRisk AS ( + SELECT SUM(amount) amount + FROM clientRisk + WHERE clientFk = 1), + dates AS ( + SELECT DISTINCT DATE(shipped) dated from ( + SELECT shipped + FROM + vn.ticket + WHERE clientFk = 1 + AND shipped > 1() + UNION ALL + SELECT payed + FROM + receipt r + WHERE clientFk = 1 + AND r.payed > 1()) t + ) + SELECT SUM(IFNULL(t.total,0))+ cr.amount consumo, d.dated + FROM dates d + JOIN clientRisk cr ON TRUE + LEFT JOIN ( + SELECT SUM(IFNULL(-totalWithVat,0)) total, + d.dated + FROM dates d + JOIN vn.ticket t + WHERE clientFk = 1 + AND t.shipped > dated + AND refFk IS NOT NULL + group by d.dated + UNION ALL + SELECT SUM(IFNULL(r.amountPaid,0)) total, + d.dated + FROM dates d + JOIN receipt r + WHERE r.clientFk = 1 + AND DATE(r.payed) > d.dated + group by d.dated + UNION ALL + SELECT SUM(IFNULL(totalWithVat,0)) total, + d.dated + FROM dates d + JOIN vn.ticket t + WHERE clientFk = 1 + AND DATE(t.shipped) <= d.dated + AND refFk IS NULL + AND t.isDeleted = FALSE + AND t.shipped > 1() + group by d.dated) t on t.dated = d.dated + GROUP BY d.dated + ORDER BY d.dated desc +SELECT + ${__from} AS time, + MIN(amount) +FROM clientCredit cc where clientFk = 1 +union all +SELECT + ${__to} AS time, + MIN(amount) +FROM clientCredit cc where clientFk = 1 +SELECT + ${__from} AS time, + MAX(amount) maxAmount +FROM clientCredit cc where clientFk = 1 +union all +SELECT + ${__to} AS time, + MAX(amount) +FROM clientCredit cc where clientFk = 1 +WITH + currentCredit AS ( + SELECT amount + FROM clientCredit + WHERE clientFk = 1 + ORDER BY created DESC + LIMIT 1) +SELECT + ${__from} AS time, + amount currentCredit +FROM currentCredit +union all +SELECT + ${__to} AS time, + amount +FROM currentCredit +-- Muestra los valores de los distintos creditos +SELECT credit, creationDate time +FROM vn.creditClassification cc + JOIN creditInsurance ci ON ci.creditClassificationFk = cc.id +WHERE client = 1 + AND (1(cc.dateStart) OR 1(cc.dateEnd) OR cc.dateEnd IS NULL) +UNION ALL +-- Añade valores al final del rango para los contratos discontinuos +SELECT ci.credit , IFNULL(cc.dateEnd,1()) time + FROM vn.creditClassification cc + LEFT JOIN vn.creditClassification cc2 + ON cc2.client = cc.client AND cc.dateEnd = cc2.dateStart + JOIN ( + SELECT credit, ci.creditClassificationFk + FROM creditInsurance ci + GROUP BY ci.creditClassificationFk + order by ci.creationDate DESC + LIMIT 1 + ) ci ON ci.creditClassificationFk = cc.id + WHERE cc.client = 1 AND cc2.id IS NULL + AND (1(cc.dateStart) OR 1(cc.dateEnd)) +UNION ALL +-- Añade un valor NULL cuando los contratos no son contiguos para que el grafico "pare" +SELECT NULL, TIMESTAMPADD( second,1,cc.dateEnd) time + FROM vn.creditClassification cc + LEFT JOIN vn.creditClassification cc2 + ON cc2.client = cc.client AND cc.dateEnd = cc2.dateStart + WHERE cc.client = 1 AND cc2.id IS NULL + AND (1(cc.dateStart) OR 1(cc.dateEnd)) +order by time +WITH + clientRisk AS ( + SELECT SUM(amount) amount + FROM clientRisk + WHERE clientFk = 1), + dates AS ( + SELECT DISTINCT DATE(dued) dated from ( + SELECT dued + FROM vn.invoiceOut + WHERE clientFk = 1 + AND dued > 1() + UNION ALL + SELECT payed + FROM + receipt r + WHERE clientFk = 1 + AND r.payed > 1()) t + ) + SELECT SUM(IFNULL(t.total,0)) + cr.amount balance, d.dated time + FROM dates d + JOIN clientRisk cr ON TRUE + LEFT JOIN ( + SELECT SUM(IFNULL(r.amountPaid,0)) total, + d.dated + FROM dates d + JOIN receipt r + WHERE r.clientFk = 1 + AND DATE(r.payed) > d.dated + group by d.dated + UNION ALL + SELECT -SUM(amount) total, + d.dated + FROM dates d + JOIN vn.invoiceOut io + WHERE clientFk = 1 + AND DATE(io.dued) > d.dated + group by d.dated) t on t.dated = d.dated + GROUP BY d.dated + ORDER BY d.dated desc +WITH + currentCredit AS ( + SELECT amount + FROM clientCredit + WHERE clientFk = 1 + ORDER BY created DESC + LIMIT 1) +SELECT + ${__from} AS time, + amount currentCredit +FROM currentCredit +union all +SELECT + ${__to} AS time, + amount +FROM currentCredit +SELECT id FROM vn.client + SELECT IFNULL(SUM(((`cb`.`quantity` * `s`.`price`) * (100 - `s`.`discount`)) / 100), 0) Importe, + c.id Id, + c.ticketFk Ticket, + cs.description Estado, + observation Observaciones, + cl.name 'Cliente', + cr.description 'Responsable', + crs.description 'Motivo', + u.name 'Comercial', + u.id 'userFk', + LEFT(c.ticketCreated, 10) 'Cre. del Ticket', + CONCAT(LEFT(c.created, 10), ' ', RIGHT(c.created, 8)) 'Creación' + FROM vn.claim c + LEFT JOIN vn.claimBeginning cb ON cb.claimFk = c.id + LEFT JOIN vn.sale s ON s.id = cb.saleFk + LEFT JOIN vn.claimDevelopment cd ON cd.claimFk = c.id + LEFT JOIN vn.claimResponsible cr ON cr.id = cd.claimResponsibleFk + LEFT JOIN vn.claimReason crs ON crs.id = cd.claimReasonFk + JOIN vn.client cl ON cl.id = c.clientFk + JOIN vn.worker w ON w.id = c.workerFk + JOIN vn.claimState cs ON cs.id = c.claimStateFk + LEFT JOIN account.user u ON u.id = w.id + WHERE 1(c.created) AND cr.description IN(1) + GROUP BY c.id + ORDER BY c.created DESC +SELECT description +FROM vn.claimResponsible; + +SELECT YEAR(s.dated) Año, MONTH(s.dated) Mes, u.name, SUM(amount) importe +FROM bs.salesByclientSalesPerson s + JOIN account.user u on u.id = s.salesPersonFk + WHERE s.dated BETWEEN '2022-01-01' AND '2022-01-31' + AND u.name NOT IN ('pako','jgallego','melvira','juanma','enric','jfrau','juanvi','sandra','palomaft','ralvarez','salva','amparocm','jordi','chechu','fran','daniel','elaine','ineslj','nuriald','mluz','david','antoniomp','ismaelalcolea','jstegarescu','mireia','miguelft','pacorico','raquelgc','ruben','pepe') + GROUP BY u.name +SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.lastSalesPersonFk workerFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY `year`, `month`, c.lastSalesPersonFk + + + +SELECT t.id Ticket, + t.shipped Fecha, + g.clientFk 'Id Cliente', + c.name Cliente, + c.salesPersonFk Comercial, + u.name Trabajador, + SUM(g.amount) Greuge + FROM greuge g + JOIN ticket t ON t.id = g.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN account.user u ON u.id = c.salesPersonFk + JOIN greugeType gt On gt.id = g.greugeTypeFk + WHERE gt.name= 'Diferencia portes' AND + t.shipped BETWEEN 1() AND 1() + GROUP BY g.ticketFk + HAVING Greuge >50 + ORDER BY Greuge DESC +SELECT sub.ticketFk Ticket, + c.id Cliente, + c.name Descripción, + u.id Empleado, + u.name Usuario, + sub.itemFk Artículo, + sub.concept Concepto, + sub.margin Margen, + sub.mana Maná, + sub.margin - sub.mana Diferencia, + sub.created 'Fecha Entregado', + sub1.odbc_date 'Fecha Maná' + FROM (SELECT s.ticketFk, t.clientFk, s.itemFk, s.concept, sc1.value * s.quantity margin, sc2.value * s.quantity mana, sc2.created + FROM vn.saleComponent sc1 + JOIN vn.saleComponent sc2 ON sc1.saleFk = sc2.saleFk AND sc2.componentFk=37 + JOIN vn.sale s ON s.id =sc1.saleFk AND sc1.componentFk=29 + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE t.shipped BETWEEN 1() AND 1() AND sc1.value < sc2.value and s.quantity <> 0 + ) sub JOIN(SELECT t.id,i.odbc_date + FROM vn.ticket t + JOIN vncontrol.inter i ON i.Id_Ticket = t.id + JOIN vn.state s ON s.id= i.state_id + WHERE s.code='DELIVERED' AND t.shipped BETWEEN 1() AND 1() + )sub1 ON sub1.id = sub.ticketFk + JOIN vn.client c ON c.id = sub.clientFk + JOIN account.user u ON u.id = c.salesPersonFk + WHERE sub1.odbc_date > sub.created + ORDER BY Diferencia +SELECT v.numberPlate MATRICULA, + w2.code CHOFER, + et.description DESTINO, + t.routeFk RUTA, + count(e.id) Total, + IF(COUNT(e.id) = SUM(est.code = 'DELIVERED'), 0, COUNT(e.id) - COUNT(DISTINCT es.id)) 'Sin Escanear', + sum(est.code = 'PALLETIZED') 'Pallet', + sum(est.code = 'STORED') "Plataforma", + sum(est.code = 'ON DELIVERY') "Reparto", + sum(est.code = 'DELIVERED') Entregadas, + sum(IF(ISNuLL(es.id),0,(est.code = 'LOST'))) Perdidas +FROM vn.expedition e +LEFT JOIN vn.expeditionScan es ON es.expeditionFk = e.id +JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk +JOIN vn.ticket t ON t.id = e.ticketFk +JOIN vn.route r ON r.id = t.routeFk +LEFT JOIN vn.routesMonitor rm ON rm.routeFk = r.id +LEFT JOIN vn.expeditionTruck et ON et.id = rm.expeditionTruckFk +LEFT JOIN vn.vehicle v ON v.id = r.vehicleFk +LEFT JOIN vn.worker w2 ON w2.id = r.workerFk +JOIN vn.agencyMode am ON am.id = r.agencyModeFk +JOIN vn.agency ag ON ag.id = am.agencyFk +JOIN vn.warehouse w ON w.id = ag.warehouseFk +WHERE DATE(r.created) = '1' + AND t.routeFk + AND w.name = '1' +GROUP BY t.routeFk +SELECT t.id Ticket, + t.addressFk Consignatario, + t.nickname Cliente, + c.itemPackingTypeFk IPT, + e.id Expedicion, + e.packagingFk Embalaje + FROM vn.expedition e + JOIN vn.expeditionScan es ON es.expeditionFk = e.id + JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk + JOIN vn.ticket t ON t.id = e.ticketFk + LEFT JOIN vn.ticketCollection tc on tc.ticketFk = t.id + LEFT JOIN vn.collection c on c.id = tc.collectionFk + JOIN vn.route r ON r.id = t.routeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + JOIN vn.warehouse w ON w.id = ag.warehouseFk + WHERE t.routeFk = '1' + AND w.name = '1' + AND est.code = 'LOST' + ORDER BY t.id, t.addressFk; +SELECT t.id ticketFk, + t.addressFk CONSIGNATARIO, + t.nickname CLIENTE, + count(e.id) Total, + count(DISTINCT es.id) Escaneadas + FROM vn.expedition e + LEFT JOIN vn.expeditionScan es ON es.expeditionFk = e.id + LEFT JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk + JOIN vn.ticket t ON t.id = e.ticketFk + JOIN vn.route r ON r.id = t.routeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + JOIN vn.warehouse w ON w.id = ag.warehouseFk + WHERE t.routeFk = '1' + AND w.name = '1' + GROUP BY t.id + HAVING Total <> Escaneadas + ORDER BY t.addressFk; +SELECT t.addressFk Consignatario , + e.ticketFk Ticket, + c.itemPackingTypeFk IPT, + e.id Expedicion, + e.packagingFk Embalaje, + DATE_FORMAT(se.updated,'%H:%i') Sorter + FROM vn.expedition e + JOIN vn.ticket t ON t.id = e.ticketFk + LEFT JOIN vn.ticketCollection tc on tc.ticketFk = t.id + LEFT JOIN vn.collection c on c.id = tc.collectionFk + LEFT JOIN vn.expeditionScan es ON es.expeditionFk = e.id + LEFT JOIN srt.expedition se ON se.id = e.id + JOIN vn.route r ON r.id = t.routeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + JOIN vn.warehouse w ON w.id = ag.warehouseFk + WHERE t.routeFk = '1' + AND ISNULL(es.id) + AND w.name = '1' + ORDER BY t.addressFk ,e.ticketFk +SELECT es.palletFk PALLET, count(es.id) CAJAS +FROM vn.expeditionScan es +JOIN vn.expedition e on e.id = es.expeditionFk +JOIN vn.ticket t on t.id = e.ticketFk +WHERE t.routeFk = '1' +GROUP BY es.palletFk +order by palletFk +SELECT et.description PLATAFORMA, + DATE_FORMAT(et.ETD,'%H:%i') ETD, + IFNULL(se.pallets,0) + IFNULL(sp.pallets,0) + COUNT(distinct ep.id) TOTAL, + se.pallets `PREPARAR`, + sp.pallets `ENCAJADO`, + COUNT(distinct ep.id) PALETIZADOS +FROM vn.expeditionTruck et +LEFT JOIN vn.expeditionPallet ep on et.id = ep.truckFk +JOIN vn.routesMonitor rm ON et.id = rm.expeditionTruckFk +JOIN vn.route r ON r.id = rm.routeFk +JOIN vn.agencyMode am ON am.id = r.agencyModeFk +JOIN vn.agency ag ON ag.id = am.agencyFk +JOIN vn.warehouse w ON w.id = ag.warehouseFk +LEFT JOIN (SELECT et.id, + CAST(SUM(p.width*p.height*p.`depth`) / 2000000 AS DECIMAL(5,1)) pallets + FROM vn.ticket t + JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk + JOIN vn.route r ON r.id = rm.routeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + JOIN vn.warehouse w ON w.id = ag.warehouseFk + JOIN vn.expeditionTruck et ON et.id = rm.expeditionTruckFk + JOIN vn.expedition e ON e.ticketFk = t.id + JOIN vn.packaging p ON p.id = e.packagingFk + LEFT JOIN vn.expeditionScan es ON es.expeditionFk = e.id + WHERE DATE(r.created) = '1' + AND w.name = '1' + AND es.palletFk IS NULL + GROUP BY et.id) sp ON sp.id = et.id +LEFT JOIN (SELECT et.id, + CAST(sum(sv.volume) / 2 AS DECIMAL(5,1)) pallets + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk + JOIN vn.route r ON r.id = rm.routeFk + JOIN vn.agencyMode am ON am.id = r.agencyModeFk + JOIN vn.agency ag ON ag.id = am.agencyFk + JOIN vn.warehouse w ON w.id = ag.warehouseFk + JOIN vn.expeditionTruck et ON et.id = rm.expeditionTruckFk + LEFT JOIN vn.expedition e ON e.ticketFk = t.id + WHERE DATE(r.created) = '1' + AND w.name = '1' + AND e.id IS NULL + GROUP BY et.id) se ON se.id = et.id +WHERE DATE(r.created) = '1' + AND w.name = '1' +GROUP BY et.description +ORDER BY et.ETD; +SELECT t.nickName CONSIGNATARIO, t.addressFk CONSIGNA, t.clientFk CLIENTE, count(*) CAJAS, t.routeFk MOD 1000 RUTA +FROM vn.expeditionScan es +JOIN vn.expedition e on e.id = es.expeditionFk +JOIN vn.ticket t on t.id = e.ticketFk +WHERE es.palletFk = '1' +GROUP BY t.addressFk +SELECT es.palletFk `Nº PALLET`, +GROUP_CONCAT(DISTINCT t.routeFk MOD 1000 ORDER BY r.id SEPARATOR ' , ') RUTAS +FROM vn.expedition e +JOIN vn.ticket t on t.id = e.ticketFk +JOIN vn.expeditionStateType est ON est.id = e.stateTypeFk +JOIN vn.expeditionScan es ON es.expeditionFk = e.id +JOIN vn.expeditionPallet ep on ep.id = es.palletFk +JOIN vn.expeditionTruck et ON et.id = ep.truckFk +JOIN vn.routesMonitor rm ON et.id = rm.expeditionTruckFk +JOIN vn.route r ON r.id = rm.routeFk +JOIN vn.agencyMode am ON am.id = r.agencyModeFk +JOIN vn.agency ag ON ag.id = am.agencyFk +JOIN vn.warehouse w ON w.id = ag.warehouseFk +WHERE DATE(r.created) = '1' + AND w.name = '1' + AND est.code = 'PALLETIZED' + GROUP BY es.palletFk; +SELECT DISTINCT w.name + FROM vn.warehouse w + JOIN vn.agency ag ON ag.warehouseFk = w.id + JOIN vn.agencyMode am ON am.agencyFk = ag.id + JOIN vn.route r ON r.agencyModeFk = am.id + WHERE r.created > TIMESTAMPADD(DAY,-7,CURDATE()) +SELECT LEFT(dated,10) FROM vn.time WHERE dated BETWEEN TIMESTAMPADD(WEEK,-1,CURDATE()) AND TIMESTAMPADD(WEEK,1,CURDATE()) +SELECT r.id routeFk +FROM vn.route r +WHERE r.created >= '1' +ORDER BY r.id; +SELECT DISTINCT palletFk +FROM vn.expeditionScan WHERE scanned BETWEEN TIMESTAMPADD(DAY,-2,'1') AND '1' +SELECT CONCAT(w.firstName, ' ', w.lastName) Trabajador, w.id, w.code, + c.id COLECCIÓN, + c.itemPackingTypeFk IPT, + DATE_FORMAT(c.created,'%H:%i') HORA_INICIO, + tn.name TREN, + c.saleTotalCount TOTAL_LINEAS, + c.salePickedCount TOTAL_PICKING, + CAST(100 * c.salePickedCount / c.saleTotalCount AS DECIMAL(3,0)) PORCENTAJE, + DATE_FORMAT(TIMEDIFF(NOW(),c.created) ,'%H:%i') TIEMPO, + CAST(c.salePickedCount / (TIME_TO_SEC(TIMEDIFF(NOW(),c.created)) / 3600) AS DECIMAL(10,0)) LINEAS_HORA, + sub2.totalLineas, + se.description SECTOR +FROM vn.collection c + JOIN vn.state s ON s.id = c.stateFk + JOIN vn.train tn ON tn.id = c.trainFk + JOIN vn.worker w ON w.id = c.workerFk + JOIN vn.sector se ON se.id = w.sectorFk + LEFT JOIN ( + SELECT w.id, sum(c.saleTotalCount) totalLineas + FROM vn.collection c + JOIN vn.worker w ON w.id = c.workerFk + WHERE 1(c.created) + GROUP BY w.id + ) sub2 ON sub2.id = w.id +WHERE 1(c.created) + AND s.code = 'ON_PREPARATION' + AND c.saleTotalCount +order by c.id +SELECT + SUM(volume) / SUM(hourWorked) 'm³/Hora' + FROM vn.errorProduction e + JOIN vn.worker w on w.id= e.userFk + WHERE 1(e.dated) + AND w.id = 1 + +SELECT + CAST(AVG(c.salePickedCount / (TIME_TO_SEC(TIMEDIFF(sub.lastTime,c.created)) / 3600)) + AS DECIMAL(10,0)) MEDIA +FROM vn.collection c + JOIN vn.state s ON s.id = c.stateFk + JOIN vn.train tn ON tn.id = c.trainFk + JOIN vn.worker w ON w.id = c.workerFk + LEFT JOIN ( + SELECT tc.collectionFk, max(st.created) lastTime + FROM vn.saleTracking st + JOIN vn.worker w ON w.id = st.workerFk + JOIN vn.sale s ON s.id = st.saleFk + JOIN vn.ticketCollection tc ON tc.ticketFk = s.ticketFk + WHERE 1(tc.created) + AND w.id = 1 + GROUP BY tc.collectionFk + ) sub ON sub.collectionFk = c.id +WHERE 1(c.created) + AND c.saleTotalCount + AND w.id = 1 + +SELECT IF(1 < 0, 0, IF(1 > 10, 10, 1)) 'Nota' +SELECT + c.id Colección, + c.itemPackingTypeFk IPT, + DATE_FORMAT(c.created,'%H:%i') 'H. Inicio', + tn.name Tren, + c.saleTotalCount - sub.Previa 'Líneas', + sub.Previa 'Lineas Previa', + DATE_FORMAT(TIMEDIFF(sub.lastTime,c.created) ,'%H:%i') Tiempo, + CAST(c.salePickedCount / (TIME_TO_SEC(TIMEDIFF(sub.lastTime,c.created)) / 3600) AS DECIMAL(10,0)) 'Líneas Hora', + Volumen +FROM vn.collection c + JOIN vn.state s ON s.id = c.stateFk + JOIN vn.train tn ON tn.id = c.trainFk + JOIN vn.worker w ON w.id = c.workerFk + LEFT JOIN ( + SELECT tc.collectionFk, max(st.created) lastTime, sum(sv.volume) Volumen, Count(Distinct sgd.id) Previa + FROM vn.saleTracking st + JOIN vn.worker w ON w.id = st.workerFk + JOIN vn.sale s ON s.id = st.saleFk + LEFT join vn.saleGroupDetail sgd on sgd.saleFk = s.id + JOIN vn.saleVolume sv on sv.saleFk = s.id + JOIN vn.ticketCollection tc ON tc.ticketFk = s.ticketFk + WHERE 1(tc.created) + AND w.id = 1 + GROUP BY tc.collectionFk) sub ON sub.collectionFk = c.id +WHERE 1(c.created) + AND c.saleTotalCount + AND w.id = 1 +ORDER BY c.created desc +SELECT chb.created, w.code + FROM vn.collectionHotbed chb + JOIN vn.worker w ON w.id = chb.userFk; +SELECT id, created, wagons , trainFk, itemPackingTypeFk + FROM vn.collection WHERE workerFk IS NULL ORDER BY id DESC +SELECT tc.collectionFk 'Colección', +c.itemPackingTypeFk 'ITP', +DATE_FORMAT(MIN(e.created),'%H:%i') 'H. Inicio', +DATE_FORMAT(MAX(e.created),'%H:%i') 'H. Fin', +COUNT(Distinct e.id) 'Bultos', +TIMEDIFF(max(e.created),min(e.created)) 'Tiempo Expedición', +c.saleTotalCount 'Total Líneas', +Cast(Sum(sv.volume) as Decimal(10,2)) Volumen +FROM vn.expedition e +JOIN vn.worker w ON w.id = e.workerFk +JOIN vn.ticket t ON t.id = e.ticketFk +JOIN vn.sale s on s.ticketFk = t.id +JOIN vn.saleVolume sv on sv.saleFk = s.id +JOIN vn.ticketCollection tc ON tc.ticketFk = t.id +JOIN vn.collection c ON c.id = tc.collectionFk +WHERE 1(e.created) and w.id = 1 +GROUP BY tc.collectionFk +HAVING Bultos > 1 +SELECT d.name 'Departamento', + SUM(b.started <= TIMESTAMPADD(YEAR,-1,'2022-09-07') AND (b.ended >= TIMESTAMPADD(YEAR,-1,'2022-09-07') OR b.ended IS NULL)) 'Año Pasado', + SUM(b.started <= '2022-09-07' AND (b.ended >= '2022-09-07' OR b.ended IS NULL)) 'Año Actual' +FROM vn.business b + JOIN vn.worker w ON w.id = b.workerFk + JOIN vn.department d ON d.id = b.departmentFk + WHERE ( + (b.started <= '2022-09-07' AND (b.ended >= '2022-09-07' OR b.ended IS NULL)) + OR + (b.started <= TIMESTAMPADD(YEAR,-1,'2022-09-07') AND (b.ended >= TIMESTAMPADD(YEAR,-1,'2022-09-07') OR b.ended IS NULL)) + ) + AND d.name IN ('CAMARA','CAMPOS','COMPLEMENTOS','PRESERVADO','PRODUCCION','TALLER NATURAL', 'TALLER ARTIFICIAL') + GROUP BY departamento; +SELECT DISTINCT CONCAT(w.firstName, ' ', w.lastName) __text, + w.id __value + FROM vn.worker w + JOIN vn.workerDepartment wd ON wd.workerFk = w.id + JOIN vn.department d ON d.id = wd.departmentFk + LEFT JOIN vn.collection c2 ON c2.workerFk = w.id + LEFT JOIN vn.expedition e ON e.workerFk = w.id + WHERE d.code IN ('PROD', 'CMA', 'CMP') +0.20 +3 +SELECT (((SUM(volume) / SUM(hourWorked) - 1) / (1 - 1)) * 10) + FROM vn.errorProduction e + JOIN vn.worker w on w.id= e.userFk + WHERE 1(e.dated) + AND w.id = 1 +20 +100 +SELECT (((AVG(c.salePickedCount / (TIME_TO_SEC(TIMEDIFF(sub.lastTime,c.created)) / 3600)) - 1) / (1 - 1)) * 10) +FROM vn.collection c + JOIN vn.state s ON s.id = c.stateFk + JOIN vn.train tn ON tn.id = c.trainFk + JOIN vn.worker w ON w.id = c.workerFk + LEFT JOIN ( + SELECT tc.collectionFk, max(st.created) lastTime + FROM vn.saleTracking st + JOIN vn.worker w ON w.id = st.workerFk + JOIN vn.sale s ON s.id = st.saleFk + JOIN vn.ticketCollection tc ON tc.ticketFk = s.ticketFk + WHERE 1(tc.created) + AND w.id = 1 + GROUP BY tc.collectionFk + ) sub ON sub.collectionFk = c.id +WHERE 1(c.created) + AND c.saleTotalCount + AND w.id = 1 +SELECT IF(1 < 0, 0, IF(1 > 10, 10, 1)) +SELECT ((1 * 30) / 100) + ((1 * 70) / 100) +SELECT IF(1 < 0, 0, IF(1 > 10, 10, 1)) +SELECT b.itemFk, i.longName, i.`size`, CAST(SUM(b.quantity * b.buyingValue) / sum(b.quantity) AS DECIMAL(10,3))pmp, venta +FROM vn.item i +JOIN vn.itemType it ON it.id = i.typeFk +JOIN vn.buy b ON b.itemFk = i.id +JOIN vn.entry e ON e.id = b.entryFk +JOIN vn.travel t ON t.id = e.travelFk +JOIN vn.supplier s ON s.id = e.supplierFk +LEFT JOIN (SELECT itemFk, CAST(sum(quantity * value) / sum(quantity) AS DECIMAL (10,3)) venta + FROM ( + SELECT s.id, s.itemFk, s.quantity , sum(sc.value) value + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component co ON co.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = co.typeFk AND ct.isBase + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + JOIN vn.client c ON c.id = t.clientFk + WHERE t.warehouseFk = 60 + AND NOT i.isFloramondo + AND 1(t.shipped) + AND it.code = '1' + AND c.isRelevant + GROUP BY s.id) sub2 + GROUP BY sub2.itemFk +) sub ON sub.itemFk = i.id +WHERE e.invoiceInFk + AND 1(t.landed) + AND it.code = '1' + AND NOT i.isFloramondo + AND t.warehouseInFk = 60 +GROUP BY i.id +ORDER BY longName ; +SELECT code FROM vn.itemType + SELECT 1 'Volumen Total Teórico' +SELECT 0 'Calendario de Volumen 1 - 1' +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '1' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-01') + INTERVAL 1 DAY, '1-1-01') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '3' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-03') + INTERVAL 1 DAY, '1-1-03') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '2' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-02') + INTERVAL 1 DAY, '1-1-02') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '4' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-04') + INTERVAL 1 DAY, '1-1-04') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '5' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-05') + INTERVAL 1 DAY, '1-1-05') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '6' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-06') + INTERVAL 1 DAY, '1-1-06') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '7' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-07') + INTERVAL 1 DAY, '1-1-07') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '8' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-08') + INTERVAL 1 DAY, '1-1-08') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '9' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-09') + INTERVAL 1 DAY, '1-1-09') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '10' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-10') + INTERVAL 1 DAY, '1-1-10') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '11' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-11') + INTERVAL 1 DAY, '1-1-11') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '12' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-12') + INTERVAL 1 DAY, '1-1-12') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '13' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-1') + INTERVAL 1 DAY, '1-1-13') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '14' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-14') + INTERVAL 1 DAY, '1-1-14') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT 1 'Encajadores' + +SELECT 1 'Sacadores' +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '15' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-15') + INTERVAL 1 DAY, '1-1-15') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '16' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-16') + INTERVAL 1 DAY, '1-1-16') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '17' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-17') + INTERVAL 1 DAY, '1-1-17') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '18' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-18') + INTERVAL 1 DAY, '1-1-18') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '19' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-19') + INTERVAL 1 DAY, '1-1-19') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '20' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-20') + INTERVAL 1 DAY, '1-1-20') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '21' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-21') + INTERVAL 1 DAY, '1-1-21') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT 1 / 1 'Por día:' + +SELECT 1 / 1 'Por día:' +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '22' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-22') + INTERVAL 1 DAY, '1-1-22') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '23' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-23') + INTERVAL 1 DAY, '1-1-23') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '24' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-24') + INTERVAL 1 DAY, '1-1-24') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '25' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-25') + INTERVAL 1 DAY, '1-1-25') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '26' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-26') + INTERVAL 1 DAY, '1-1-26') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '27' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-27') + INTERVAL 1 DAY, '1-1-27') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '28' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-28') + INTERVAL 1 DAY, '1-1-28') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT (1 / 1) / 1 'Por turno:' + +SELECT (1 / 1) / 1 'Por turno:' +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '29' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-29') + INTERVAL 1 DAY, '1-1-29') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '30' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-30') + INTERVAL 1 DAY, '1-1-30') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM ( SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '31' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-31') + INTERVAL 1 DAY, '1-1-31') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT CONCAT(w.firstName, ' ', w.lastName) + FROM vn.workerDepartment wd + LEFT JOIN vn.worker w ON w.id = wd.workerFk + LEFT JOIN vn.errorProduction ep ON ep.userFk = w.id + WHERE ep.rol = "Encajadores" + AND ep.dated > CURDATE() - INTERVAL 7 DAY + GROUP BY w.id + ORDER BY w.firstName +SELECT CONCAT(w.firstName, ' ', w.lastName) + FROM vn.workerDepartment wd + LEFT JOIN vn.worker w ON w.id = wd.workerFk + LEFT JOIN vn.errorProduction ep ON ep.userFk = w.id + WHERE ep.rol = "Sacadores" + AND ep.dated > CURDATE() - INTERVAL 7 DAY + GROUP BY w.id + ORDER BY w.firstName +0 +2, 2.1, 2.2, 2.3, 2.4, 2.5 ,2.6 ,2.7, 2.8, 2.9, 3 +1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2 +1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 +1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12, 13, 14, 15, 16 +1, 2, 3, 4, 5, +SELECT IF(1, (FLOOR((1 / (1 * 1))) + 1) , 0) +SELECT IF(1, (FLOOR((1 / (1 * 1))) + 1), 0) +SELECT DISTINCT `year` + FROM vn.`time` t + WHERE `year` BETWEEN 2017 AND YEAR(CURDATE()) +UNION ALL +SELECT CONCAT('Previsión ', YEAR(CURDATE())) +1 - Enero, 2 - Febrero, 3 - Marzo, 4 - Abril, 5 - Mayo, 6 - Junio, 7 - Junio, 8 - Agosto, 9 - Septiembre, 10 - Octubre, 11 - Noviembre, 12 - Diciembre +SELECT SUBSTRING('1', 5) +SELECT TRIM(LEFT('1', 2)) +SELECT description SECTOR, pasillo PASILLO, sum(num > 0) OCUPADOS , sum(num = 0) LIBRES +FROM ( +SELECT s.description , LEFT(p.code,1) pasillo, sum(IFNULL(ish.id,0) > 0) num + FROM vn.sector s + JOIN vn.parking p ON s.id = p.sectorFk + LEFT JOIN vn.shelving sh ON sh.parkingFk = p.id + LEFT JOIN vn.itemShelving ish ON ish.shelvingFk = sh.code AND ish.visible > 0 + WHERE s.warehouseFk = 60 + GROUP BY s.id, p.id) sub group BY pasillo +SELECT + it.name "Reino", + ic.name "Familia", + YEAR(si.dated) "Año", + WEEK(si.dated) "Semana", + si.netSale "Venta neta", + si.stems "Tallos", + si.`references` "Referencias", + si.trash "Tallos basura", + si.faults "Tallos faltas", + si.claimed "Reclamado", + si.accepted "Aceptado", + si.sale "Venta", + si.buy "Compra", + si.saleComponent "Venta componente", + si.costComponent "Coste componente", + si.marginComponent "Margen componente" +FROM bs.salesByItemTypeDay si + JOIN vn.itemType it ON it.id = si.itemTypeFk + JOIN vn.itemCategory ic ON ic.id = si.itemCategoryFk +WHERE si.dated BETWEEN 1() AND 1() +GROUP BY it.name, ic.name, YEAR(si.dated), WEEK(si.dated) +SELECT * + FROM bs.clientDied +SELECT + w.id workerFk, + w.firstName, + w.lastName, + wc.name, + COUNT(*) days, + IF( maternity.workerFk IS NULL, 'No', 'Sí') maternity, + IF( COUNT(*) >= DATEDIFF(1(), CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01')) + AND maternity.workerFk IS NULL, 'Sí', 'No') compute, + sub.routeCount, + sub.km, + sub.m3, + mainlineDelivered.total, + sub.kmAmount, + sub.m3Amount, + mainlineDelivered.amount, + sub.totalAmount + IFNULL(mainlineDelivered.amount, 0) totalAmount + FROM business b + JOIN worker w ON w.id = b.workerFk + JOIN time t ON t.dated BETWEEN b.started AND IFNULL(b.ended,1()) + JOIN department AS d ON d.id = b.departmentFk + JOIN workCenter wc ON wc.id = b.workcenterFk + JOIN routeConfig rc ON TRUE + LEFT JOIN workerDistributionCategory wd ON wd.workerFk = b.workerFk + JOIN ( + SELECT workerFk, + COUNT(*) routeCount, + SUM(r.kmEnd-r.KmStart) km, + SUM(r.m3) m3, + SUM(rc.yearlyKm) kmAmount, + SUM(rc.yearlyM3) m3Amount, + SUM(rc.yearlyKm) + SUM(rc.yearlyM3) totalAmount + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + WHERE r.created BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + GROUP BY r.workerFk + )sub ON sub.workerFk = w.id + LEFT JOIN ( + SELECT + rc.workerFk, + COUNT(*) total, + COUNT(*) * rco.mainlineDelivered amount + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + JOIN vn.time t ON t.dated = rc.dated + JOIN routeConfig rco + JOIN vn.business b ON t.dated BETWEEN b.started + AND IFNULL(b.ended, CURDATE()) AND b.workerBusinessProfessionalCategoryFk = rco.truckerBusinessProfessionalCategoryFk + AND rc.workerFk = b.workerFk + WHERE rc.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND ra.isMainlineDelivered + GROUP BY rc.workerFk )mainlineDelivered ON mainlineDelivered.workerFk = w.id + LEFT JOIN ( + SELECT DISTINCT b.workerFk + FROM vn.calendar c + JOIN vn.absenceType ab ON ab.id = c.dayOffTypeFk + JOIN vn.business b ON b.id = c.businessFk + WHERE ab.`code` = 'maternity' + AND dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + )maternity ON maternity.workerFk = w.id + WHERE t.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND d.name IN ('REPARTO') + AND wd.workerFk IS NULL + GROUP BY b.workerFK +SELECT + COUNT(*) workers, + IFNULL(wc.deliveryManAdjustment, 0) deliveryManAdjustment, + workcenterName, + SUM(amount)/ (COUNT(*) + IFNULL(wc.deliveryManAdjustment, 0)) amountAVG + FROM (SELECT + w.id, + b.workcenterFk, + wc.name workcenterName, + sub.amount + IFNULL(mainlineDelivered.amount, 0) amount + FROM business b + JOIN worker w ON w.id = b.workerFk + JOIN time t ON t.dated BETWEEN b.started AND IFNULL(b.ended,1()) + JOIN department AS d ON d.id = b.departmentFk + JOIN workCenter wc ON wc.id = b.workcenterFk + JOIN routeConfig rc ON TRUE + LEFT JOIN workerDistributionCategory wd ON wd.workerFk = b.workerFk + JOIN ( + SELECT workerFk, + SUM(rc.yearlyKm) + SUM(rc.yearlyM3) amount + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + WHERE r.created BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + GROUP BY r.workerFk + )sub ON sub.workerFk = w.id + LEFT JOIN ( + SELECT + rc.workerFk, + COUNT(*) * rco.mainlineDelivered amount + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + JOIN vn.time t ON t.dated = rc.dated + JOIN routeConfig rco + JOIN vn.business b ON t.dated BETWEEN b.started + AND IFNULL(b.ended, CURDATE()) AND b.workerBusinessProfessionalCategoryFk = rco.truckerBusinessProfessionalCategoryFk + AND rc.workerFk = b.workerFk + WHERE rc.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND ra.isMainlineDelivered + GROUP BY rc.workerFk + )mainlineDelivered ON mainlineDelivered.workerFk = w.id + LEFT JOIN ( + SELECT DISTINCT b.workerFk + FROM vn.calendar c + JOIN vn.absenceType ab ON ab.id = c.dayOffTypeFk + JOIN vn.business b ON b.id = c.businessFk + WHERE ab.`code` = 'maternity' + AND dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + )maternity ON maternity.workerFk = w.id + WHERE t.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND d.name IN ('REPARTO') + AND wd.workerFk IS NULL + AND maternity.workerFk IS NULL + GROUP BY b.workerFK + HAVING COUNT(*) >= DATEDIFF(1(), CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01')) + ) sub2 + JOIN vn.workCenter wc ON wc.id = sub2.workcenterFk + GROUP BY workcenterFk +SELECT r.id 'Id Ruta', + a.name Agencia, + DATE_FORMAT(r.created, "%d/%m/%Y") Fecha, + r.m3, + IF(sub.category = '1', rc.cat5m3, rc.cat4m3) '€' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + LEFT JOIN vn.agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vn.agency a ON a.id = am.agencyFk + JOIN (SELECT DISTINCT t.dated, wdc.category, wl.workCenterFk + FROM vn.time t + JOIN workerLabour wl ON t.dated BETWEEN wl.started AND IFNULL(wl.ended, 1()) + JOIN department d ON d.id = wl.departmentFk + JOIN workerDistributionCategory wdc ON wdc.workerFk = wl.workerFk + WHERE wl.workerFk = '1' + AND d.name = 'REPARTO' + AND t.dated BETWEEN 1() AND 1() + ) sub ON sub.workCenterFk = r.commissionWorkcenterFk AND r.created = sub.dated +SELECT SUM(IF(cat5m3 IS NULL,0,1)) 'Rutas', + SUM(IF(cat5m3 IS NULL,0, r.m3)) 'M3', + CAST(SUM(IF(sub.category = 1, cat5m3, cat4m3)) AS DECIMAL(10,2)) '€' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN (SELECT DISTINCT t.dated, wdc.category, wl.workCenterFk + FROM vn.time t + JOIN workerLabour wl ON t.dated BETWEEN wl.started AND IFNULL(wl.ended, 1()) + JOIN department d ON d.id = wl.departmentFk + JOIN workerDistributionCategory wdc ON wdc.workerFk = wl.workerFk + WHERE wl.workerFk = '1' + AND d.name = 'REPARTO' + AND t.dated BETWEEN 1() AND 1() + ) sub ON sub.workCenterFk = r.commissionWorkcenterFk AND r.created = sub.dated + JOIN vn.routeConfig co + + +SELECT COUNT(*) routes, + SUM(IFNULL(rc.m3, 0)) freelanceM3, + CAST(SUM(IFNULL(rc.freelanceYearlyM3, 0)) / routers.total AS DECIMAL (10,2)) commissionYearlyFreelanceM3, + IF(sub.category = 1, co.rateCat5, co.rateCat4) * deliveryMan.amountAVG commissionYearlyDeliveryManAvg, + CAST(SUM(IFNULL(rc.freelanceYearlyM3, 0)) / routers.total AS DECIMAL (10,2)) + + (IF(sub.category = 1, co.rateCat5, co.rateCat4) * deliveryMan.amountAVG) totalCommissionYearly + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN ( + SELECT DISTINCT t.dated, wdc.category, wl.workCenterFk + FROM vn.time t + JOIN workerLabour wl ON t.dated BETWEEN wl.started AND IFNULL(wl.ended, 1()) + JOIN department d ON d.id = wl.departmentFk + JOIN workerDistributionCategory wdc ON wdc.workerFk = wl.workerFk + WHERE wl.workerFk = '1' + AND d.name = 'REPARTO' + AND t.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + ) sub ON sub.workCenterFk = r.commissionWorkcenterFk AND r.created = sub.dated + JOIN vn.routeConfig co + JOIN( + SELECT SUM(amount)/ (COUNT(*) + IFNULL(deliveryManAdjustment, 0)) amountAVG + FROM (SELECT + w.id, + b.workcenterFk, + wc.deliveryManAdjustment, + sub.amount + IFNULL(mainlineDelivered.amount, 0) amount + FROM business b + JOIN worker w ON w.id = b.workerFk + JOIN time t ON t.dated BETWEEN b.started AND IFNULL(b.ended,1()) + JOIN department AS d ON d.id = b.departmentFk + JOIN workCenter wc ON wc.id = b.workcenterFk + JOIN routeConfig rc ON TRUE + LEFT JOIN workerDistributionCategory wd ON wd.workerFk = b.workerFk + JOIN ( + SELECT workerFk, + SUM(rc.yearlyKm) + SUM(rc.yearlyM3) amount + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + WHERE r.created BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + GROUP BY r.workerFk + )sub ON sub.workerFk = w.id + LEFT JOIN ( + SELECT + rc.workerFk, + COUNT(*) * rco.mainlineDelivered amount + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + JOIN vn.time t ON t.dated = rc.dated + JOIN routeConfig rco + JOIN vn.business b ON t.dated BETWEEN b.started + AND IFNULL(b.ended, CURDATE()) AND b.workerBusinessProfessionalCategoryFk = rco.truckerBusinessProfessionalCategoryFk + AND rc.workerFk = b.workerFk + WHERE rc.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND ra.isMainlineDelivered + GROUP BY rc.workerFk + )mainlineDelivered ON mainlineDelivered.workerFk = w.id + LEFT JOIN ( + SELECT DISTINCT b.workerFk + FROM vn.calendar c + JOIN vn.absenceType ab ON ab.id = c.dayOffTypeFk + JOIN vn.business b ON b.id = c.businessFk + WHERE ab.`code` = 'maternity' + AND dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + )maternity ON maternity.workerFk = w.id + WHERE t.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND d.name IN ('REPARTO') + AND wd.workerFk IS NULL + AND maternity.workerFk IS NULL + AND wc.name = '1' + GROUP BY b.workerFK + HAVING COUNT(*) >= DATEDIFF(1(), CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01')) + ) sub2 + ) deliveryMan + JOIN ( + SELECT COUNT(DISTINCT w.workerFk) total + FROM workerDistributionCategory w + JOIN business b ON b.workerFk = w.workerFk + JOIN workCenter wc ON wc.id = b.workCenterFk + WHERE wc.name ='1' + AND (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) + AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + )routers + + WHERE freelanceYearlyM3 +SELECT DISTINCT wc.name + FROM workerLabour wl + JOIN (SELECT MAX(businessFk) businessFk + FROM workerLabour wl + JOIN vn.department AS d ON d.id = wl.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + AND d.name IN ('REPARTO') + GROUP BY wl.workerFK)sub ON sub.businessFk = wl.businessFk + JOIN workCenter wc ON wc.id = wl.workCenterFk + SELECT CONCAT( b.workerFk, ' - ', w.firstName, ' ', w.lastname) + FROM business b + JOIN (SELECT MAX(b.id) businessFk + FROM business b + JOIN vn.department AS d ON d.id = b.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + AND d.name IN ('REPARTO') + GROUP BY b.workerFK)sub ON sub.businessFk = b.id + JOIN worker w ON w.id = b.workerFk + JOIN workCenter wc ON wc.id = b.workCenterFk + LEFT JOIN workerDistributionCategory wd ON wd.workerFk = b.workerFk + WHERE wc.name ='1' + AND wd.workerFk + ORDER BY w.firstName, w.lastname + +SELECT w.id + FROM worker w + WHERE '1' = CONCAT( w.id, ' - ', w.firstName, ' ', w.lastname) + + + + SELECT sub.clientFk Id, + sub.name Cliente, + CAST(IFNULL(portfolioWeight.amount, 0) AS DECIMAL(10,2)) Peso, + CAST(IFNULL(currentSale.amount, 0) AS DECIMAL(10,2)) currentSale, + CAST((IFNULL(currentSale.amount,0)) - (IFNULL(portfolioWeight.amount,0)) AS DECIMAL(10,2)) salesIncrease, + CAST((((IFNULL(currentSale.amount,0)) / (IFNULL(portfolioWeight.amount,1)))-1) * 100 AS DECIMAL(10,2)) salesIncreasePercentage + FROM (SELECT c.id clientFk, c.name + FROM vn.client c + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name = '1' ) sub + LEFT JOIN(SELECT c.id clientFk, + SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name ='1' + AND s.dated BETWEEN 1() AND 1() + GROUP BY c.id + )currentSale ON currentSale.clientFk = sub.clientFk + LEFT JOIN ( + SELECT c.id clientFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY c.id + )portfolioWeight ON portfolioWeight.clientFk = currentSale.clientFk; +SELECT u.name + FROM account.user u + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = u.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + WHERE u.name IN ('ruben','ismaelalcolea') +SELECT sub.clientFk 'Id', + sub.name 'Cliente', + CAST(IFNULL(portfolioWeight.amount, 0) AS DECIMAL(10,2)) 'Peso Cartera', + CAST(IFNULL(currentSale.amount, 0) AS DECIMAL(10,2)) 'Venta hasta Hoy', + CAST((IFNULL(currentSale.amount,0)) - (IFNULL(portfolioWeight.amount,0)) AS DECIMAL(10,2)) 'Incremento de ventas (€)', + CAST((((IFNULL(currentSale.amount,0)) / (IFNULL(portfolioWeight.amount,1)))-1) * 100 AS DECIMAL(10,2)) 'Incremento de ventas (%)' + FROM (SELECT c.id clientFk, c.name + FROM vn.client c + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name = '1' ) sub + LEFT JOIN(SELECT c.id clientFk, + SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name = '1' + AND s.dated BETWEEN 1() AND 1() + GROUP BY c.id + )currentSale ON currentSale.clientFk = sub.clientFk + LEFT JOIN ( + SELECT c.id clientFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY c.id + )portfolioWeight ON portfolioWeight.clientFk = currentSale.clientFk; +SELECT u.name + FROM account.user u + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = u.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + WHERE u.name IN ('ruben','ismaelalcolea') +SELECT + SUM(IF (invoiced >= 10000, TRUE, FALSE)) '>10.000€', + SUM(IF (invoiced BETWEEN 7000 AND 9999, TRUE, FALSE)) '7.000 a 10.000', + SUM(IF (invoiced BETWEEN 5000 AND 6999, TRUE, FALSE)) '5.000 a 7.000', + SUM(IF (invoiced BETWEEN 3000 AND 4999, TRUE, FALSE)) '3.000 a 5.000', + SUM(IF (invoiced BETWEEN 1000 AND 2999, TRUE, FALSE)) '1.000 a 3.000', + SUM(IF (invoiced BETWEEN 500 AND 999, TRUE, FALSE)) '500 a 1.000', + SUM(IF (invoiced BETWEEN 300 AND 499, TRUE, FALSE)) '300 a 500', + SUM(IF (invoiced <= 299, TRUE, FALSE)) '<300' +FROM bs.clientAnnualConsumption +SELECT + SUM(IF (invoiced >= 10000, TRUE, FALSE)) '>10.000€', + SUM(IF (invoiced BETWEEN 7000 AND 9999, TRUE, FALSE)) '7.000 a 10.000', + SUM(IF (invoiced BETWEEN 5000 AND 6999, TRUE, FALSE)) '5.000 a 7.000', + SUM(IF (invoiced BETWEEN 3000 AND 4999, TRUE, FALSE)) '3.000 a 5.000', + SUM(IF (invoiced BETWEEN 1000 AND 2999, TRUE, FALSE)) '1.000 a 3.000', + SUM(IF (invoiced BETWEEN 500 AND 999, TRUE, FALSE)) '500 a 1.000', + SUM(IF (invoiced BETWEEN 300 AND 499, TRUE, FALSE)) '300 a 500', + SUM(IF (invoiced <= 299, TRUE, FALSE)) '<300' +FROM bs.clientAnnualConsumption cc + JOIN vn.client c ON c.id = cc.clientFk +WHERE c.businessTypeFk IN ('florist','events') +SELECT salesPersonCode, + SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 1, 0)) peques, + SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 0, 1)) NOpeques, + ((SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 1, 0)))/SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 0, 1)))*100 '% Pequeños' + FROM (SELECT t.addressFk, + SUM(sv.litros) litros, + t.totalWithoutVat, + w.code salesPersonCode + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + WHERE 1(t.shipped) + AND dm.code IN('AGENCY','DELIVERY','PICKUP') + GROUP BY t.addressFk) sub + JOIN vn.volumeConfig vc + GROUP BY salesPersonCode; +SELECT total-(small+little+mini) '>75L', small '50-75L', little '25-50L', mini '1-25L' + FROM(SELECT + COUNT(*) total, + SUM(IF(sub.volume >= 50 AND sub.volume < 75 AND sub.import < 50, TRUE, FALSE)) small, + SUM(IF(sub.volume >= 25 AND sub.volume < 50 AND sub.import < 50, TRUE, FALSE)) little, + SUM(IF(sub.volume >0 AND sub.volume < 25 AND sub.import < 50, TRUE, FALSE)) mini + FROM (SELECT t.id, SUM(sv.litros) volume, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub)sub2 +SELECT total-(small+little+mini) '>75L', small '50-75L', little '25-50L', mini '1-25L' + FROM(SELECT + COUNT(*) total, + SUM(IF(sub.volume >= 50 AND sub.volume < 75, TRUE, FALSE)) small, + SUM(IF(sub.volume >= 25 AND sub.volume < 50, TRUE, FALSE)) little, + SUM(IF(sub.volume >0 AND sub.volume < 25, TRUE, FALSE)) mini + FROM (SELECT t.id, SUM(sv.litros) volume + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub)sub2 +SELECT firstname AS 'Nombre', lastname AS 'Apellidos', sector AS 'Sector', SUM(ticketCount) AS 'Total tickets', SUM(saleCount) AS 'Total lineas', SUM(volume) AS 'M3', SUM(hourWorked) AS 'Total horas', SUM(volume) / SUM(hourWorked) AS 'M3Hora' +FROM vn.sectorProductivity +WHERE dated BETWEEN 1() AND 1() AND sector = '1' +GROUP BY workerFk, sector +ORDER BY firstname; + +SELECT fec time, m3Listos, m3Pendientes +FROM vn.sectorPreviaVolume +WHERE fec BETWEEN 1() AND 1() +GROUP BY sector, fec; + +SELECT sector +FROM vn.sectorProductivity; +SELECT fecha time, sum(importe) +FROM bs.compradores_evolution +GROUP BY fecha + +SELECT SUM(IF(YEAR(dated) = YEAR(CURDATE()) - 2, sale, 0)) '1', + SUM(IF(YEAR(dated) = YEAR(CURDATE()) - 1, sale, 0)) '1', + SUM(IF(YEAR(dated) = YEAR(CURDATE()), sale, 0)) '1 ' + FROM bs.salesByItemTypeDay + WHERE YEAR(dated) > YEAR(CURDATE()) - 3 + AND WEEK(dated) < WEEK(CURDATE()) +SELECT SUM(IF(YEAR(sbi.dated) = YEAR(CURDATE()) - 2, sbi.sale, 0)) '1', + SUM(IF(YEAR(sbi.dated) = YEAR(CURDATE()) - 1, sbi.sale, 0)) '1', + SUM(IF(YEAR(sbi.dated) = YEAR(CURDATE()), sbi.sale, 0)) '1 ' + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE WEEK(sbi.dated) = WEEK(CURDATE()) +SELECT w.code, + SUM(sbi.sale) + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE sbi.dated <= CURDATE() - INTERVAL 2 YEAR + AND YEAR(sbi.dated) = YEAR(CURDATE() - INTERVAL 2 YEAR) + AND w.code IN('DAV','EVA','JCA','JFP','JVF','NAD','NOE','RIC','TON') + GROUP BY w.code +SELECT w.code, + SUM(sbi.sale) + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE sbi.dated <= CURDATE() + AND YEAR(sbi.dated) = YEAR(CURDATE()) + AND w.code IN('DAV','EVA','JCA','JFP','JVF','NAD','NOE','RIC','TON') + GROUP BY w.code +SELECT w.code, + SUM(sbi.sale) + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE sbi.dated <= CURDATE() - INTERVAL 1 YEAR + AND YEAR(sbi.dated) = YEAR(CURDATE() - INTERVAL 1 YEAR) + AND w.code IN('DAV','EVA','JCA','JFP','JVF','NAD','NOE','RIC','TON') + GROUP BY w.code +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'JCA' + GROUP BY fecha + +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'DAV' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'FRN' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'JOR' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'JMP' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'JVF' + GROUP BY fecha +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'JCA' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'DAV' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'FRN' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'JOR' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'JVF' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'JMP' + GROUP BY MONTH(sbi.dated) +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'TON' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'NAD' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'NOE' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'LOM' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'JFP' + GROUP BY fecha +SELECT fecha time, + SUM(importe) + FROM bs.compradores_evolution ce + JOIN vn.worker w ON w.id = ce.Id_Trabajador + WHERE w.code = 'EVA' + GROUP BY fecha +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'TON' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'NAD' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'NOE' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'LOM' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'JFP' + GROUP BY MONTH(sbi.dated) +SELECT MAX(sbi.dated), + CAST(SUM(sbi.marginComponent)/SUM(sbi.sale) AS decimal(5,2)) Margen + FROM bs.salesByItemTypeDay sbi + JOIN vn.itemType it ON it.id = sbi.itemTypeFk + JOIN vn.worker w ON w.id = it.workerFk + WHERE w.code = 'EVA' + GROUP BY MONTH(sbi.dated) +SELECT YEAR(CURDATE()) +SELECT YEAR(CURDATE()) - 1 +SELECT YEAR(CURDATE()) - 2 +SELECT Fecha time, + SUM(Greuge ) Greuge, + SUM(Fosil) Fosil, + SUM(Ventas) Ventas, + SUM(Greuge) * 100 / SUM(Ventas) * 100 'Tarifa Media', + SUM(Recobro) Recobro + FROM bi.Greuge_Evolution + GROUP BY Fecha; +SELECT Fecha time, + SUM(Greuge ) Greuge, + SUM(Fosil) Fosil, + SUM(Ventas) Ventas, + SUM(Greuge) * 100 / SUM(Ventas) * 100 'Tarifa Media', + SUM(Recobro) Recobro, + sub.Morosidad + FROM bi.Greuge_Evolution ge + JOIN (SELECT ABS(SUM(amount)) Morosidad, + created + FROM bs.defaulter + GROUP BY created) sub ON sub.created = ge.Fecha + GROUP BY Fecha; +SELECT dated time, SUM(amount) + FROM bs.salesPersonEvolution + WHERE 1(dated) + GROUP BY dated + +SELECT s.dated time, sum(s.amount) CLAUDI + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO CLAUDI' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) MIRGIR + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO MIRIAM MAR' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) JVALLES + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO J VALLES' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) JBROCAL + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO J BROCAL' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) ELENA + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO ELENA BASCUNANA' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT s.dated time, sum(s.amount) CZAMBRANO + FROM bs.salesPersonEvolution s + JOIN account.user u ON u.id = s.salesPersonFk + JOIN vn.workerDepartment w ON w.workerFk = u.id + JOIN vn.department d On d.id = w.departmentFk + WHERE d.name='EQUIPO C ZAMBRANO' AND s.dated BETWEEN 1() AND 1() + GROUP BY s.dated + ORDER BY s.dated + +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT d.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' AND u.name = '1' +SELECT + + SUM(month1) Enero, + SUM(month2) Febrero, + SUM(month3) Marzo, + SUM(month4) Abril, + SUM(month5) Mayo, + SUM(month6) Junio, + SUM(month7) Julio, + SUM(month8) Agosto, + SUM(month9) Septiembre, + SUM(month10) Octubre, + SUM(month11) Noviembre, + SUM(month12) Diciembre + FROM (SELECT u.id userFk, + u.name userName, + SUM(i.amount) total, + SUM(IF(MONTH(i.issued) = 1, i.amount, 0)) month1, + SUM(IF(MONTH(i.issued) = 2, i.amount, 0)) month2, + SUM(IF(MONTH(i.issued) = 3, i.amount, 0)) month3, + SUM(IF(MONTH(i.issued) = 4, i.amount, 0)) month4, + SUM(IF(MONTH(i.issued) = 5, i.amount, 0)) month5, + SUM(IF(MONTH(i.issued) = 6, i.amount, 0)) month6, + SUM(IF(MONTH(i.issued) = 7, i.amount, 0)) month7, + SUM(IF(MONTH(i.issued) = 8, i.amount, 0)) month8, + SUM(IF(MONTH(i.issued) = 9, i.amount, 0)) month9, + SUM(IF(MONTH(i.issued) = 10, i.amount, 0)) month10, + SUM(IF(MONTH(i.issued) = 11, i.amount, 0)) month11, + SUM(IF(MONTH(i.issued) = 12, i.amount, 0)) month12 + FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + JOIN account.user u ON u.id =c.salesPersonFk + WHERE 1(i.issued) AND u.name = '1' + GROUP BY userFk, MONTH(i.issued) + )sub + + GROUP BY sub.userFk + +SELECT u.name +FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + JOIN account.user u ON u.id =c.salesPersonFk + WHERE u.name NOT IN ('pako','alicia','david','juanvi','enric','salva','sandra','susana','jsaez','jordi','ralvarez','jandreu','antoniomp','jgallego','eva','juanma','elaine','toni') +SELECT + clientFk 'ID Cliente', + socialName 'Razón Social', + description 'Tipo de negocio', + name 'Contacto', + created 'Fecha creación', + SUM(total) 'Facturación Anual', + SUM(month1) Enero, + SUM(month2) Febrero, + SUM(month3) Marzo, + SUM(month4) Abril, + SUM(month5) Mayo, + SUM(month6) Junio, + SUM(month7) Julio, + SUM(month8) Agosto, + SUM(month9) Septiembre, + SUM(month10) Octubre, + SUM(month11) Noviembre, + SUM(month12) Diciembre + FROM (SELECT i.clientFk, + bt.description, + cc.name, + c.socialName, + c.created, + SUM(i.amount) total, + SUM(IF(MONTH(i.issued) = 1, i.amount, 0)) month1, + SUM(IF(MONTH(i.issued) = 2, i.amount, 0)) month2, + SUM(IF(MONTH(i.issued) = 3, i.amount, 0)) month3, + SUM(IF(MONTH(i.issued) = 4, i.amount, 0)) month4, + SUM(IF(MONTH(i.issued) = 5, i.amount, 0)) month5, + SUM(IF(MONTH(i.issued) = 6, i.amount, 0)) month6, + SUM(IF(MONTH(i.issued) = 7, i.amount, 0)) month7, + SUM(IF(MONTH(i.issued) = 8, i.amount, 0)) month8, + SUM(IF(MONTH(i.issued) = 9, i.amount, 0)) month9, + SUM(IF(MONTH(i.issued) = 10, i.amount, 0)) month10, + SUM(IF(MONTH(i.issued) = 11, i.amount, 0)) month11, + SUM(IF(MONTH(i.issued) = 12, i.amount, 0)) month12 + FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + JOIN vn.businessType bt ON bt.code = c.businessTypeFk + JOIN vn.contactChannel cc ON cc.id = c.contactChannelFk + WHERE 1(i.issued) + GROUP BY i.clientFk, MONTH(i.issued) + )sub + GROUP BY sub.clientFk +SELECT + description 'Tipo de negocio', + sum(numClientes) numClientes, + SUM(total) 'Facturación Anual', + CAST(SUM(total)/sum(numClientes) AS DECIMAL(10,0)) mediaConsumo, + SUM(month1) Enero, + SUM(month2) Febrero, + SUM(month3) Marzo, + SUM(month4) Abril, + SUM(month5) Mayo, + SUM(month6) Junio, + SUM(month7) Julio, + SUM(month8) Agosto, + SUM(month9) Septiembre, + SUM(month10) Octubre, + SUM(month11) Noviembre, + SUM(month12) Diciembre + FROM (SELECT i.clientFk, + bt.description, + count(distinct c.id) numClientes, + SUM(i.amount) total, + SUM(IF(MONTH(i.issued) = 1, i.amount, 0)) month1, + SUM(IF(MONTH(i.issued) = 2, i.amount, 0)) month2, + SUM(IF(MONTH(i.issued) = 3, i.amount, 0)) month3, + SUM(IF(MONTH(i.issued) = 4, i.amount, 0)) month4, + SUM(IF(MONTH(i.issued) = 5, i.amount, 0)) month5, + SUM(IF(MONTH(i.issued) = 6, i.amount, 0)) month6, + SUM(IF(MONTH(i.issued) = 7, i.amount, 0)) month7, + SUM(IF(MONTH(i.issued) = 8, i.amount, 0)) month8, + SUM(IF(MONTH(i.issued) = 9, i.amount, 0)) month9, + SUM(IF(MONTH(i.issued) = 10, i.amount, 0)) month10, + SUM(IF(MONTH(i.issued) = 11, i.amount, 0)) month11, + SUM(IF(MONTH(i.issued) = 12, i.amount, 0)) month12 + FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + LEFT JOIN vn.businessType bt ON bt.code = c.businessTypeFk + LEFT JOIN vn.contactChannel cc ON cc.id = c.contactChannelFk + WHERE 1(i.issued) + GROUP BY c.businessTypeFk + )sub + GROUP BY sub.clientFk +SELECT + pay 'Forma de pago', + SUM(total) 'Facturación Anual', + SUM(month1) Enero, + SUM(month2) Febrero, + SUM(month3) Marzo, + SUM(month4) Abril, + SUM(month5) Mayo, + SUM(month6) Junio, + SUM(month7) Julio, + SUM(month8) Agosto, + SUM(month9) Septiembre, + SUM(month10) Octubre, + SUM(month11) Noviembre, + SUM(month12) Diciembre + FROM (SELECT i.clientFk, + bt.description, + cc.name 'channel', + pm.name 'pay', + c.socialName, + c.created, + SUM(i.amount) total, + SUM(IF(MONTH(i.issued) = 1, i.amount, 0)) month1, + SUM(IF(MONTH(i.issued) = 2, i.amount, 0)) month2, + SUM(IF(MONTH(i.issued) = 3, i.amount, 0)) month3, + SUM(IF(MONTH(i.issued) = 4, i.amount, 0)) month4, + SUM(IF(MONTH(i.issued) = 5, i.amount, 0)) month5, + SUM(IF(MONTH(i.issued) = 6, i.amount, 0)) month6, + SUM(IF(MONTH(i.issued) = 7, i.amount, 0)) month7, + SUM(IF(MONTH(i.issued) = 8, i.amount, 0)) month8, + SUM(IF(MONTH(i.issued) = 9, i.amount, 0)) month9, + SUM(IF(MONTH(i.issued) = 10, i.amount, 0)) month10, + SUM(IF(MONTH(i.issued) = 11, i.amount, 0)) month11, + SUM(IF(MONTH(i.issued) = 12, i.amount, 0)) month12 + FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + JOIN vn.payMethod pm ON pm.id = c.payMethodFk + JOIN vn.businessType bt ON bt.code = c.businessTypeFk + JOIN vn.contactChannel cc ON cc.id = c.contactChannelFk + WHERE 1(i.issued) + GROUP BY c.payMethodFk + )sub + GROUP BY sub.clientFk +SELECT + name 'Medio de Contacto', + SUM(total) 'Facturación Anual', + SUM(month1) Enero, + SUM(month2) Febrero, + SUM(month3) Marzo, + SUM(month4) Abril, + SUM(month5) Mayo, + SUM(month6) Junio, + SUM(month7) Julio, + SUM(month8) Agosto, + SUM(month9) Septiembre, + SUM(month10) Octubre, + SUM(month11) Noviembre, + SUM(month12) Diciembre + FROM (SELECT i.clientFk, + cc.name, + SUM(i.amount) total, + SUM(IF(MONTH(i.issued) = 1, i.amount, 0)) month1, + SUM(IF(MONTH(i.issued) = 2, i.amount, 0)) month2, + SUM(IF(MONTH(i.issued) = 3, i.amount, 0)) month3, + SUM(IF(MONTH(i.issued) = 4, i.amount, 0)) month4, + SUM(IF(MONTH(i.issued) = 5, i.amount, 0)) month5, + SUM(IF(MONTH(i.issued) = 6, i.amount, 0)) month6, + SUM(IF(MONTH(i.issued) = 7, i.amount, 0)) month7, + SUM(IF(MONTH(i.issued) = 8, i.amount, 0)) month8, + SUM(IF(MONTH(i.issued) = 9, i.amount, 0)) month9, + SUM(IF(MONTH(i.issued) = 10, i.amount, 0)) month10, + SUM(IF(MONTH(i.issued) = 11, i.amount, 0)) month11, + SUM(IF(MONTH(i.issued) = 12, i.amount, 0)) month12 + FROM vn.invoiceOut i + JOIN vn.client c ON c.id = i.clientFk + JOIN vn.businessType bt ON bt.code = c.businessTypeFk + JOIN vn.contactChannel cc ON cc.id = c.contactChannelFk + WHERE 1(i.issued) + GROUP BY c.contactChannelFk + )sub + GROUP BY sub.clientFk +SELECT CONCAT(w.firstName, ' ', w.lastName) 'Nombre del Trabajador', + SUM(wmt.code = 'Ubication') Ubicación, + SUM(wmt.code = 'Inventory') Inventario, + SUM(wmt.code = 'Labelled') Etiquetado, + SUM(wmt.code = 'Parking') Parking, + SUM(wmt.code = 'AbusedMate') 'M. Material', + SUM(wmt.code = 'Input') Entrada, + COUNT(wmt.code) Total + FROM vn.workerMistake wm + LEFT JOIN vn.worker w ON w.id = wm.userFk + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = w.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + LEFT JOIN vn.workerMistakeType wmt ON wmt.code = wm.workerMistakeTypeFk + WHERE d.name = '1' + AND 1(wm.created) + GROUP BY w.id + ORDER BY w.firstName +Producción, Cámara +SELECT CONCAT(firstname, ' ', lastname) 'Nombre del Trabajador', + ticketNumber 'Total Tickets', + lineNumber 'Total Líneas', + error Errores, + 100 * error / ticketNumber Ratio, + volume m³, + hourStart 'Hora Inicio', + hourEnd 'Hora Fin' + FROM vn.errorProduction + WHERE rol = '1' + AND dated BETWEEN 1() AND 1() + ORDER BY ratio DESC, firstname; +SELECT CONCAT(firstname, ' ', lastname) 'Nombre del Trabajador', + SUM(ticketNumber) 'Total Tickets', + SUM(lineNumber) 'Total Líneas', + SUM(error) Errores, + 100 * SUM(error)/ SUM(ticketNumber) Ratio, + FLOOR(SUM(volume)) m³, + SUM(volume) / SUM(hourWorked) 'm³/Hora', + SUM(hourWorked) 'Total Horas' + FROM vn.errorProduction + WHERE rol = '1' + AND dated BETWEEN 1() AND 1() + AND hourWorked + GROUP BY userFk, rol + ORDER BY ratio DESC, firstname; +SELECT sub1.ticketFk 'Ticket', + CONCAT(sub1.firstName, ' ', sub1.lastName) 'Nombre del Trabajador', + sub1.workerFk, + DATE_FORMAT(DATE(sub1.created), '%d-%m-%Y') 'Fecha' + FROM ( + SELECT tt.ticketFk, tt.workerFk, w.firstName, w.lastName,tt.created + FROM vn.ticketTracking tt + JOIN vn.worker w ON w.id = tt.workerFk + WHERE tt.stateFk = 5 AND tt.created BETWEEN 1() AND 1() + ) sub1 + JOIN ( + SELECT tt.ticketFk, tt.workerFk + FROM vn.ticketTracking tt + WHERE tt.stateFk = 6 + GROUP BY tt.ticketFk + HAVING COUNT(DISTINCT tt.workerFk) < 2 + ) sub2 ON sub2.ticketFk = sub1.ticketFk AND sub2.workerFk = sub1.workerFk + ORDER BY DATE_FORMAT(sub1.created, '%Y-%m-%d') DESC; +Sacadores, Encajadores, Revisadores +SELECT t.number ID, + td.subject Asunto, + sub.respuestas Respuestas, + tp.priority_desc Prioridad, + u.name Autor, + ts.name Estado, + LEFT(t.`created`, 19) Creado + FROM osticket.ost_ticket t + JOIN osticket.ost_user u ON u.id = t.user_id + JOIN osticket.ost_staff s ON s.staff_id = t.staff_id + JOIN osticket.ost_ticket__cdata td ON td.ticket_id = t.ticket_id + JOIN osticket.ost_ticket_priority tp ON tp.priority_id = td.priority + JOIN osticket.ost_ticket_status ts ON ts.id = t.status_id + JOIN osticket.ost_thread ot ON ot.object_id = t.ticket_id + AND ot.object_type = "T" + JOIN (SELECT te.thread_id, COUNT(*) Respuestas + FROM ost_thread_entry te + WHERE te.editor IS NULL + GROUP BY te.thread_id) sub ON sub.thread_id = ot.id + WHERE s.username = '${__user.login}' + AND ts.name IN (1) + ORDER BY tp.priority_urgency ASC, + t.lastupdate DESC +SELECT t.number ID, + td.subject Asunto, + tp.priority_desc Prioridad, + u.name Autor + FROM osticket.ost_ticket t + JOIN osticket.ost_user u ON u.id = t.user_id + LEFT JOIN osticket.ost_staff s ON s.staff_id = t.staff_id + JOIN osticket.ost_ticket__cdata td ON td.ticket_id = t.ticket_id + JOIN osticket.ost_ticket_priority tp ON tp.priority_id = td.priority + JOIN osticket.ost_ticket_status ts ON ts.id = t.status_id + LEFT JOIN osticket.ost_department d ON d.id = t.dept_id + WHERE s.username IS NULL + AND d.name = "Informática" + AND ts.name = "Abierto" + AND t.team_id = 0 + AND t.ticket_pid IS NULL + ORDER BY t.created DESC +SELECT COUNT(*) "Tickets cerrados hoy:" + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + JOIN ost_ticket_status ots ON ot.status_id = ots.id + LEFT JOIN ost_staff otsf ON ot.staff_id = otsf.staff_id + WHERE otsf.username = '${__user.login}' + AND ots.state = 'closed' + AND ot.ticket_pid IS NULL + AND od.code = 'IT' + AND DATE(ot.closed) = CURDATE() +SELECT COUNT(*) "Tickets cerrados esta semana:" + FROM ost_ticket ot + JOIN ost_department od ON od.id = ot.dept_id + JOIN ost_ticket_status ots ON ot.status_id = ots.id + LEFT JOIN ost_staff otsf ON ot.staff_id = otsf.staff_id + WHERE otsf.username = '${__user.login}' + AND ots.state = 'closed' + AND ot.ticket_pid IS NULL + AND od.code = 'IT' + AND WEEK(ot.closed) = WEEK(CURDATE()) + AND YEAR(ot.closed) = YEAR(CURDATE()) +SELECT i.id "ID", + i.subject "Asunto", + CONCAT(sub2.value, '‍‍‍‍‍') "osTicket", + e."name" "Prioridad", + CAST(sub.hours AS DECIMAL(10,2)) "Dedic.", + i.estimated_hours "Estim.", + is2."name" "Estado", + CAST(i.created_on AS varchar) "Creado" + FROM redmine.public.issues i + JOIN redmine.public.users u ON u.id = i.assigned_to_id + JOIN redmine.public.issue_statuses is2 ON is2.id = i.status_id + JOIN redmine.public.enumerations e ON e.id = i.priority_id + LEFT JOIN (SELECT issue_id, SUM(hours) "hours" + FROM redmine.public.time_entries + GROUP BY issue_id) sub ON sub.issue_id = i.id + LEFT JOIN (SELECT customized_id, value + FROM custom_values cv + JOIN custom_fields cf ON cf.id = cv.custom_field_id + WHERE cf."name" = 'osTicket' + AND cv.value <> '') sub2 ON sub2.customized_id = i.id + WHERE u.login = '${__user.login}' + AND is2.name IN (1) + ORDER BY e."position" DESC, + i.id +SELECT 'Semana' "Nombre", + CAST(SUM(hours) AS DECIMAL(10,2)) "Horas Computadas", + NULL "Horas Asignadas" + FROM redmine.public.time_entries te + JOIN redmine.public.users u ON u.id = te.user_id + WHERE u.login = '${__user.login}' + AND DATE_PART('week', te.spent_on) = DATE_PART('week', now()) + AND DATE_PART('year', te.spent_on) = DATE_PART('year', now()) +UNION ALL +SELECT 'Asignadas', + NULL, + CASE + WHEN SUM(i.estimated_hours) - SUM(sub.hours) < 0 THEN NULL + ELSE CAST(SUM(i.estimated_hours) - SUM(sub.hours) AS DECIMAL(10, 2)) + END + FROM redmine.public.issues i + JOIN redmine.public.users u ON u.id = i.assigned_to_id + JOIN redmine.public.issue_statuses is2 ON is2.id = i.status_id + JOIN redmine.public.enumerations e ON e.id = i.priority_id + LEFT JOIN (SELECT issue_id, SUM(hours) "hours" + FROM redmine.public.time_entries + GROUP BY issue_id) sub ON sub.issue_id = i.id + WHERE u.login = '${__user.login}' + AND is2."name" IN ('Nueva', 'En progreso', 'Análisis') +SELECT SUM(te.hours) "Horas de Hoy" + FROM redmine.public.time_entries te + JOIN redmine.public.users u ON u.id = te.user_id + WHERE u.login = '${__user.login}' + AND DATE(te.spent_on) = date(now()) +SELECT name FROM osticket.ost_ticket_status +SELECT name FROM redmine.public.issue_statuses +SELECT total Total, + total-permanent Temporales, + permanent Fijos, + DATE(CONCAT(year,'-',month,'-01')) time + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'staff' + ORDER BY w.year , w.month +SELECT Mes, Año, Total,Total-Fijos Temporales, Fijos + FROM (SELECT @month:=DATE_FORMAT(@dated:=DATE(CONCAT(year,'-',month,'-01')) , '%M'), + CONCAT(UPPER(LEFT(@month, 1)), SUBSTRING(@month, 2)) Mes, + year Año, + @dated dated, + total Total, + permanent Fijos + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'staff' + ORDER BY w.year DESC , w.month DESC) sub +SELECT code , Total-Fijos Temporales, Fijos, Total + FROM (SELECT total Total, + permanent Fijos, + CASE w.code + WHEN 'staff' THEN 'Plantilla Anterior' + WHEN 'layoffs' THEN 'Bajas' + WHEN 'hiring' THEN 'Altas' + END code + FROM bs.workerLabourDataByMonth w + ORDER BY w.year DESC , w.month DESC) sub + LIMIT 3 +SELECT total Total, + total-permanent Temporales, + permanent Fijos, + DATE(CONCAT(year,'-',month,'-01')) time + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'hiring' + ORDER BY w.year , w.month +SELECT Mes, Año, Total,Total-Fijos Temporales, Fijos + FROM (SELECT @month:=DATE_FORMAT(@dated:=DATE(CONCAT(year,'-',month,'-01')) , '%M'), + CONCAT(UPPER(LEFT(@month, 1)), SUBSTRING(@month, 2)) Mes, + year Año, + @dated dated, + total Total, + permanent Fijos + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'hiring' + ORDER BY w.year DESC , w.month DESC) sub +SELECT Mes, Año, Total,Total-Fijos Temporales, Fijos + FROM (SELECT @month:=DATE_FORMAT(@dated:=DATE(CONCAT(year,'-',month,'-01')) , '%M'), + CONCAT(UPPER(LEFT(@month, 1)), SUBSTRING(@month, 2)) Mes, + year Año, + @dated dated, + total Total, + permanent Fijos + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'layoffs' + ORDER BY w.year DESC , w.month DESC) sub +SELECT total Total, + total-permanent Temporales, + permanent Fijos, + DATE(CONCAT(year,'-',month,'-01')) time + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'layoffs' + ORDER BY w.year , w.month +SELECT total Total, + total-permanent Temporales, + permanent Fijos, + DATE(CONCAT(year,'-',month,'-01')) time + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'disabled' + ORDER BY w.year , w.month +SELECT Mes, Año, Total,Total-Fijos Temporales, Fijos + FROM (SELECT @month:=DATE_FORMAT(@dated:=DATE(CONCAT(year,'-',month,'-01')) , '%M'), + CONCAT(UPPER(LEFT(@month, 1)), SUBSTRING(@month, 2)) Mes, + year Año, + @dated dated, + total Total, + permanent Fijos + FROM bs.workerLabourDataByMonth w + WHERE w.code = 'disabled' + ORDER BY w.year DESC , w.month DESC) sub +SELECT * FROM vn.claim + +SELECT * FROM vn.claimDevelopment + +SELECT * FROM vn.claimResponsible + +SELECT + SUM(rb.bultos) Bultos, + SUM(r.m3) m3, + SUM(rb.bultos * rb.practico) Práctico, + SUM(rb.bultos * rb.teorico) Teórico, + SUM(rb.bultos * rb.greuge) Greuge, + rb.Fecha time + FROM bi.rutasBoard rb + LEFT JOIN vn.agencyMode am ON rb.Id_Agencia = am.id + LEFT JOIN vn.route r ON rb.Id_Ruta = r.id + WHERE 1(rb.Fecha) + AND am.name = '1' + GROUP BY rb.Fecha; +SELECT + SUM(rb.bultos * rb.greuge) Greuge, + SUM(r.m3) m3, + SUM(rb.bultos * rb.practico) Práctico, + SUM(rb.teorico * rb.bultos) Teórico, + SUM(rb.bultos) Bultos, + rb.Fecha time + FROM bi.rutasBoard rb + JOIN vn.agencyMode am ON rb.Id_Agencia = am.id + LEFT JOIN vn.route r ON rb.Id_Ruta = r.id + WHERE 1(rb.Fecha) + AND am.name = '1' + GROUP BY rb.warehouse_id, + rb.month, + rb.year, + rb.Id_Agencia; +SELECT name FROM vn.agencyMode; +SELECT +c.ticketFk 'Ticket', +c.id 'claim', +-SUM(s.price * s.quantity) 'payedBack', +-SUM(((s.price * s.quantity) * (100 - s.discount) / 100) * (1-(5 - c.responsibility) / 4)) 'Greuge', +-SUM(((s.price * s.quantity) * (100 - s.discount) / 100) * (5 - c.responsibility) / 4) 'loses' +FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id + JOIN vn.sale s ON s.id = ce.saleFk + JOIN vn.client cl ON cl.id = c.clientFk +WHERE 1(s.created) +AND CONCAT(cl.socialName,' ', '(',cl.id,')') = '1' +GROUP BY c.id +select +CONCAT(w.firstName,' ',w.lastName,' ','(',w.code,')') 'Comercial' +FROM vn.client c +join vn.worker w on w.id = c.salesPersonFk +where w.id not in (103,418,763,1103,1250,1478,1731,3401,4022,22240,852,3366,7566,1696) +group by w.id +SELECT +CONCAT(cl.socialName,' ', '(',cl.id,')') 'client' +FROM bs.sale ss + JOIN vn.client cl ON cl.id = ss.clientFk + JOIN vn.worker w ON w.id = cl.salesPersonFk + JOIN ( SELECT + cl.socialName client, + -SUM(s.price * s.quantity) claimedMoney, + -SUM(((s.price * s.quantity) * (100 - s.discount) / 100) * (5 - c.responsibility) / 4) Loses, + -SUM(((s.price * s.quantity) * (100 - s.discount) / 100) * (1-(5 - c.responsibility) / 4)) Greuge + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id + JOIN vn.sale s ON s.id = ce.saleFk + JOIN vn.client cl ON cl.id = c.clientFk + JOIN vn.worker w on w.id = cl.salesPersonFk + WHERE 1(c.created) + AND CONCAT(w.firstName,' ',w.lastName,' ','(',w.code,')') = '1' + GROUP BY c.clientFk) claimed ON claimed.client = cl.socialName +WHERE 1(ss.dated) +AND CONCAT(w.firstName,' ',w.lastName,' ','(',w.code,')') = '1' +GROUP BY ss.clientFk +ORDER BY 'ratioClaimed' desc +SELECT +CONCAT(i.longName,'(',i.id,')') 'Artículo', +s.quantity 'Cantidad Vendida', +SUM(s.price * s.quantity) 'Importe vendido' +FROM vn.sale s + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.ticketWeekly tw ON tw.ticketFk = s.ticketFk + JOIN vn.worker w ON w.id = it.workerFk +WHERE 1(s.created) +AND CONCAT(w.firstName, ' ', w.lastName, ' (', w.`code`, ')') = '1' +GROUP BY i.id + +SELECT CONCAT(w.firstName, ' ', w.lastName, ' (', w.`code`, ')') Comprador +FROM vn.worker w +JOIN vn.buyer b on b.userFk = w.id +WHERE b.userFk not in (103,5432,4714,977,1906,3764,1103,3376) +SELECT it.name +FROM vn.itemType it +JOIN vn.worker w ON w.id = it.workerFk +where CONCAT(w.firstName, ' ', w.lastName, ' (', w.`code`, ')') = '1' +select +Concat(i2.longName,' ','(',i2.id,')') 'Artículo' +from bs.sale ss +join vn.sale s2 on s2.id = ss.saleFk +JOIN vn.ticket t on t.id = s2.ticketFk +join vn.item i2 on i2.id = s2.itemFk +join vn.itemType it2 on it2.id = i2.typeFK + join( + SELECT + - sum((s.price * s.quantity) * (5 - c.responsibility) / 4) 'Perdidas', + i.id 'codigo' + FROM vn.claim c + join vn.claimEnd ce on ce.claimFk = c.id + join vn.sale s on s.id = ce.saleFk + join vn.item i on i.id = s.itemFk + join vn.itemType it on it.id = i.typeFk + WHERE 1(c.ticketCreated) and it.name = '1' and not i.isFloramondo + Group by i.id + ) sub on sub.codigo = i2.id +WHERE 1(ss.dated) and it2.name = '1' and t.agencyModeFk <> 23 +AND NOT i2.isFloramondo +group by i2.id +SELECT i.subName +FROM vn.item i +WHERE i.subName in ('Inverposentos','TurFunza','Benchmark','San Juan') +SELECT +CONCAT(i.longName,' ','(',i.id,')') Artículo, +COunt(DISTINCT s.discount) Descuentos, +SUM((s.price * s.quantity) * s.discount / 100) Dinero +FROM vn.sale s +JOIN vn.item i ON i.id = s.itemFk +JOIN vn.ticket t ON t.id = s.ticketFk +WHERE 1(t.shipped) +AND t.clientFk not in (200,400) +AND s.discount +group by i.id +order by dinero DESC +SELECT +t.id 'Ticket', +s.quantity 'Tallos/Paquetes', +s.discount Descuento, +SUM((s.price * s.quantity) * s.discount / 100) 'Importe Descontado' +FROM vn.ticket t +JOIN vn.sale s ON s.ticketFk = t.id +JOIN vn.item i ON i.id = s.itemFk +WHERE 1(t.shipped) +AND CONCAT(i.longName,' ','(',i.id,')') = '1' +AND s.discount +GROUP BY t.id +SELECT +c.id Reclamacion, +-SUM(s.price * s.quantity) dinero, +u.name Comercial +FROM vn.claim c +JOIN account.user u ON u.id = c.workerFk +JOIN vn.claimEnd ce ON ce.claimFk = c.id +JOIN vn.sale s ON s.id = ce.saleFk +JOIN vn.claimDevelopment cd ON cd.claimFk = c.id +WHERE 1(s.created) AND cd.claimReasonFk = 26 +Group by c.id + + +SELECT c.id Recla, +- SUM(s.price * s.quantity) Importe +FROM vn.claim c +JOIN vn.claimEnd ce ON ce.claimFk = c.id +JOIN vn.sale s ON s.id = ce.saleFk +JOIN vn.claimDevelopment cd ON cd.claimFk = c.id +WHERE 1(s.created) AND cd.claimResponsibleFk = 15 +group by c.id + + + + +SELECT +CONCAT(i.longName,' ','(',i.id,')') Artículo +FROM vn.sale s +JOIN vn.item i ON i.id = s.itemFk +JOIN vn.ticket t ON t.id = s.ticketFk +WHERE 1(t.shipped) +AND t.clientFk not in (200,400) +AND s.discount + + +SELECT firstName, lastName, count(*) AS inocentes +FROM ( +SELECT t.id, t.nickname , w.firstName , w.lastName +FROM util.debug d +JOIN vn.ticket t ON t.id = d.value +JOIN vn.client c ON c.id = t.clientFk +JOIN vn.worker w ON w.id = c.salesPersonFk +WHERE d.variable = 'inocente') sub +GROUP BY firstName, lastName +ORDER BY inocentes DESC; + +SELECT sub.clientFk 'Id', + sub.name 'Cliente', + CAST(IFNULL(portfolioWeight.amount, 0) AS DECIMAL(10,2)) 'Peso Cartera', + CAST(IFNULL(currentSale.amount, 0) AS DECIMAL(10,2)) 'Venta hasta Hoy', + CAST((IFNULL(currentSale.amount,0)) - (IFNULL(portfolioWeight.amount,0)) AS DECIMAL(10,2)) 'Incremento de ventas (€)', + CAST((((IFNULL(currentSale.amount,0)) / (IFNULL(portfolioWeight.amount,1)))-1) * 100 AS DECIMAL(10,2)) 'Incremento de ventas (%)' + FROM (SELECT c.id clientFk, c.name + FROM vn.client c + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name = 'josemp' ) sub + LEFT JOIN(SELECT c.id clientFk, + SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE u.name = 'josemp' + AND s.dated BETWEEN 1() AND 1() + GROUP BY c.id + )currentSale ON currentSale.clientFk = sub.clientFk + LEFT JOIN ( + SELECT c.id clientFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY c.id + )portfolioWeight ON portfolioWeight.clientFk = currentSale.clientFk; + + +SELECT SUM(volume), u.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('pako','jfrau','juanvi','sandra','palomaft','ralvarez','salva','amparocm','jordi','chechu','fran','daniel','elaine','ineslj','nuriald','mluz','david','antoniomp','jstegarescu','mireia','miguelft','pacorico','raquelgc','ruben','pepe') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('bruno','anamariasa','anabc','miriamc','deborajd','gemmaem') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('aleixandresc','enrique','jsoria','nachom','sergio','tonycooper') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('cris','jbrocal','jmontoro','jvilar','manoli','mmar') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('cesteban','claudi','mballesterolmos','monicafaust','olgapc','rodrigo') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('debora','dsarrion','elena','germansd','jorge','lozano') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('clopez','javi','juanbau','mari','silverio') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('czambrano','jibanez','jvalles','miriam','mserrano') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT IFNULL(SUM(psl.printerModelFk = "ZD421"), 0) `Impresoras Normales` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerGeneric' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(SUM(psl.printerModelFk = "ZD500R"), 0) `Etiquetas RFID` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerLabel' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(SUM(psl.printerModelFk = "ZD500R"), 0) `Impresoras RFID` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerGeneric' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT psl.packingSiteFk Puesto, + hostFk Ordenador, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.printerModelFk = "ZD500R" + AND psl.typeErrorFk = "labelerGeneric" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Ordenador, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.printerModelFk = "ZD421" + AND psl.typeErrorFk = "labelerGeneric" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Ordenador, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.printerModelFk = "ZD500R" + AND psl.typeErrorFk = "labelerLabel" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT SUM(psl.printerModelFk = "ZD421" AND psl.typeErrorFk = "labelerGeneric") `Impresoras Normales`, + SUM(psl.printerModelFk = "ZD500R" AND psl.typeErrorFk = "labelerGeneric") `Impresoras RFID`, + SUM(psl.printerModelFk = "ZD500R" AND psl.typeErrorFk = "labelerLabel") `Etiquetas RFID`, + SUM(psl.printerModelFk IN ("ZD421", "ZD500R") AND psl.typeErrorFk IN ("labelerGeneric", "labelerLabel")) Total, + DATE(psl.creationDate) time + FROM vn.packingSiteLog psl + GROUP BY DATE(psl.creationDate) +SELECT date + FROM ( + SELECT concat(LEFT(creationDate, 10), ' (', COUNT(*), ')') date + FROM vn.packingSiteLog + WHERE date(creationDate) < CURDATE() + GROUP BY date(creationDate) + + UNION ALL + + SELECT "Hoy" date + + UNION ALL + + SELECT CONCAT("Todas", ' (', COUNT(*), ')') date + FROM vn.packingSiteLog) sub + ORDER BY date DESC +SELECT IFNULL(COUNT(*), 0) `Congeladas` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerFreeze' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(COUNT(*), 0) `Sin Ethernet` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerEthernetConnection' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(COUNT(*), 0) `No Calibra` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerCalibrate' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(COUNT(*), 0) `No USB` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerUsbConnection' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(COUNT(*), 0) `VOID IP` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerVoid' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT IFNULL(COUNT(*), 0) `Otros` + FROM vn.packingSiteLog psl + WHERE psl.typeErrorFk = 'labelerOther' + AND DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", curdate(), LEFT("1", 10))) +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerFreeze" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerEthernetConnection" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerCalibrate" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerUsbConnection" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerVoid" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT psl.packingSiteFk Puesto, + hostFk Host, + printerModelFk Modelo, + COUNT(*) Incidencias + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerOther" + GROUP BY hostFk + ORDER BY Incidencias DESC +SELECT SUM(psl.typeErrorFk = "labelerCalibrate") `Calibrar`, + SUM(psl.typeErrorFk = "labelerEthernetConnection") `Conexión Internet`, + SUM(psl.typeErrorFk = "labelerFreeze") `Congelada`, + SUM(psl.typeErrorFk = "labelerUsbConnection") `Conexion USB`, + SUM(psl.typeErrorFk = "labelerVoid") `VOID`, + SUM(psl.typeErrorFk = "labelerOther") `Otros`, + SUM(psl.typeErrorFk IN ("labelerCalibrate", "labelerEthernetConnection", "labelerFreeze", "labelerUsbConnection", "labelerVoid", "labelerOther")) Total, + DATE(psl.creationDate) time + FROM vn.packingSiteLog psl + GROUP BY DATE(psl.creationDate) +SELECT psl.packingSiteFk Puesto, + description Descripción + FROM vn.packingSiteLog psl + WHERE DATE(psl.creationDate) = IF(LEFT("1", 5) = "Todas", DATE(psl.creationDate), IF(LEFT("1", 3) = "Hoy", CURDATE(), LEFT("1", 10))) + AND psl.typeErrorFk = "labelerOther" + GROUP BY hostFk +SELECT date + FROM ( + SELECT concat(LEFT(creationDate, 10), ' (', COUNT(*), ')') date + FROM vn.packingSiteLog + WHERE date(creationDate) < CURDATE() + AND typeErrorFk IN ("labelerCalibrate", "labelerEthernetConnection", "labelerFreeze", "labelerUsbConnection", "labelerVoid", "labelerOther") + GROUP BY date(creationDate) + + UNION ALL + + SELECT "Hoy" date + + UNION ALL + + SELECT CONCAT("Todas", ' (', COUNT(*), ')') date + FROM vn.packingSiteLog + WHERE typeErrorFk IN ("labelerCalibrate", "labelerEthernetConnection", "labelerFreeze", "labelerUsbConnection", "labelerVoid", "labelerOther")) sub + ORDER BY date DESC + SELECT + (SELECT SUM(sv.volume) +FROM vn.saleVolume sv +JOIN vn.ticket tk on tk.id = sv.ticketFk +JOIN vn.client cl on cl.id = tk.clientFk +JOIN vn.clientType ct on ct.id = cl.clientTypeFk +WHERE YEAR(sv.shipped) = "1" -1 AND MONTH(sv.shipped) = "1" and ct.code = 'normal' ) "1", +(SELECT SUM(sv.volume) +FROM vn.saleVolume sv +JOIN vn.ticket tk on tk.id = sv.ticketFk +JOIN vn.client cl on cl.id = tk.clientFk +JOIN vn.clientType ct on ct.id = cl.clientTypeFk +WHERE YEAR(sv.shipped) = "1" AND MONTH(sv.shipped) = "1" and ct.code = 'normal') "1" +SELECT DISTINCT YEAR + FROM vn.`time` t + WHERE t.YEAR <= YEAR(CURDATE()) +1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 +SELECT 1 - 1 +SELECT w.code, + subCost.amount Tallos, + subCost.cost "Coste Teorico", + SUM(subSale.totalSaleValue) "Venta Neta", + SUM(subSale.totalSaleValue) - subCost.cost Beneficio, + (SUM(subSale.totalSaleValue) - subCost.cost) / SUM(subSale.totalSaleValue) * 100 "Margen %" + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN worker w ON w.id = it.workerFk + JOIN ticket t ON t.id = s.ticketFk + LEFT JOIN ( + SELECT s.id, + SUM(s.priceFixed) price, + COUNT(s.priceFixed ) priceCount, + s.priceFixed * s.quantity * (t.clientFk NOT IN (1)) totalSaleValue, + s.quantity + FROM sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN warehouse w ON w.id = t.warehouseFk + AND w.id = '1' + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + WHERE 1(t.shipped) + AND it.workerFk = 1 + GROUP BY s.id + ) subSale ON subSale.id = s.id + LEFT JOIN( + SELECT it.workerFk, + FLOOR(SUM(b.quantity * (b.buyingValue + b.freightValue + b.comissionValue + b.packageValue))) Cost, + SUM(b.quantity) amount, + SUM(b.quantity * (b.buyingValue + b.freightValue + b.comissionValue + b.packageValue)) / SUM(b.quantity) CosteMedio + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.supplier s ON s.id = e.supplierFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse wh ON wh.id = tr.warehouseInFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE NOT i.isFloramondo + AND wh.id = '1' + AND e.supplierFk NOT IN (1) + AND 1(tr.landed) + AND it.workerFk = 1 + GROUP BY it.workerFk + ) subCost ON subCost.workerFk = w.id + WHERE 1(t.shipped) + AND it.workerFk = 1 + GROUP BY it.workerFk +SELECT it.code Tipo, + it.name Nombre, + COUNT(DISTINCT i.id) Referencias, + SUM(s.quantity) Tallos, + CAST(SUM(sub.price)/SUM(priceCount) AS DECIMAL(10,2)) 'Precio Medio', + CAST(SUM(totalSaleValue) AS DECIMAL(10,2)) Venta + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + AND it.workerFk = 1 + JOIN worker w ON w.id = it.workerFk + JOIN ticket t ON t.id = s.ticketFk + JOIN ( + SELECT s.id, + SUM(s.priceFixed) price, + COUNT(s.priceFixed) priceCount, + s.priceFixed * s.quantity * (t.clientFk NOT IN (1)) totalSaleValue + FROM sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN warehouse w ON w.id = t.warehouseFk + AND w.id = '1' + JOIN vn.client c ON t.clientFk = c.id + AND c.typeFk IN ('Normal','handMaking') + WHERE 1(t.shipped) + GROUP BY s.id + ) sub ON sub.id = s.id + WHERE 1(t.shipped) + GROUP BY it.id + ORDER BY it.name; +SELECT i.id 'Item_id', + it.code Tipo, + i.name Articulo, + SUM(s.quantity) Tallos, + CAST(SUM(sub.price)/SUM(priceCount) AS DECIMAL(10,2)) Price, + CAST(SUM(totalSaleValue) AS DECIMAL(10,2)) Venta + FROM sale s + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + AND it.workerFk = 1 + JOIN worker w ON w.id = it.workerFk + JOIN ticket t ON t.id = s.ticketFk + JOIN ( + SELECT s.id, + SUM(s.priceFixed) price, + COUNT(s.priceFixed) priceCount, + s.priceFixed * s.quantity * (t.clientFk NOT IN (1)) totalSaleValue + FROM sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN warehouse w ON w.id = t.warehouseFk + AND w.id = '1' + JOIN vn.client c ON t.clientFk = c.id + AND c.typeFk IN ('Normal','handMaking') + WHERE 1(t.shipped) + GROUP BY s.id + ) sub ON sub.id = s.id + WHERE 1(t.shipped) + AND (1 IS NULL OR it.code IN (1)) + GROUP BY i.id + ORDER BY it.name; +SELECT + it.code Code, + it.name Nombre, + COUNT(DISTINCT i.id) Referencias, + SUM(b.quantity) Tallos, + SUM(b.quantity * (b.buyingValue + b.freightValue + b.comissionValue + b.packageValue)) / SUM(b.quantity) CosteMedio, + FLOOR(SUM(b.quantity * (b.buyingValue + b.freightValue + b.comissionValue + b.packageValue))) Coste + FROM buy b + JOIN entry e ON e.id = b.entryFk + JOIN supplier s ON s.id = e.supplierFk + JOIN travel tr ON tr.id = e.travelFk + JOIN warehouse wh ON wh.id = tr.warehouseInFk + JOIN item i ON i.id = b.itemFk + JOIN itemType it ON it.id = i.typeFk + WHERE NOT i.isFloramondo + AND wh.id = '1' + AND e.supplierFk NOT IN (1) + AND 1(tr.landed) + AND it.workerFk = '1' + GROUP BY it.code + ORDER BY it.name + +SELECT name __text, id __value FROM warehouse; +SELECT w.code __text, + w.id __value + FROM vn.worker w + LEFT JOIN account.user u ON u.id = w.id + LEFT JOIN account.role r ON u.role = r.id + WHERE r.name in ('buyer','buyerBoss'); +SELECT name __text, + id __value FROM vn.supplier; +SELECT name __text, + id __value FROM vn.client; +SELECT code FROM itemType WHERE ( 1 IS NULL OR workerFk = 1) + SELECT IFNULL(floor(sum(volume)),0) 'Volumen Total' + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE 1(sv.shipped) + AND w.code = '1' + AND NOT am.name = 'ABONO' +SELECT 1 Actual, + IF(SIGN(1 + 1)=1, 1 + 1, 0) Mínima +SELECT (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 5 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 4 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 3 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 2 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 1 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR)) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 1 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) 'Año atrás', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 2 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 3 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 4 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 5 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 6 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 7 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1' +SELECT 1 Actual, + IF(SIGN(1)=1, 1, 0) Mínima +SELECT (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 6 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 6 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 5 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 5 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 4 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 4 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 3 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 3 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 2 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 2 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() - INTERVAL 1 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() - INTERVAL 1 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE(), ' 00:00:00')) AND (SELECT CONCAT(CURDATE(), ' 23:59:59')) + AND w.name IN( 'Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND am.name <> 'ABONO') 'Hoy', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 1 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 1 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 2 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 2 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 3 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 3 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 4 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 4 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 5 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 5 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1', + (SELECT IFNULL(floor(sum(volume)),0) + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE sv.shipped BETWEEN (SELECT CONCAT(CURDATE() + INTERVAL 6 DAY, ' 00:00:00')) AND (SELECT CONCAT(CURDATE() + INTERVAL 6 DAY, ' 23:59:59')) + AND w.code = '1' + AND am.name <> 'ABONO') '1' +SELECT (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 5 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 4 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 3 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 2 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) - INTERVAL 1 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR)) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 1 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) 'Hoy', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 2 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 3 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 4 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 5 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 6 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1', + (SELECT * + FROM ( SELECT IFNULL(floor(sum(m3)),0) * 1.15 + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = (SELECT (CURDATE() - INTERVAL 1 YEAR) + INTERVAL 7 DAY) + AND w.code = '1' + UNION ALL + SELECT 0) sub + LIMIT 1) '1' +SELECT 1 Actual, + IF(SIGN(1)=1, 1, 0) Mínima + SELECT IF((IFNULL(floor(SUM(volume)),0) - sv2.Encajado) > 0, IFNULL(floor(SUM(volume)),0) - sv2.Encajado, 0) 'Restante:', + FLOOR(sv2.Encajado) 'Encajado:', + IF(((sv2.Encajado / IFNULL(floor(SUM(volume)),0)) * 100) < 100, ((sv2.Encajado / IFNULL(floor(SUM(volume)),0)) * 100), 100) 'Completado:', + NOW() time + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN ( SELECT SUM(IF (alertLevel >= 2, volume,0)) 'Encajado', + NOW() time + FROM vn.saleVolume sv + JOIN vn.sale s ON s.id = sv.saleFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(sv.shipped) + AND w.code = '1') sv2 + WHERE 1(sv.shipped) + AND w.code = '1' + AND NOT am.name = 'ABONO' +SELECT SUM(IF (alertLevel = 0 OR alertLevel IS NULL, 1, 0)) 'Libre', + SUM(IF (alertLevel = 1 AND ts.code <> 'CHECKED', 1, 0)) 'Preparando', + SUM(IF (alertLevel = 1 AND ts.code = 'CHECKED', 1, 0)) 'Revisado', + SUM(IF (alertLevel >= 2, 1, 0)) 'Encajado' + FROM vn.ticket t + JOIN vn.sale s on s.ticketFk = t.id + LEFT JOIN vn.ticketState ts ON t.id = ts.ticketFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND w.code = '1' +SELECT SUM(cm3) / 1000000 m3, + CONVERT(DATE_FORMAT(odbc_date, "%Y-%m-%d %H"), datetime) - INTERVAL 2 HOUR time + FROM `bs`.`packingSpeed` ps + JOIN vn.warehouse w ON w.id = ps.warehouse_id + WHERE w.code = '1' + AND cm3 + GROUP BY HOUR(odbc_date) +SELECT SUM(cm3) / 1000000 Media, + CONVERT(DATE_FORMAT(odbc_date, "%Y-%m-%d %H"), datetime) - INTERVAL 2 HOUR time + FROM `bs`.`packingSpeed` ps + JOIN vn.warehouse w ON w.id = ps.warehouse_id + WHERE w.code = '1' + AND cm3 + GROUP BY HOUR(odbc_date) +SELECT IF(1 = 0, '00:00',DATE_FORMAT(DATE_ADD(curtime(), INTERVAL (1 * 60) MINUTE), '%h:%i %p')) 'Hora fin a esa velocidad: ' +SELECT SUM(cm3) / 1000000 Pico, + CONVERT(DATE_FORMAT(odbc_date, "%Y-%m-%d %H"), datetime) - INTERVAL 2 HOUR time + FROM `bs`.`packingSpeed` ps + JOIN vn.warehouse w ON w.id = ps.warehouse_id + WHERE w.code = '1' + AND cm3 + GROUP BY HOUR(odbc_date) +SELECT SUM(IF (alertLevel = 0 AND i.itemPackingTypeFk = 'H' OR ISNULL(i.itemPackingTypeFk), volume,0)) 'Lib H', + SUM(IF (alertLevel = 0 AND i.itemPackingTypeFk = 'V', volume,0)) 'Lib V', + SUM(IF (alertLevel = 1 AND i.itemPackingTypeFk = 'H' AND ts.code <> 'CHECKED', volume,0)) 'Pre H', + SUM(IF (alertLevel = 1 AND i.itemPackingTypeFk = 'V' AND ts.code <> 'CHECKED', volume,0)) 'Pre V', + SUM(IF (alertLevel = 1 AND i.itemPackingTypeFk = 'H' AND ts.code = 'CHECKED', volume,0)) 'Rev H', + SUM(IF (alertLevel = 1 AND i.itemPackingTypeFk = 'V' AND ts.code = 'CHECKED', volume,0)) 'Rev V', + SUM(IF (alertLevel >= 2 AND i.itemPackingTypeFk = 'H', volume,0)) 'Enc H', + SUM(IF (alertLevel >= 2 AND i.itemPackingTypeFk = 'V', volume,0)) 'Enc V', + NOW() time + FROM vn.saleVolume sv + JOIN vn.sale s ON s.id = sv.saleFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(sv.shipped) +SELECT COUNT(DISTINCT collectionFk) 'Colecciones' + FROM vn.ticketCollection + WHERE 1(created) +SELECT IF(SIGN(1) = 1, CONCAT('La campaña comenzará en unos ', 1, ' días'), 'Estamos en campaña') DaysLeft, + IF(c.code="mothersDay", "Día de la Madre", IF(c.code="allSaints", "Día de Todos los Santos", IF(c.code="valentinesDay", "Día de San Valentin", 0))) Celebration + FROM vn.campaign c + WHERE CURDATE() < c.dated + ORDER BY dated + LIMIT 1 +SELECT COUNT(*) 'Expediciones' + FROM vn.expedition e + JOIN vn.ticket t ON t.id = e.ticketFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.ticketState ts ON ts.ticketFk = t.id + WHERE 1(e.created) + AND w.code = '1' + AND ts.alertLevel < 3 +SELECT COUNT(*) 'Clientes Web:' + FROM hedera.userSession + WHERE userVisitFk + AND lastUpdate > CURTIME() - INTERVAL 15 MINUTE +SELECT COUNT(DISTINCT c.id) 'Clientes: ' + FROM vn.client c + JOIN vn.ticket t ON t.clientFk = c.id + JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND ts.alertLevel < 3 + AND w.code = '1' +SELECT COUNT(*) 'Pedidos Web:' + FROM hedera.`order` + WHERE 1(date_make) + AND source_app = "WEB" +SELECT COUNT(DISTINCT(s.itemFk)) 'Artículos:' + FROM vn.ticketState ts + JOIN vn.ticket t ON t.id = ts.ticketFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND ts.alertLevel < 3 + AND w.code = '1' +SELECT COUNT(*) 'Lineas: ' + FROM vn.ticket t + join vn.sale s on s.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND w.code = '1' +SELECT SUM(s.quantity) 'Tallos:' + FROM vn.ticketState ts + JOIN vn.ticket t ON t.id = ts.ticketFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND w.code = '1' +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 1 DAY, 10)))) + +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 2 DAY, 10)))) +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 3 DAY, 10)))) +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 4 DAY, 10)))) +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 5 DAY, 10)))) +SELECT CONCAT('more_',DAYNAME((LEFT(CURDATE() + INTERVAL 6 DAY, 10)))) +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 1 DAY, 10)))) +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 2 DAY, 10)))) +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 4 DAY, 10)))) + +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 3 DAY, 10)))) +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 5 DAY, 10)))) +SELECT YEAR(CURDATE() - INTERVAL 1 YEAR) +SELECT CONCAT('less_',DAYNAME((LEFT(CURDATE() - INTERVAL 6 DAY, 10)))) + +SELECT YEAR(CURDATE()) +SELECT SUM(m3) +FROM ( SELECT IFNULL(CAST(SUM(IFNULL(p.volume, p.width * p.height * p.`depth`) / 1000000) AS DECIMAL(5, 1)), 0) m3 + FROM vn.warehouse w + LEFT JOIN vn.ticket t ON t.warehouseFk = w.id + LEFT JOIN vn.expedition e ON t.id = e.ticketFk + LEFT JOIN vn.packaging p ON p.id = e.packagingFk + WHERE e.created BETWEEN CURRENT_TIMESTAMP() - INTERVAL 1 HOUR AND CURRENT_TIMESTAMP() + AND w.code = '1' + AND e.itemPackingTypeFk = 'H' + UNION ALL + SELECT IFNULL(CAST(SUM(IFNULL(p.volume, p.width * p.height * p.`depth`) / 1000000) AS DECIMAL(5, 1)), 0) m3 + FROM vn.warehouse w + LEFT JOIN vn.ticket t ON t.warehouseFk = w.id + LEFT JOIN vn.expedition e ON t.id = e.ticketFk + LEFT JOIN vn.packaging p ON p.id = e.packagingFk + WHERE e.created BETWEEN CURRENT_TIMESTAMP() - INTERVAL 1 HOUR AND CURRENT_TIMESTAMP() + AND w.code = '1' + AND e.itemPackingTypeFk IS NULL) sub +SELECT FLOOR(SUM(sv.volume)/((HOUR('1') * 60 + MINUTE('1'))/60 - + (HOUR(NOW()) * 60 + MINUTE(NOW()))/60)) velocidadMinima + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.expedition e ON e.ticketFk = t.id + WHERE t.shipped BETWEEN CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59') + AND sv.itemPackingTypeFk IS NOT NULL + AND e.id IS NULL + AND sv.itemPackingTypeFk = 'H' +SELECT IFNULL(CAST(SUM(IFNULL(p.volume, p.width * p.height * p.`depth`) / 1000000) AS DECIMAL(5, 1)), 0) m3 + FROM vn.warehouse w + LEFT JOIN vn.ticket t ON t.warehouseFk = w.id + LEFT JOIN vn.expedition e ON t.id = e.ticketFk + LEFT JOIN vn.packaging p ON p.id = e.packagingFk + WHERE e.created BETWEEN CURRENT_TIMESTAMP() - INTERVAL 1 HOUR AND CURRENT_TIMESTAMP() + AND w.code = '1' + AND e.itemPackingTypeFk = 'V' +SELECT IFNULL(FLOOR(SUM(sv.volume)/((HOUR('1') * 60 + MINUTE('1'))/60 - + (HOUR(NOW()) * 60 + MINUTE(NOW()))/60)), 0) velocidadMinima + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + LEFT JOIN vn.expedition e ON e.ticketFk = t.id + WHERE t.shipped BETWEEN CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59') + AND sv.itemPackingTypeFk IS NOT NULL + AND e.id IS NULL + AND sv.itemPackingTypeFk = 'V' +SELECT 1 + 1 +SELECT IF(1= 0 + , 0 + , ( + SELECT + IFNULL(floor(SUM(volume)), 0) + FROM + vn.saleVolume sv + JOIN vn.ticketState ts ON + ts.ticketFk = sv.ticketFk AND ts.alertLevel < 2 + JOIN vn.ticket t ON + t.id = ts.ticketFk + JOIN vn.warehouse w ON + w.id = t.warehouseFk + JOIN vn.agencyMode am ON + am.id = t.agencyModeFk + WHERE 1(sv.shipped) + AND w.name IN( 'Algemesi') + AND am.name <> 'ABONO') / 1) +SELECT ABS(DATEDIFF(CURDATE(), DATE_ADD(dated, INTERVAL IF(c.code="mothersDay", -15, IF(c.code="allSaints", -20, IF(c.code="valentinesDay", -7, 0))) DAY))) DaysLeft + FROM vn.campaign c + WHERE CURDATE() < c.dated + ORDER BY dated + LIMIT 1 +SELECT MONTH(CURDATE()) +SELECT CONCAT(UPPER(LEFT(MONTHNAME(CURDATE()),1)),LOWER(SUBSTRING(MONTHNAME(CURDATE()),2,LENGTH(MONTHNAME(CURDATE()))))) +SELECT MAX(hourTheoretical) horaFinal FROM vn.zoneEstimatedDelivery +ALG +SELECT sum(volume), now() time + FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk + WHERE 1(sv.shipped) + AND warehouseFk = 60 + +SELECT z.id zona , z.name nombre, ze.hourTheoretical teorica, ze.hourEffective practica, ze.totalVolume volumen,ze.remainingVolume restante + FROM vn.zoneEstimatedDelivery ze + JOIN vn.zone z ON z.id = ze.zoneFk + ORDER BY teorica +SELECT sum(IF (alertLevel = 0, volume,0)) 'Libre', sum(IF (alertLevel = 1, volume,0)) 'Preparando', sum(IF (alertLevel >= 2, volume,0)) 'Encajado', now() time +FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk +WHERE 1(sv.shipped) + AND warehouseFk = 60 +SELECT sum(IF (alertLevel = 0, 1, 0)) 'Libre', sum(IF (alertLevel = 1, 1, 0)) 'Preparando', sum(IF (alertLevel = 2, 1, 0)) 'Encajado', now() time +FROM vn.ticketState ts + JOIN vn.ticket t ON t.id = ts.ticketFk +WHERE 1(t.shipped) + AND warehouseFk = 60 +SELECT sum(volume), now() time + FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk + WHERE 1(sv.shipped) + AND warehouseFk = 60 + +SELECT z.id zona , z.name nombre, ze.hourTheoretical teorica, ze.hourPractical practica, ze.totalVolume volumen,ze.remainingVolume restante + FROM bs.zone_ETD ze + JOIN vn.zone z ON z.id = ze.zoneFk + ORDER BY teorica +SELECT sum(IF (alertLevel = 0, volume,0)) 'Libre', sum(IF (alertLevel = 1, volume,0)) 'Preparando', sum(IF (alertLevel >= 2, volume,0)) 'Encajado', now() time +FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk +WHERE 1(sv.shipped) + AND warehouseFk = 60 +SELECT sum(IF (alertLevel = 0, 1, 0)) 'Libre', sum(IF (alertLevel = 1, 1, 0)) 'Preparando', sum(IF (alertLevel = 2, 1, 0)) 'Encajado', now() time +FROM vn.ticketState ts + JOIN vn.ticket t ON t.id = ts.ticketFk +WHERE 1(t.shipped) + AND warehouseFk = 60 +WITH tt AS ( +WITH + tSold AS ( + SELECT s.itemFk , SUM(s.quantity) quantity + FROM vn.sale s + JOIN vn.ticket t ON s.ticketFk = t.id + WHERE t.shipped BETWEEN '1' AND TIMESTAMPADD(HOUR, 23, '1') + AND t.warehouseFk = 60 + GROUP BY s.itemFk), + tStock AS ( + SELECT itemFk, sum(amount) amount + FROM ( + SELECT st.item_id itemFk, st.amount + FROM cache.stock st + WHERE st.warehouse_id = 60 + UNION ALL + SELECT s.itemFk, - s.quantity + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE t.shipped BETWEEN curdate() AND TIMESTAMPADD(HOUR,-1,'1') + AND t.warehouseFk = 60 + UNION ALL + SELECT b.itemFk, b.quantity + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + WHERE tr.landed BETWEEN curdate() AND '1' + AND tr.warehouseInFk = 60) sub + GROUP BY itemFk) + SELECT ts.itemFk, tk.amount stock, ts.quantity sold + FROM tStock tk + JOIN tSold ts ON ts.itemFk = tk.itemFk + WHERE tk.amount < ts.quantity) + SELECT * FROM tt; + + + + SELECT s.itemFk , SUM(s.quantity) quantity + FROM vn.sale s + JOIN vn.ticket t ON s.ticketFk = t.id + WHERE t.shipped BETWEEN '1' AND TIMESTAMPADD(HOUR, 23, '1') + AND t.warehouseFk = 60 + GROUP BY s.itemFk +SELECT DATE_FORMAT(dated,'%Y-%m-%d') FROM vn.time where dated >= curdate() +SELECT s.dated time, SUM(s.amount) '2023' + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() AND 1() + GROUP BY DAY(s.dated) + ORDER BY s.dated +SELECT s.dated + INTERVAL 1 YEAR time, SUM(s.amount) '2022' + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY DAY(s.dated) + ORDER BY s.dated +SELECT (((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE()))))*100) AS 'Transcurrido mes' + +SELECT (currentBuy.amount * 100) / goal.amount 'Porcentaje Objetivo HOY' + FROM ( + SELECT SUM(s.amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.worker w ON w.id = s.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + WHERE s.dated BETWEEN util.firstDayOfMonth(CURDATE()) AND CURDATE() + AND d.name LIKE 'EQUIPO %')currentBuy + JOIN ( + SELECT SUM(s.amount) * 1.35 amount + FROM bs.salesByclientSalesPerson s + JOIN vn.worker w ON w.id = s.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + WHERE s.dated BETWEEN util.firstDayOfMonth(CURDATE()- INTERVAL 1 YEAR) AND LAST_DAY(CURDATE()- INTERVAL 1 YEAR) + AND d.name LIKE 'EQUIPO %') goal +SELECT SUM(s.amount) 'VENTA 2022', + SUM(s.amount) * 1.35 'OBJETIVO' + FROM bs.salesByclientSalesPerson s + JOIN vn.worker w ON w.id = s.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + WHERE s.dated BETWEEN util.firstDayOfMonth(CURDATE()- INTERVAL 1 YEAR) AND LAST_DAY(CURDATE()- INTERVAL 1 YEAR) + AND d.name LIKE 'EQUIPO %' +SELECT SUM(s.quantity * sc.value) 'VENTA HASTA HOY 2023' + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + WHERE t.shipped BETWEEN 1() AND CURDATE() + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + +SELECT SUM(s.quantity * sc.value) 'VENTA MES TOTAL 2023' + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + WHERE t.shipped BETWEEN 1() AND 1() + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + +SELECT source_app, + sum(total), + date_make time +FROM hedera.`order` +WHERE 1(date_make) +GROUP BY source_app + +SELECT source_app, + sum(total), + date_make time +FROM hedera.`order` +WHERE 1(date_make) +GROUP BY source_app + +SELECT + 1(date_make, '7d') , source_app metric, + sum(total) +FROM hedera.order +WHERE + 1(date_make) + group by source_app,1(date_make, '7d') + + +SELECT SUM(amount + surcharge) '2021' + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1 +SELECT SUM(amount + surcharge) '2022' + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1 +SELECT + (SELECT + ((p.perdidas + p.greuge + sub.mc) / (SUM(s.amount + s.surcharge))) * 100 Ratio + FROM bs.sale s + LEFT JOIN (SELECT + SUM(ces.price * ces.quantity) claimed, + SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) perdidas, + SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4)) greuge + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3,5) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + )p ON true + LEFT JOIN (SELECT + SUM((s.price * s.quantity) * (s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400)) sub ON true + WHERE YEAR(s.dated) = "1" + AND MONTH(s.dated) = "1") "1" + +SELECT + (SELECT + ((p.perdidas + p.greuge + sub.mc) / (SUM(s.amount + s.surcharge))) * 100 Ratio + FROM bs.sale s + LEFT JOIN (SELECT + SUM(ces.price * ces.quantity) claimed, + SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) perdidas, + SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4)) greuge + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + ) p ON true + LEFT JOIN (SELECT + SUM((s.price * s.quantity) * (s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400)) sub ON true + WHERE YEAR(s.dated) = "1" AND MONTH(dated) = "1") "1" + +SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) "1" + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND NOT c.isChargedToMana +SELECT + (SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4)) + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3,5) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" -1 + AND MONTH(ces.created) = "1" + AND c.isChargedToMana <> 1) "1" + +SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) "1" + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3,5) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND NOT c.isChargedToMana + +SELECT + (SELECT FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4))) + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND c.isChargedToMana <> 1) "1" +SELECT + (SELECT + SUM((- s.price * s.quantity * (100 - s.discount) / 100) * (1 - (5 - c.responsibility) / 4)) + sub.mc Mana + + FROM vn.claim c + JOIN vn.claimEnd ce on ce.claimFk = c.id and ce.claimDestinationFk in (2,3,5) + JOIN vn.sale s on s.id = ce.saleFk + JOIN ( + SELECT SUM((s.price * s.quantity) *(s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400,31,1118) + )sub + JOIN vn.ticket tk on tk.id = c.ticketFk + WHERE YEAR(tk.shipped) = "1" + AND MONTH(tk.shipped) = "1" + AND isChargedToMana = 1) "1" +SELECT + (SELECT + SUM((- s.price * s.quantity * (100 - s.discount) / 100) * (1 - (5 - c.responsibility) / 4)) + sub.mc Mana + + FROM vn.claim c + JOIN vn.claimEnd ce on ce.claimFk = c.id and ce.claimDestinationFk in (2,3) + JOIN vn.sale s on s.id = ce.saleFk + JOIN ( + SELECT SUM((s.price * s.quantity) *(s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400,31,1118) + )sub + JOIN vn.ticket tk on tk.id = c.ticketFk + WHERE YEAR(tk.shipped) = "1" + AND MONTH(tk.shipped) = "1" + AND isChargedToMana = 1) "1" +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) amount + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND NOT c.isChargedToMana)sub1 + +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4) amount + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3,5) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND NOT c.isChargedToMana)sub1 + + +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4)) amount + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3,5) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" -1 + AND MONTH(ces.created) = "1" + AND c.isChargedToMana <> 1)sub1 + + +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (1 - (5 - c.responsibility) / 4))) amount + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) + JOIN vn.sale ces ON ces.id = ce.saleFk + WHERE YEAR(ces.created) = "1" + AND MONTH(ces.created) = "1" + AND c.isChargedToMana <> 1)sub1 + +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT + SUM((- s.price * s.quantity * (100 - s.discount) / 100) * (1 - (5 - c.responsibility) / 4)) + sub2.mc amount + + FROM vn.claim c + JOIN vn.claimEnd ce on ce.claimFk = c.id and ce.claimDestinationFk in (2,3,5) + JOIN vn.sale s on s.id = ce.saleFk + JOIN ( + SELECT SUM((s.price * s.quantity) *(s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400,31,1118) + )sub2 + JOIN vn.ticket tk on tk.id = c.ticketFk + WHERE YEAR(tk.shipped) = "1" + AND MONTH(tk.shipped) = "1" + AND isChargedToMana = 1)sub1 + + +SELECT (sub1.amount/sub.amount)*100 '1' + FROM (SELECT SUM(amount + surcharge) amount + FROM bs.sale + WHERE YEAR(dated) = 1 AND MONTH(dated) = 1)sub + + JOIN (SELECT + SUM((- s.price * s.quantity * (100 - s.discount) / 100) * (1 - (5 - c.responsibility) / 4)) + sub2.mc amount + + FROM vn.claim c + JOIN vn.claimEnd ce on ce.claimFk = c.id and ce.claimDestinationFk in (2,3) + JOIN vn.sale s on s.id = ce.saleFk + JOIN ( + SELECT SUM((s.price * s.quantity) *(s.discount / 100)) mc + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE YEAR(t.shipped) = "1" + AND MONTH(t.shipped) = "1" + AND t.clientFk NOT IN (200,400,31,1118) + )sub2 + JOIN vn.ticket tk on tk.id = c.ticketFk + WHERE YEAR(tk.shipped) = "1" + AND MONTH(tk.shipped) = "1" + AND isChargedToMana = 1)sub1 + +SELECT DISTINCT YEAR + FROM vn.`time` t + WHERE t.YEAR <= YEAR(CURDATE()) +1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 +SELECT 1 - 1 +SELECT +SUM(s.amount) + p.claimed +FROM bs.sale s + LEFT JOIN ( SELECT + s.id sale, + -SUM(s.price * s.quantity) claimed + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id + JOIN vn.sale s ON s.id = ce.saleFk + WHERE YEAR(s.created) = "1" + AND MONTH(s.created) = "1")p ON true +WHERE YEAR(dated) = "1" + AND MONTH(dated) = "1" +SELECT +SUM(s.amount) + p.claimed +FROM bs.sale s + LEFT JOIN ( SELECT + s.id sale, + -SUM(s.price * s.quantity) claimed + FROM vn.claim c + JOIN vn.claimEnd ce ON ce.claimFk = c.id + JOIN vn.sale s ON s.id = ce.saleFk + WHERE YEAR(s.created) = "1" -1 + AND MONTH(s.created) = "1")p ON true +WHERE YEAR(dated) = "1" -1 + AND MONTH(dated) = "1" +SELECT (sub1.importe-sub2.importe) / sub3.countPeople diferencia, CONCAT(sub1.name,'(',sub5.peopleMinSale ,'/', sub3.countPeople,')'), now() time_sec + FROM (SELECT SUM(importe) importe, name, año, departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale, u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale OR u.id IN (9873, 5021, 5332) + )sub + GROUP BY sub.departmentFk + )sub1 + JOIN ( SELECT SUM(v.portfolioWeight) importe, d.name, wd.departmentFk + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1())-1 AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' AND u.id NOT IN (9873, 5021, 5332) + GROUP by wd.departmentFk + ORDER by importe DESC + )sub2 ON sub1.name = sub2.name + JOIN ( SELECT COUNT(*) countPeople, wd.departmentFk + FROM vn.salesPersonSince sps + JOIN vn.workerDepartment wd ON sps.workerFk = wd.workerFk + WHERE util.firstDayOfMonth(sps.started) <= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) AND + wd.workerFk NOT IN (9873, 5021) + GROUP BY wd.departmentFk + ) sub3 ON sub3.departmentFk = sub1.departmentFk + LEFT JOIN (SELECT COUNT(*) peopleMinSale, sub4.departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale,u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale AND u.id NOT IN (9873, 5021,5332) + )sub4 + GROUP BY sub4.departmentFk + )sub5 ON sub5.departmentFk = sub1.departmentFk + ORDER BY diferencia DESC +SELECT COUNT(*) total, sub.name, now() time_sec + FROM(SELECT u.name, t.id, t.shipped + FROM bs.clientNewBorn cn + JOIN vn.ticket t ON t.clientFk = cn.clientFk + JOIN vn.client c ON c.id = t.clientFk + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + LEFT JOIN (SELECT clientFk + FROM vn.ticket t + WHERE shipped + BETWEEN util.firstDayOfMonth(DATE_SUB(1(), INTERVAL 1 YEAR)) + AND CONCAT(DATE_SUB(util.firstDayOfMonth(1()), INTERVAL 1 DAY), ' 23:59:59') + GROUP BY clientFk) sub ON sub.clientFk = t.clientFk + WHERE t.shipped >= util.firstDayOfMonth(1()) + AND d.name LIKE 'EQUIPO%' + AND t.totalWithoutVat > 50 + AND ISNULL(sub.clientFk) + GROUP BY cn.clientFk) sub + GROUP BY sub.name + ORDER BY total DESC; +SELECT u.name, SUM(importe) amount, NOW() time_sec + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN account.user u ON u.id = c.salespersonFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + JOIN vn.sale s ON s.ticketFk=t.id + JOIN bs.ventas v ON v.Id_Movimiento = s.id + JOIN vn.itemType it ON it.id = v.tipo_id + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND it.itemPackingTypeFk = 'F' + AND dm.code NOT IN ('AGENCY', 'OTHER') + AND u.name NOT IN ('alicia','pedro','pako','amparocm','ismaelalcolea','enric','david','ruben','pacorico','pepe','ralvarez','juanvi','eva','jordi','antoniomp','sandra','elaine','bruno','melvira','palomaft','benjamin','fran','mluz') + GROUP BY u.name + ORDER BY amount DESC +SELECT SUM(importe), + u.name + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.`user` u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE cn.firstShipped > now() - INTERVAL 1 YEAR + AND d.name LIKE 'EQUIPO%' + AND 1(v.fecha) + GROUP BY u.id + ORDER BY SUM(importe) DESC +SELECT SUM(importe) total, u.name, now() time_sec + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN province p ON p.id = c.provinceFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND cn.firstShipped >= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + AND d.name LIKE 'EQUIPO%' + AND p.name IN ('Granada', 'Jaén', 'Córdoba', 'Cádiz', 'Huelva', 'Cáceres', 'Badajoz', 'Girona') + GROUP BY u.id + ORDER BY total DESC + +SELECT SUM(v.portfolioWeight) importe, u.name, NOW() time_sec + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + WHERE v.año = YEAR(1()) + AND v.mes = MONTH(1()) + AND u.name NOT IN ('ismaelalcolea', 'ruben', 'salva', 'david', 'ralvarez', 'enric', 'pacorico', 'pako', 'pepe', 'antoniomp', 'juanvi', 'jandreu', 'jordi', 'palomaft', 'benjamin') + GROUP BY v.Id_Trabajador + ORDER BY importe DESC + + + + + + +SELECT total-(small+little+mini) '>150L', small '75-150L', little '25-75L', mini '1-25L' + FROM(SELECT + COUNT(*) total, + SUM(IF(sub.volume >= 75 AND sub.volume < 150 AND import < 50, TRUE, FALSE)) small, + SUM(IF(sub.volume >= 25 AND sub.volume < 75 AND import < 50, TRUE, FALSE)) little, + SUM(IF(sub.volume >0 AND sub.volume < 25 AND import < 50, TRUE, FALSE)) mini + FROM (SELECT t.id, SUM(sv.litros) volume, t.totalWithoutVAT import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub)sub2 +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 150 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 150 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + GROUP BY sub.userFk + ORDER BY porcentaje ASC, totales DESC + SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 150 AND sub.import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 150 AND sub.import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, d.id departmentFk, d.name, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + GROUP BY sub.departmentFk + ORDER BY porcentaje ASC, totales DESC +SELECT + sub.name, + sub.name2, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + GROUP BY sub.userFk + ORDER BY porcentaje ASC, totales DESC +SELECT total-(small+little+mini) '>150L', small '75-150L', little '25-75L', mini '1-25L' + FROM(SELECT + COUNT(*) total, + SUM(IF(sub.volume >= 75 AND sub.volume < 150 AND import < 50, TRUE, FALSE)) small, + SUM(IF(sub.volume >= 25 AND sub.volume < 75 AND import < 50, TRUE, FALSE)) little, + SUM(IF(sub.volume >0 AND sub.volume < 25 AND import < 50, TRUE, FALSE)) mini + FROM (SELECT t.id, SUM(sv.litros) volume, t.totalWithoutVAT import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub)sub2 + SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 150 AND sub.import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 150 AND sub.import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, d.id departmentFk, d.name, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + GROUP BY sub.departmentFk + ORDER BY porcentaje ASC, totales DESC +SELECT salesPersonCode, + SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 1, 0)) peques, + SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 0, 1)) NOpeques, + ((SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 1, 0)))/SUM(IF(sub.litros < vc.minTicketVolume AND sub.totalWithoutVat < vc.minTicketValue, 0, 1)))*100 '% Pequeños' + FROM (SELECT t.addressFk, + SUM(sv.litros) litros, + t.totalWithoutVat, + w.code salesPersonCode + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + WHERE 1(t.shipped) + AND dm.code IN('AGENCY','DELIVERY','PICKUP') + GROUP BY t.addressFk) sub + JOIN vn.volumeConfig vc + GROUP BY salesPersonCode; +SELECT salesPersonCode, count(*) peques + FROM (SELECT t.addressFk, + SUM(sv.litros) litros, + t.totalWithoutVat, + w.code salesPersonCode + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + WHERE 1(t.shipped) + AND dm.code IN('AGENCY','DELIVERY','PICKUP') + GROUP BY t.addressFk) sub + JOIN vn.volumeConfig vc + WHERE sub.litros < vc.minTicketVolume + AND sub.totalWithoutVat < vc.minTicketValue + GROUP BY salesPersonCode; +SELECT count(*) 'Trabajadores activos' + FROM workerDepartment + WHERE departmentFk IN (37, 23, 103) +SELECT count(*) 'Producción' + FROM workerDepartment + WHERE departmentFk IN (37) +SELECT count(*) 'Cámara' + FROM workerDepartment + WHERE departmentFk IN (23) +SELECT CONCAT(w.firstName, ' ', w.lastName) + FROM workerDepartment wd + LEFT JOIN worker w ON w.id = wd.workerFk + WHERE departmentFk IN (37) + ORDER BY w.firstName +SELECT CONCAT(w.firstName, ' ', w.lastName) + FROM workerDepartment wd + LEFT JOIN worker w ON w.id = wd.workerFk + WHERE departmentFk IN (23) + ORDER BY w.firstName +WITH tBuy AS ( + SELECT itemFk, DATE(MIN(tr.landed)) landed + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + WHERE w.name = 'Algemesi' + AND tr.landed > curdate() + GROUP BY itemFk + ) + SELECT + i.id codigoArticulo, + i.longName Descripcion, + i.`size` Medida, + i.subName Productor, + p.code Parking, + s.code Matrícula, + ish.visible Cantidad, + sc.description sector, + tBuy.landed PróximaLlegada + FROM vn.item i + LEFT JOIN vn.itemShelving ish ON ish.itemFk = i.id + LEFT JOIN vn.shelving s ON s.code = ish.shelvingFk + LEFT JOIN vn.parking p ON p.id = s.parkingFk + LEFT JOIN vn.sector sc ON sc.id = p.sectorFk + LEFT JOIN tBuy ON tBuy.itemFk = i.id + WHERE i.doPhoto + AND (ish.visible > 0 OR 1 = 1) + GROUP BY i.id + ORDER BY p.pickingOrder; +Todos : 1, Sólo con ubicación : 2 + + SELECT i.id codigoArticulo, i.longName Descripción, p.code Parking, iss.shelvingFk +FROM vn.item i + JOIN vn.itemShelvingStock iss ON iss.itemFk = i.id AND iss.warehouseFk = 60 + JOIN vn.parking p ON p.id = iss.parkingFk +WHERE i.image IS NULL + ORDER BY p.pickingOrder; +SELECT CONCAT(s.firstname, ' ', s.lastname) nickname, t.nTickets + FROM ost_staff s + JOIN ost_department d ON d.id = s.dept_id + LEFT JOIN ( + SELECT t.staff_id, COUNT(*) nTickets + FROM ost_ticket t + JOIN ost_ticket_status ts ON ts.id = t.status_id + JOIN ost_staff s ON s.staff_id = t.staff_id + JOIN ost_department d ON d.id = s.dept_id + WHERE d.code = 'IT' + AND ts.state = 'open' AND ts.name <> 'En curso' + GROUP BY s.staff_id + ) t ON t.staff_id = s.staff_id + WHERE d.code = 'IT' + ORDER BY nickname; +with my_users as ( + select distinct u.id, concat(u.firstname, ' ', u.lastname) nickname + from users u + join groups_users gu on gu.user_id = u.id + join users g on g.id = gu.group_id + where g.lastname in ('developer', 'sysadmin') +), my_issues as ( + select u.id user_id, count(*) issues + from issues i + join my_users u on u.id = i.assigned_to_id + join issue_statuses s on s.id = i.status_id + where s.name = 'Análisis' + group by u.id +) +select u.nickname, + r.issues + from my_users u + left join my_issues r on u.id = r.user_id + order by nickname + +with my_users as ( + select distinct u.id, concat(u.firstname, ' ', u.lastname) nickname + from users u + join groups_users gu on gu.user_id = u.id + join users g on g.id = gu.group_id + where g.lastname in ('developer', 'sysadmin') +), my_issues as ( + select i.id, c."value" = 'Refactorización' is_refactor + from issues i + join my_users u on u.id = i.assigned_to_id + join issue_statuses s on s.id = i.status_id + join versions v on v.id = i.fixed_version_id + join custom_values c on c.customized_id = i.id and c.customized_type = 'Issue' + join custom_fields f on f.id = c.custom_field_id + where f."name" = 'Area' + and not s.is_closed + and v.status = 'open' and s.name <> 'Feedback' +), my_time as ( + select i.assigned_to_id user_id, t.is_refactor, + round(sum(greatest(i.estimated_hours - t.hours, 0))::numeric, 2) hours + from issues i + join ( + select i.id issue_id, i.is_refactor, coalesce(sum(t.hours), 0) hours + from my_issues i + left join time_entries t on i.id = t.issue_id + group by i.id, i.is_refactor + ) t on i.id = t.issue_id + group by i.assigned_to_id, t.is_refactor + having round(sum(greatest(i.estimated_hours - t.hours, 0))::numeric, 2) > 0 +) +select u.nickname, + r.hours refactor, + d.hours development + from my_users u + left join my_time r on u.id = r.user_id and r.is_refactor + left join my_time d on u.id = d.user_id and not d.is_refactor + order by nickname + +with my_users as ( + select distinct u.id, concat(u.firstname, ' ', u.lastname) nickname + from users u + join groups_users gu on gu.user_id = u.id + join users g on g.id = gu.group_id + where g.lastname in ('developer', 'sysadmin') +), my_time as ( + select t.user_id, + round(sum(t.hours)::numeric, 2) hours, + c."value" = 'Refactorización' is_refactor + from time_entries t + join issues i on i.id = t.issue_id + join my_users u on u.id = t.user_id + join custom_values c on c.customized_id = i.id and c.customized_type = 'Issue' + join custom_fields f on f.id = c.custom_field_id + where 1(t.spent_on) + AND f."name" = 'Area' + group by t.user_id, is_refactor + having round(sum(t.hours)::numeric, 2) > 0 +) +select u.nickname, + r.hours refactor, + d.hours development + from my_users u + left join my_time r on r.user_id = u.id and r.is_refactor + left join my_time d on d.user_id = u.id and not d.is_refactor + order by nickname + +select count(*) "new" + from issues i + join issue_statuses s on s.id = i.status_id + where s.name = 'Nueva' + and i.assigned_to_id is null +select count(*) "feedback" + from issues i + join issue_statuses s on s.id = i.status_id + where s.name = 'Feedback' +select count(*) "created" + from issues + where 1(created_on) +select count(*) "closed" + from issues i + join issue_statuses s on s.id = i.status_id + where s.is_closed + and 1(i.closed_on) +SELECT + '' AS Asiento, + DATE_FORMAT(ii.booked, "%d/%m/%Y") FechaHechoContabilizado, + '1' Concepto, + 'A' ClaveProducto, + i.name DescripciónProducto, + 'A' RegimenFiscal, + CONCAT('Fact. ',ii.supplierRef) Justificante, + '3' ProvTipoDoc, + s.nif ProvNumDocumento, + s.name ProvRazonSocial, + FORMAT(SUM(b.stickers * (i.weightByPiece / 1000)), 1, 'es_ES') kilogramos, + FORMAT(SUM(b.stickers * (i.weightByPiece * 0.05 / 1000)), 1, 'es_ES') kilogramosNoReciclados, + 'Entrada de Mercancía' Observaciones + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.supplier s ON s.id = e.supplierFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.item i ON i.id = p.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + JOIN vn.invoiceIn ii ON ii.id = e.invoiceInFk + WHERE it.code = 'PNR' + AND 1(ii.booked) + AND w.name = 'VNH' + AND b.quantity + AND e.invoiceInFk + GROUP BY i.id, e.invoiceInFk +UNION ALL +SELECT + '' AS Asiento, + DATE_FORMAT(io.issued, "%d/%m/%Y"), + '2' Concepto, + 'A' ClaveProducto, + tb.name DescripciónProducto, + 'A' RegimenFiscal, + CONCAT('Fact. ',io.`ref`) Justificante, + '1' DestTipoDoc, + c.fi DestNumDoc, + c.name DestRazonSocial, + FORMAT(-SUM((s.quantity / tb.packing) * tb.weightByPiece / 1000),1, 'es_ES') kilogramos, + FORMAT(-SUM((s.quantity / tb.packing) * tb.weightByPiece * 0.05 / 1000), 1, 'es_ES') kilogramosNoReciclados, + 'Salida Extranjero' Observaciones + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.province p2 ON p2.id = c.provinceFk + JOIN vn.country c2 ON c2.id = p2.countryFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.warehouse w2 ON w2.id = t.warehouseFk + JOIN ( + SELECT b.itemFk, i.name,b.packageFk, b.packing, i.weightByPiece + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.supplier s ON s.id = e.supplierFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.item i ON i.id = p.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + WHERE it.code = 'PNR' + AND 1(tr.landed) + AND b.quantity + AND e.invoiceInFk + AND w.name = 'VNH' + GROUP BY b.itemFk + ) tb ON tb.itemFk = s.itemFk + JOIN vn.invoiceOut io ON io.`ref` = t.refFk + WHERE w2.name = 'VNH' + AND 1(t.shipped) + AND c2.code <> 'ES' + AND s.quantity > 0 +GROUP BY tb.itemFk, t.refFk +ORDER BY FechaHechoContabilizado; +SELECT + IF(DATE_FORMAT(NOW(),'%d/%c') = DATE_FORMAT(tr.landed,'%d/%c'),CONCAT(DATE_FORMAT(tr.landed,'%d/%c'), ' (Hoy)'),DATE_FORMAT(tr.landed,'%d/%c')) Fecha, + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * b.stickers) / 1000000) m3_total + FROM vn.buy b + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.supplier s ON s.id = e.supplierFk + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse wo ON wo.id = tr.warehouseOutFk + JOIN vn.warehouse wi ON wi.id = tr.warehouseInFk + WHERE tr.landed BETWEEN 1() AND 1() + AND ic.merchandise + AND NOT e.isRaid + AND wi.name = '1' + GROUP BY tr.landed + ORDER BY tr.landed; + + +SELECT + wo.name Origen, + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * b.stickers) / 1000000) 'Volumen', + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * b.printedStickers) / 1000000) 'Etiquetado', + SUM(b.stickers) 'Etiquetas', + SUM(b.printedStickers) 'Impresas' + FROM vn.buy b + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.supplier s ON s.id = e.supplierFk + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse wo ON wo.id = tr.warehouseOutFk + JOIN vn.warehouse wi ON wi.id = tr.warehouseInFk + WHERE tr.landed = '1' + AND ic.merchandise + AND NOT e.isRaid + AND wi.name = '1' + GROUP BY tr.landed, wo.id + ORDER BY Volumen DESC + + +SELECT + DATE_FORMAT(c.created,'%e/%c %H:%i') salida, + ao.nickname origen, + s.name transportista, + c.packagesList Envases, + c.truckPlate matrícula, + c.specialAgreements Temperatura, + c.senderInstruccions Notas, + DATE_FORMAT(c.landed, '%e/%c %H:%i') llegada + +FROM vn.cmr c + JOIN vn.address a on a.id = c.addressToFk + JOIN vn.warehouse w on w.addressName = a.nickname + LEFT JOIN vn.address ao ON ao.id = c.addressFromFk + JOIN vn.supplier s on s.id = c.supplierFk + WHERE DATE(c.ead) = '1' + AND w.name = '1' + ORDER BY (llegada IS NOT NULL), llegada DESC, transportista, origen + + +SELECT name + FROM vn.warehouse + WHERE code IS NOT NULL +SELECT left(dated, 10) + FROM vn.time + WHERE dated > TIMESTAMPADD(WEEK, -1, CURDATE()) +SELECT + SUM(IF (año = 2020, importe,0)) AS '2020', + SUM(IF (año = 2021, importe,0)) AS '2021', + NOW() time_sec + FROM bs.vendedores v + WHERE v.año > 2019 AND v.mes=2 + +SELECT u.name, SUM(importe) amount, NOW() time_sec + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN account.user u ON u.id = c.salespersonFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + JOIN vn.sale s ON s.ticketFk=t.id + JOIN bs.ventas v ON v.Id_Movimiento = s.id + JOIN vn.itemType it ON it.id = v.tipo_id + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND it.itemPackingTypeFk = 'F' + AND dm.code NOT IN ('AGENCY') + AND u.name NOT IN ('ismaelalcolea', 'ruben', 'salva', 'david', 'ralvarez', 'enric', 'pacorico', 'pako', 'pepe', 'antoniomp', 'juanvi') + GROUP BY u.name + ORDER BY amount DESC +CALL vn.zone_upcomingDeliveries(); + + + + + + +SELECT (sub1.importe-sub2.importe) / sub5.peopleMinSale diferencia, CONCAT(sub1.name,'(',sub5.peopleMinSale ,'/', sub3.countPeople,')'), now() time_sec + FROM (SELECT SUM(importe) importe, name, año, departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale, u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale OR u.id IN (9873, 5021) + )sub + GROUP BY sub.departmentFk + )sub1 + JOIN ( SELECT SUM(v.portfolioWeight) importe, d.name, wd.departmentFk + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1())-1 AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP by wd.departmentFk + ORDER by importe DESC + )sub2 ON sub1.name = sub2.name + JOIN ( SELECT COUNT(*) countPeople, wd.departmentFk + FROM vn.salesPersonSince sps + JOIN vn.workerDepartment wd ON sps.workerFk = wd.workerFk + WHERE util.firstDayOfMonth(sps.started) <= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + GROUP BY wd.departmentFk + ) sub3 ON sub3.departmentFk = sub1.departmentFk + LEFT JOIN (SELECT COUNT(*) peopleMinSale, sub4.departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale,u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale OR u.id IN (9873, 5021) + )sub4 + GROUP BY sub4.departmentFk + )sub5 ON sub5.departmentFk = sub1.departmentFk + ORDER BY diferencia DESC +SELECT SUM(importe) total, u.name, now() time_sec + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND cn.firstShipped >= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + AND d.name LIKE 'EQUIPO%' + GROUP BY u.id + ORDER BY total DESC + +SELECT SUM(importe) total, u.name, now() time_sec + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN province p ON p.id = c.provinceFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND cn.firstShipped >= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + AND d.name LIKE 'EQUIPO%' + AND p.name IN ('Granada', 'Jaén', 'Córdoba', 'Cádiz', 'Huelva', 'Cáceres', 'Badajoz', 'Girona') + GROUP BY u.id + ORDER BY total DESC + +/*SELECT SUM(importe) total, u.name, now() time_sec + + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + WHERE v.fecha >= util.quarterFirstDay(YEAR(1()),1) + AND cn.firstShipped >= util.quarterFirstDay(YEAR(1())-1,1) + AND u.role = 18 AND u.name NOT IN ('ismaelalcolea', 'ruben') + GROUP BY u.id + ORDER BY total DESC + limit 20*/ +/*SELECT (sub1.importe-sub2.importe) / sub5.peopleMinSale diferencia, CONCAT(sub1.name,'(', sub5.peopleMinSale, '/', sub3.countPeople,')'), now() time_sec + FROM (SELECT SUM(importe) importe, name, departmentFk + FROM(SELECT SUM(v.importe) importe, d.name, v.año, wd.departmentFk, (200000 * DATEDIFF(1(),'2020-07-01'))/153 minSale + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minSale + ORDER BY importe DESC + ) sub + GROUP BY sub.departmentFk + ) sub1 + JOIN ( SELECT SUM(v.portfolioWeight) importe, d.name, wd.departmentFk + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1())-1 AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP by wd.departmentFk + ORDER by importe DESC + )sub2 ON sub1.name = sub2.name + JOIN (SELECT COUNT(*) countPeople, wd.departmentFk + FROM vn.salesPersonSince sps + JOIN vn.workerDepartment wd ON sps.workerFk = wd.workerFk + GROUP BY wd.departmentFk + ) sub3 ON sub3.departmentFk = sub1.departmentFk + JOIN (SELECT COUNT(*) peopleMinSale, sub4.departmentFk + FROM (SELECT SUM(v.importe) importe, d.name, v.año, wd.departmentFk, (200000 * DATEDIFF(1(),'2020-07-01'))/153 minSale + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minSale + ORDER BY importe DESC + )sub4 + GROUP BY sub4.departmentFk + ) sub5 ON sub5.departmentFk = sub1.departmentFk + ORDER BY diferencia DESC*/ +SELECT + SUM(IF (año = 2019, importe,0)) AS '2019', + SUM(IF (año = 2020, importe,0)) AS '2020', + SUM(IF (año = 2021, importe,0)) AS '2021', + NOW() time_sec + FROM bs.vendedores v + WHERE v.año > 2017 AND v.mes >= 1 AND v.mes <= 11 +SELECT SUM(sqs.amount) importe, + u.name, + NOW() time_sec + FROM bs.salesMonthlySnapshot sqs + JOIN account.user u ON u.id = sqs.workerFk + JOIN vn.`time` t ON t.dated = DATE(1()) + JOIN (SELECT salesYear + FROM `time` + WHERE `month` = MONTH(1()) AND `year` = YEAR(1()) + LIMIT 1) sub1 + WHERE sub1.salesYear = t.salesYear + GROUP by sqs.workerFk + ORDER by importe DESC + LIMIT 15 +SELECT pq.id, + p.name printer, + r.name report, + pqa.value param, + pq.statusCode, + pq.error, + pq.workerFk, + pq.created - INTERVAL 1 HOUR created + FROM vn.printQueue pq + JOIN vn.printer p ON p.id = pq.printerFk + JOIN vn.report r ON r.id = pq.reportFk + JOIN vn.printQueueArgs pqa ON pqa.printQueueFk = pq.id + LEFT JOIN vn.ticketCollection tc ON tc.collectionFk = pqa.value + LEFT JOIN vn.ticket t ON t.id = pqa.value + WHERE pq.statusCode = 'queued' +GROUP BY pq.id +SELECT name report, + IF(`method` IS NULL, 0, 1) 'ㅤ', + method + FROM vn.report + ORDER BY name DESC +SELECT r.name, + pq.error, + COUNT(pq.id) total + FROM vn.printQueue pq + JOIN vn.report r ON r.id = pq.reportFk + WHERE pq.statusCode = 'error' + GROUP BY pq.error, pq.reportFk +SELECT pq.id, + p.name printer, + r.name report, + pqa.value param, + pq.statusCode, + pq.error, + pq.workerFk, + pq.created - INTERVAL 1 HOUR created + FROM vn.printQueue pq + JOIN vn.printer p ON p.id = pq.printerFk + JOIN vn.report r ON r.id = pq.reportFk + JOIN vn.printQueueArgs pqa ON pqa.printQueueFk = pq.id + LEFT JOIN vn.ticketCollection tc ON tc.collectionFk = pqa.value + LEFT JOIN vn.ticket t ON t.id = pqa.value + WHERE pq.statusCode = 'error' +GROUP BY pq.id +SELECT pq.id, + p.name printer, + r.name report, + pqa.value param, + pq.statusCode, + pq.workerFk, + pq.created - INTERVAL 1 HOUR created + FROM vn.printQueue pq + JOIN vn.printer p ON p.id = pq.printerFk + JOIN vn.report r ON r.id = pq.reportFk + JOIN vn.printQueueArgs pqa ON pqa.printQueueFk = pq.id + LEFT JOIN vn.ticketCollection tc ON tc.collectionFk = pqa.value + LEFT JOIN vn.ticket t ON t.id = pqa.value + WHERE pq.statusCode = 'printing' +GROUP BY pq.id +SELECT TIME_FORMAT(created, '%r') 'Last print' + FROM vn.printQueue pq + WHERE pq.statusCode = 'printed' + ORDER BY created DESC + LIMIT 1 +SELECT pq.id, + p.name printer, + r.name report, + pqa.value param, + pq.statusCode, + pq.workerFk, + pq.created - INTERVAL 1 HOUR created + FROM vn.printQueue pq + JOIN vn.printer p ON p.id = pq.printerFk + JOIN vn.report r ON r.id = pq.reportFk + JOIN vn.printQueueArgs pqa ON pqa.printQueueFk = pq.id + LEFT JOIN vn.ticketCollection tc ON tc.collectionFk = pqa.value + LEFT JOIN vn.ticket t ON t.id = pqa.value + WHERE pq.statusCode = 'printed' +GROUP BY pq.id +SELECT r.name 'Last report' + FROM vn.printQueue pq + JOIN vn.report r ON r.id = pq.reportFk + WHERE pq.statusCode = 'printed' + ORDER BY created DESC + LIMIT 1 +SELECT COUNT(pq.id) jobs, + DATE(pq.created) time + FROM vn.printQueue pq + GROUP BY DATE(created) +SELECT SUM(volume), u.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('pako','jfrau','juanvi','sandra','palomaft','ralvarez','salva','amparocm','jordi','chechu','fran','daniel','elaine','ineslj','nuriald','mluz','david','antoniomp','jstegarescu','mireia','miguelft','pacorico','raquelgc','ruben','pepe') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('bruno','anamariasa','anabc','miriamc','deborajd','gemmaem') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('jmontoro','jsoria','enrique','sergio','tonycooper','aleixandresc') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('nachom','cris','mmar','jvilar','jbrocal','mballesterolmos') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('manoli','dsarrion','cesteban','claudi','olgapc','debora') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('germansd','monicafaust','lozano','elena','silverio','rodrigo') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('juanbau','jorge','clopez','javi','mari') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('jvalles','jibanez','mserrano','miriam','czambrano') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT SUM(volume), u.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('pako','jfrau','juanvi','sandra','palomaft','ralvarez','salva','amparocm','jordi','chechu','fran','daniel','elaine','ineslj','nuriald','mluz','david','antoniomp','jstegarescu','mireia','miguelft','pacorico','raquelgc','ruben','pepe') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('bruno','josemp','anamariasa','anabc','miriamc','deborajd') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('gemmaem','jsoria','enrique','sergio','tonycooper','aleixandresc') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('nachom','cris','mmar','jvilar','jmontoro','mballesterolmos') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('manoli','jbrocal','cesteban','claudi','olgapc','rodrigo') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('germansd','monicafaust','lozano','elena','juanbau','debora') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('dsarrion','jorge','silverio','clopez','javi','mari') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('jvalles','jibanez','mserrano') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('miriam','czambrano') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT SUM(volume), u.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' OR t.id IN (3898725,3884510,3892533,3878483,3898809,3879172,3894220,3886208,3899105,3895458,3899115,3898571,3899070,3884510,3879172,3898197,3894810) + AND c.salesPersonFk IS NOT NULL + AND u.name NOT IN ('pako','jfrau','juanvi','sandra','palomaft','ralvarez','salva','amparocm','jordi','chechu','fran','daniel','elaine','ineslj','nuriald','mluz','david','antoniomp','jstegarescu','mireia','miguelft','pacorico','raquelgc','ruben','pepe') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('bruno','anamariasa','anabc','miriamc','deborajd','gemmaem') + OR t.id = 3899105 + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('aleixandresc','enrique','jsoria','nachom','sergio','tonycooper') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('cesteban','claudi','mballesterolmos','monicafaust','olgapc','rodrigo') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('cris','jbrocal','jmontoro','jvilar','manoli','mmar') + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('debora','dsarrion','elena','germansd','jorge','lozano') + OR t.id IN (3886208,3894220) + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('clopez','javi','juanbau','mari','silverio') + OR t.id IN (3878483,3898725) + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT u.name Comercial, SUM(volume) Volumen + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE sv.shipped BETWEEN '2022-10-21 00:00:00' AND '2022-10-22 23:59:59' + AND c.salesPersonFk IS NOT NULL + AND u.name IN ('czambrano','jibanez','jvalles','miriam','mserrano') + OR t.id IN (3898809,3884510,3879172,3898571,3899070,3899115,3879172,3898197,3895458,3892533,3894810) + GROUP BY u.name + ORDER BY SUM(volume) DESC +SELECT sub.`code`, + sub.volume/sub.hours productivity, + NOW() time_sec + FROM(SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + SUM(CAST(wp.volume AS DECIMAL (6,2)))volume, + SUM(CAST(wp.seconds/3600 AS DECIMAL (6,2))) hours + FROM bs.workerProductivity wp + JOIN vn.worker w ON w.id = wp.workerFk + JOIN vn.role r ON r.id = wp.roleFk + JOIN vn.warehouse wh ON wh.id = wp.warehouseFk + WHERE wp.dated BETWEEN 1() AND 1() AND + wh.name = 'Algemesi' AND + r.name='packager' + GROUP BY wp.workerFk + ORDER BY volume DESC + )sub + ORDER BY productivity DESC + + +SELECT sub.code, + CAST(SUM(sub.lineas)/CAST(SUM(sub.seconds)/3600 AS DECIMAL (6,2)) AS DECIMAL (6,0)) productivity, + NOW() time_sec + FROM (SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + count(e.ticketFk) as lineas, + TIME_TO_SEC(TIMEDIFF( MAX(st.created),MIN(st.created))) + w2.minSeconsItemPicker seconds, + t.shipped + FROM vn.expedition e + JOIN vn.ticket t ON t.id = e.ticketFk + JOIN bs.workerProductivityConfig w2 ON TRUE + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleTracking st ON st.saleFk = s.id + JOIN vn.worker w ON e.workerFk = w.id + WHERE e.workerFk IS NOT NULL AND + t.shipped BETWEEN 1() AND 1() + GROUP BY e.ticketFk + ) sub + JOIN bs.workerProductivityConfig w ON TRUE + JOIN account.role r ON r.name='packager' + WHERE sub.shipped BETWEEN 1() AND 1() + GROUP BY code + ORDER BY productivity DESC; +SELECT sub.`code`, + sub.volume/sub.hours productivity, + NOW() time_sec + FROM(SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + SUM(CAST(wp.volume AS DECIMAL (6,2)))volume, + SUM(CAST(wp.seconds/3600 AS DECIMAL (6,2))) hours + FROM bs.workerProductivity wp + JOIN vn.worker w ON w.id = wp.workerFk + JOIN vn.role r ON r.id = wp.roleFk + JOIN vn.warehouse wh ON wh.id = wp.warehouseFk + WHERE wp.dated BETWEEN 1() AND 1() AND + wh.name = 'Algemesi' AND + r.name='itemPicker' + GROUP BY wp.workerFk + ORDER BY volume DESC + LIMIT 150 + )sub + WHERE sub.volume > 2 + ORDER BY productivity DESC + + + +SELECT sub.code, + CAST(SUM(sub.lineas)/CAST(SUM(sub.seconds)/3600 AS DECIMAL (6,2)) AS DECIMAL (6,2)) productivity, + NOW() time_sec + FROM(SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + DATE(t.shipped) dated, + COUNT(t.id) lineas, + TIME_TO_SEC(TIMEDIFF( MAX(st.created),MIN(st.created))) + w2.minSeconsItemPicker seconds + FROM vn.saleTracking st + LEFT JOIN vn.salesPreviousPreparated sp ON sp.saleFk = st.saleFk + JOIN vn.sale s ON s.id = st.saleFk + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.worker w ON w.id = st.workerFk + JOIN vn.state e ON e.id = st.stateFk + JOIN vncontrol.accion a ON a.accion_id = st.actionFk + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = st.workerFk + LEFT JOIN vn.department d ON d.id = wd.departmentFk + JOIN bs.workerProductivityConfig w2 ON TRUE + WHERE t.shipped BETWEEN 1() AND 1() + AND ISNULL(sp.saleFk) + AND NOT IFNULL(d.name,'') LIKE 'EQUIPO%' + AND a.accion='SACAR' + GROUP BY t.id, st.workerFk) sub + GROUP BY sub.code + ORDER BY productivity DESC; + +SELECT sub.`code`, + sub.volume/sub.hours productivity, + NOW() time_sec + FROM(SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + SUM(CAST(wp.volume AS DECIMAL (6,2)))volume, + SUM(CAST(wp.seconds/3600 AS DECIMAL (6,2))) hours + FROM bs.workerProductivity wp + JOIN vn.worker w ON w.id = wp.workerFk + JOIN vn.role r ON r.id = wp.roleFk + JOIN vn.warehouse wh ON wh.id = wp.warehouseFk + WHERE wp.dated BETWEEN 1() AND 1() AND + wh.name = 'Algemesi' AND + r.name='checker' + GROUP BY wp.workerFk + ORDER BY volume DESC + )sub + ORDER BY productivity DESC + + +SELECT sub.code, + CAST(SUM(sub.lineas)/CAST(SUM(sub.seconds)/3600 AS DECIMAL (6,2)) AS DECIMAL (6,2)) productivity, + NOW() time_sec + FROM (SELECT CONCAT( w.firstName, ' ', w.lastName, ' (', w.`code`, ')') code, + count(t.id) as lineas, + TIME_TO_SEC(TIMEDIFF( MAX(st.created),MIN(st.created))) + w2.minSeconsItemPicker seconds, + t.shipped + FROM vn.state ss + JOIN vn.saleTracking st ON st.stateFk = ss.id + JOIN vn.worker w ON st.workerFk = w.id + JOIN bs.workerProductivityConfig w2 ON TRUE + JOIN vn.sale s ON s.id = st.saleFk + JOIN vn.ticket t ON s.ticketFk = t.id + WHERE ss.code = 'CHECKED' AND + t.shipped BETWEEN 1() AND 1() + GROUP BY t.id + ) sub + WHERE sub.shipped BETWEEN 1() AND 1() + GROUP BY code + ORDER BY productivity DESC; +100 +200 +150 +10 +30 +20 +15 +SELECT total-(small+little+mini) '>75L', small '50-75L', little '25-50L', mini '1-25L' + FROM(SELECT + COUNT(*) total, + SUM(IF(sub.volume >= 50 AND sub.volume < 75 AND import < 50, TRUE, FALSE)) small, + SUM(IF(sub.volume >= 25 AND sub.volume < 50 AND import < 50, TRUE, FALSE)) little, + SUM(IF(sub.volume >0 AND sub.volume < 25 AND import < 50, TRUE, FALSE)) mini + FROM (SELECT t.id, SUM(sv.litros) volume, t.totalWithoutVAT import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub)sub2 +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('deborajd','enrique','gemmaem','jsoria','nachom','tonycooper') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('anabc','miriamc','chechu','anamariasa') + GROUP BY sub.userFk + ORDER BY porcentaje ASC, totales DESC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('aleixandresc','cesteban','cris','jbrocal', 'jmontoro','jorge') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('debora','javi','jvilar','lozano', 'manoli','mballesterolmos') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('dsarrion','germansd','juanbau','mmar', 'olgapc','rodrigo') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('clopez','elena','jvalles','mari', 'mserrano','silverio') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('claudi','czambrano','sergio') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT + sub.name, + COUNT(*) totales, + SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE)) pequeños, + CAST((SUM(IF(sub.volumen1 < 75 AND import < 50, TRUE, FALSE))*100)/COUNT(*) AS DECIMAL (10,2)) porcentaje + + FROM (SELECT t.id ticketFk, SUM(sv.litros) volumen1, c.salesPersonFk, u.id userFk, u.name, d.name name2, t.totalWithoutVat import + FROM vn.ticket t + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.client c ON c.id = t.clientFK + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + + WHERE 1(t.shipped) + AND w.code='ALG' + AND d.name LIKE 'EQUIPO%' + GROUP BY t.id) sub + WHERE sub.name IN ('jibanez','miriam','monicafaust') + GROUP BY sub.userFk + ORDER BY porcentaje ASC +SELECT r.id 'Id Ruta', + a.name 'Agencia', + r.created Fecha, + r.kmEnd - r.kmStart km, + r.m3, + rc.km '€ Km Mes', + rc.m3 '€ M3 Mes', + rc.yearlyKm '€ Km Año', + rc.yearlyM3 '€ M3 Año' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + LEFT JOIN vn.agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vn.agency a ON a.id = am.agencyFk + JOIN worker w ON w.id = r.workerFk + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + WHERE 1(r.created) + AND w.id = '1' +SELECT * + FROM +(SELECT c.dated Fecha , + a.name COLLATE utf8_general_ci Descripción, + CAST(IF( a.isCalculate, IF(b.basicSalary, b.basicSalary, b.amount) / IF(a.isNaturalDay, monthNaturalDays.days, DAY(LAST_DAY(c.dated))), 0) * a.permissionRate AS DECIMAL(10,2)) Importe + FROM vn.business b + JOIN vn.department d ON d.id = b.departmentFk AND d.name IN ('REPARTO') + JOIN calendar c ON b.id = c.businessFk + JOIN absenceType a ON a.id = c.dayOffTypeFk + JOIN (SELECT COUNT(*) days, month, year + FROM vn.time + WHERE DAYOFWEEK(dated) NOT IN (1,7) + GROUP BY month, year + ) monthNaturalDays ON monthNaturalDays.month = MONTH(c.dated) AND monthNaturalDays.year = YEAR(c.dated) + WHERE 1(c.dated) + AND b.workerFk = '1' +UNION ALL +SELECT t.dated, + 'Festivo' , + IF(b.basicSalary, b.basicSalary, b.amount) / monthNaturalDays.days + FROM vn.business b + JOIN time t ON t.dated BETWEEN b.started AND IFNULL(b.ended,1()) + JOIN vn.department d ON d.id = b.departmentFk AND d.name IN ('REPARTO') + JOIN calendarHolidays ch ON ch.workCenterFk = b.workCenterFk AND ch.dated = t.dated + JOIN (SELECT COUNT(*) days, month, year + FROM vn.time + WHERE DAYOFWEEK(dated) NOT IN (1,7) + GROUP BY month, year) monthNaturalDays ON monthNaturalDays.month = MONTH(t.dated) AND monthNaturalDays.year = YEAR(t.dated) + WHERE 1(t.dated) + AND b.workerFk = '1' +UNION ALL + SELECT rc.dated, + name, + rc.price + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + WHERE 1(rc.dated) + AND rc.workerFk = '1' )sub +ORDER BY Fecha +SELECT route Rutas, + km Km, + m3 M3, + kmAmount '€ Km', + m3Amount '€ M3', + CONCAT(mainlineDelivered , ' / ', complement) Complementos, + complementAmount '€', + kmAmount + m3Amount + complementAmount Total + FROM + (SELECT COUNT(*) route, + SUM(rc.km) kmAmount, + SUM(rc.m3) m3Amount, + SUM(r.kmEnd-r.KmStart) km, + SUM(r.m3) m3 + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + WHERE 1( r.created ) + AND r.workerFk= '1' ) sub + JOIN (SELECT SUM(total) complement, + SUM(mainlineDelivered) mainlineDelivered, + SUM(amount) complementAmount + FROM ( + SELECT COUNT(*) total, + SUM(rc.price) amount, + SUM(IF(ra.isMainlineDelivered AND b.id IS NOT NULL, 1, 0)) mainlineDelivered + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + JOIN vn.time t ON t.dated = rc.dated + JOIN routeConfig rco + LEFT JOIN vn.business b ON t.dated BETWEEN b.started + AND IFNULL(b.ended, CURDATE()) AND b.workerBusinessProfessionalCategoryFk = rco.truckerBusinessProfessionalCategoryFk + AND rc.workerFk = b.workerFk + WHERE rc.workerFk = '1' + AND 1(rc.dated) + UNION + SELECT COUNT(*)total, + SUM(CAST( + IF( a.isCalculate, IF(b.basicSalary, b.basicSalary, b.amount) / + IF(a.isNaturalDay, monthNaturalDays.days, DAY(LAST_DAY(c.dated))), 0) + * a.permissionRate AS DECIMAL(10, 2))), + 0 isMainlineDelivered + FROM vn.business b + JOIN vn.department d ON d.id = b.departmentFk AND d.name IN ('REPARTO') + JOIN calendar c ON b.id = c.businessFk + JOIN absenceType a ON a.id = c.dayOffTypeFk + JOIN (SELECT COUNT(*) days, month, year + FROM vn.time + WHERE DAYOFWEEK(dated) NOT IN (1,7) + GROUP BY month, year + ) monthNaturalDays ON monthNaturalDays.month = MONTH(c.dated) + AND monthNaturalDays.year = YEAR(c.dated) + WHERE 1(c.dated) + AND b.workerFk = '1' + UNION + SELECT COUNT(*) total, + SUM(IF(b.basicSalary, b.basicSalary, b.amount) / monthNaturalDays.days), + 0 isMainlineDelivered + FROM vn.business b + JOIN time t ON t.dated BETWEEN b.started AND IFNULL(b.ended, 1()) + JOIN vn.department d ON d.id = b.departmentFk AND d.name IN ('REPARTO') + JOIN calendarHolidays ch ON ch.workCenterFk = b.workCenterFk AND ch.dated = t.dated + JOIN (SELECT COUNT(*) days, month, year + FROM vn.time + WHERE DAYOFWEEK(dated) NOT IN (1,7) + GROUP BY month, year + )monthNaturalDays ON monthNaturalDays.month = MONTH(t.dated) + AND monthNaturalDays.year = YEAR(t.dated) + WHERE 1(t.dated) + AND b.workerFk = '1' + )complement)sub2 +SELECT COUNT(*) 'Rutas', + SUM(r.kmEnd-r.KmStart) 'Km', + SUM(r.m3) 'M3', + mainlineDelivered.total 'Troncales', + SUM(rc.yearlyKm) '€ Km', + SUM(rc.yearlyM3) '€ M3', + mainlineDelivered.amount '€ Troncales', + SUM(rc.yearlyKm) + SUM(rc.yearlyM3) + IFNULL(mainlineDelivered.amount,0) 'Total €' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + LEFT JOIN ( + SELECT COUNT(*) total, + COUNT(*) * rco.mainlineDelivered amount + FROM routeComplement rc + JOIN routeAction ra ON ra.id = rc.routeActionFk + JOIN vn.time t ON t.dated = rc.dated + JOIN routeConfig rco + JOIN vn.business b ON t.dated BETWEEN b.started + AND IFNULL(b.ended, CURDATE()) AND b.workerBusinessProfessionalCategoryFk = rco.truckerBusinessProfessionalCategoryFk + AND rc.workerFk = b.workerFk + WHERE rc.dated BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND rc.workerFk = '1' + AND ra.isMainlineDelivered)mainlineDelivered ON TRUE + WHERE r.created BETWEEN CONCAT(YEAR(1()) - IF(MONTH(1()) <> 12, 1, 0), '-12-01') AND 1() + AND r.workerFk= '1' +SELECT DISTINCT wc.name + FROM workerLabour wl + JOIN (SELECT MAX(businessFk) businessFk + FROM workerLabour wl + JOIN vn.department AS d ON d.id = wl.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + AND d.name IN ('REPARTO') + GROUP BY wl.workerFK)sub ON sub.businessFk = wl.businessFk + JOIN workCenter wc ON wc.id = wl.workCenterFk +SELECT CONCAT( wl.workerFk, ' - ', w.firstName, ' ', w.lastname) + FROM workerLabour wl + JOIN (SELECT MAX(businessFk) businessFk + FROM workerLabour wl + JOIN vn.department AS d ON d.id = wl.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + AND d.name IN ('REPARTO') + GROUP BY wl.workerFK)sub ON sub.businessFk = wl.businessFk + JOIN worker w ON w.id = wl.workerFk + JOIN workCenter wc ON wc.id = wl.workCenterFk + LEFT JOIN workerDistributionCategory wd ON wd.workerFk = wl.workerFk + WHERE wc.name ='1' + AND wd.workerFk IS NULL + + +SELECT w.id + FROM worker w + WHERE '1' = CONCAT( w.id, ' - ', w.firstName, ' ', w.lastname) + + +SELECT r.id 'Id Ruta', + DATE_FORMAT(r.created, "%d/%m/%Y") Fecha, + w.id 'Id Conductor', + CONCAT(w.firstName, " " , w.lastName) Conductor, + wc.id 'Id Centro', + wc.name 'Centro', + rc.km 'Reparto Km Mensual', + rc.m3 'Reparto M3 Mensual', + rc.yearlyKm 'Reparto Km Anual', + rc.yearlyM3 'Reparto M3 Anual', + rc.cat4m3 'Enruta cat.IV Mensual', + rc.cat5m3 'Enruta cat.V Mensual' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN worker w ON w.id = r.workerFk + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + WHERE 1(r.created) +SELECT COUNT(*) 'Rutas', + w.id 'Id Conductor', + CONCAT(w.firstName, " " , w.lastName) Conductor, + wc.id 'Id Centro', + wc.name 'Centro', + SUM(rc.km) 'Km Mensual', + SUM(rc.m3) 'M3 Mensual', + SUM(rc.yearlyKm) 'Km Anual', + SUM(rc.yearlyM3) 'M3 Anual' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN worker w ON w.id = r.workerFk + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + WHERE 1(r.created) + GROUP BY w.id + + +SELECT COUNT(r.id) 'Rutas', + w.id 'Id Enrutador', + CONCAT(w.firstName, " " , w.lastName) Enrutador, + wc.id 'Id Centro', + wc.name 'Centro', + sub2.category Categoría, + SUM(rc.cat4m3) 'cat.IV Mensual', + SUM(rc.cat5m3) 'cat.V Mensual' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN(SELECT b.workerFk, b.workCenterFk, sub.category + FROM business b + JOIN (SELECT MAX(b.id) businessFk, wdc.category + FROM business b + JOIN workerDistributionCategory wdc ON wdc.workerFk = b.workerFk + JOIN vn.department AS d ON d.id = b.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + GROUP BY b.workerFK)sub ON sub.businessFk = b.id ) sub2 ON sub2.workCenterFk = r.commissionWorkcenterFk + JOIN worker w ON w.id = sub2.workerFk + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + WHERE 1(r.created) + GROUP BY w.id +SELECT COUNT(*) 'Rutas', + wc.id 'Id Centro', + wc.name 'Centro', + SUM(rc.km) 'Km Mensual', + SUM(rc.m3) 'M3 Mensual', + SUM(rc.yearlyKm) 'Km Anual', + SUM(rc.yearlyM3) 'M3 Anual' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN worker w ON w.id = r.workerFk + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + WHERE 1(r.created) + GROUP BY wc.id + + +SELECT COUNT(r.id) 'Rutas', + wc.id 'Id Centro', + wc.name 'Centro', + SUM(rc.cat4m3) * totalCat4 'Enruta cat.IV Mensual', + SUM(rc.cat5m3) * totalCat5 'Enruta cat.V Mensual' + FROM routeCommission rc + JOIN route r ON rc.routeFk = r.id + JOIN workCenter wc ON wc.id = r.commissionWorkcenterFk + JOIN ( +SELECT SUM(IF(sub.category = 1, 1, 0)) totalCat5, + SUM(IF(sub.category = 2, 1, 0)) totalCat4, + b.workCenterFk + FROM business b + JOIN (SELECT MAX(b.id) businessFk, wdc.category + FROM business b + JOIN workerDistributionCategory wdc ON wdc.workerFk = b.workerFk + JOIN vn.department AS d ON d.id = b.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + GROUP BY b.workerFK)sub ON sub.businessFk = b.id + GROUP BY b.workCenterFk + )sub2 ON sub2.workCenterFk = wc.id + WHERE 1(r.created) + GROUP BY wc.id +SELECT cr.description Responsable, +FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4)) Pérdidas +From vn.claim c +JOIN vn.claimEnd ce ON ce.claimFk = c.id AND ce.claimDestinationFk IN (2,3) +JOIN vn.sale ces ON ces.id = ce.saleFk +JOIN vn.claimDevelopment cd on cd.claimFk = c.id +JOIN vn.claimResponsible cr on cr.id = cd.claimResponsibleFk +JOIN vn.item i on i.id = ces.itemFk +where 1(c.created) +and not isnull(cr.description) +group by Responsable + + +SELECT 100 * FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4)) / v.facturado ratio, +FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4)) Pérdidas, + w.firstName Comprador, w.lastName Apellido, +v.facturado +From vn.claim c +JOIN vn.claimEnd ce ON ce.claimFk = c.id and ce.claimDestinationFk in (2,3) +JOIN vn.claimDevelopment cd on cd.claimFk = c.id +JOIN vn.sale ces ON ces.id = ce.saleFk +JOIN vn.item i on i.id = ces.itemFk +JOIN vn.itemType it on it.id = i.typeFk +JOIN vn.worker w on w.id = it.workerFk + JOIN (SELECT FLOOR(SUM(bs.amount)) facturado, it.workerFk + FROM bs.sale bs + JOIN vn.itemType it ON it.id = bs.typeFk + where 1(bs.dated) + GROUP BY it.workerFk + ) v on v.workerFk = w.id +where 1(c.created) +AND w.firstName NOT IN ('FRANCISCO DE ASIS','RICARDO','RUBEN') +group by w.id +order by ratio desc + +SELECT FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4)) Pérdidas, +100 * FLOOR(SUM((- ces.price * ces.quantity * (100 - ces.discount) / 100) * (5 - c.responsibility) / 4)) / v.facturado ratio, + w.firstName Comprador, w.lastName Apellido, +v.facturado +From vn.claim c +JOIN vn.claimEnd ce ON ce.claimFk = c.id and ce.claimDestinationFk in (2,3) +JOIN vn.claimDevelopment cd on cd.claimFk = c.id +JOIN vn.sale ces ON ces.id = ce.saleFk +JOIN vn.item i on i.id = ces.itemFk +JOIN vn.itemType it on it.id = i.typeFk +JOIN vn.worker w on w.id = it.workerFk + JOIN (SELECT FLOOR(SUM(bs.amount)) facturado, it.workerFk + FROM bs.sale bs + JOIN vn.itemType it ON it.id = bs.typeFk + where 1(bs.dated) + GROUP BY it.workerFk + ) v on v.workerFk = w.id +where 1(c.created) and cd.claimResponsibleFk = 1 +group by w.id +order by ratio desc + +SELECT +lv.LíneasVendidas 'Líneas Vendidas', +count(c.id) 'Líneas Reclamadas', +100 * count(c.id) / lv.LíneasVendidas 'Ratio Líneas', +td.Descuentos 'Descuentos', +td.DineroDescuentos 'Dinero Descuentos', +bs.FACTURADO 'FACUTRADO', +Floor( - sum((s.price * s.quantity) * (5 - c.responsibility) / 4)) 'Perdidas Reclamaciones', +( - sum((s.price * s.quantity) * (1-(5 - c.responsibility) / 4))) 'Greuge', +Floor( - sum((s.price * s.quantity) * (5 - c.responsibility)) / 4) + (- sum((s.price * s.quantity) * (1-(5 - c.responsibility) / 4))) + td.DineroDescuentos 'Total Dinero Reclamado', +Floor(( - sum((s.price * s.quantity) * (5 - c.responsibility)) / 4) + (- sum(s.price * s.quantity) * (1-(5 - c.responsibility) / 4)) + td.DineroDescuentos) / bs.FACTURADO * 100 'Ratio Dinero Reclamado' + +FROM vn.claim c +JOIN vn.claimEnd ce ON ce.claimFk = c.id +JOIN vn.sale s on ce.saleFk = s.id +JOIN vn.item i on i.id = s.itemFk +JOIN vn.itemType it on it.id = i.typeFk +JOIN (SELECT Count(s.id) LíneasVendidas + FROM vn.sale s + JOIN vn.item i on i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + WHERE 1(s.created) and it.name = '1' and s.quantity > '0') lv +Join (SELECT Count(s.discount) Descuentos, + Round(sum((s.quantity * s.price) * (s.discount / 100))) DineroDescuentos + FROM vn.sale s + Join vn.ticket tk on tk.id = s.ticketFk + JOIN vn.item i on i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + WHERE 1(tk.shipped) and it.name = '1' and s.discount > 0 and tk.clientFk not in (200,400) and s.quantity > '0') td +Join (SELECT Round(sum(bs.importe)) FACTURADO + FROM bs.ventas bs + JOIN vn.itemType it on it.id = bs.tipo_id + where 1(fecha) and it.name = '1') bs +WHERE 1(c.created) and it.name = '1' + + + + + + + + + + + +SELECT description FROM vn.claimResponsible +SELECT name FROM vn.itemType +SELECT COUNT(*) + FROM route r + LEFT JOIN vn.routeCommission rc ON rc.routeFk = r.id + LEFT JOIN vn.workCenter w ON w.id = r.commissionWorkCenterFk + JOIN routeConfig c + WHERE 1(r.created) + AND rc.id IS NULL + AND IF('1'='Sin asignar', w.name IS NULL, w.name = '1') + + +SELECT r.id 'Id Ruta', + DATE_FORMAT(r.created, "%d/%m/%Y") 'Fecha', + am.name 'Agencia', + IF(r.kmStart, 'Sí', 'No') 'Km Inicio', + IF(r.kmEnd, 'Sí', 'No') 'Km Fin', + IF(r.m3, 'Sí', 'No') m3, + IF(r.workerFk IS NULL, 'No', 'Sí') 'Trabajador', + IF(r.commissionWorkCenterFk IS NULL, 'No', 'Sí') 'Centro Trabajo', + IF(r.kmEnd-r.kmStart > c.kmMax, 'Sí', 'No') 'Km>4000' + FROM route r + LEFT JOIN vn.agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vn.agency a ON a.id = am.agencyFk + LEFT JOIN vn.routeCommission rc ON rc.routeFk = r.id + LEFT JOIN vn.workCenter w ON w.id = r.commissionWorkCenterFk + JOIN routeConfig c + WHERE 1(r.created) + AND rc.id IS NULL + AND IF('1'='Sin asignar', w.name IS NULL, w.name='1') + + SELECT id 'Id Ruta', + name 'Agencia', + DATE_FORMAT(created, "%d/%m/%Y") 'Gestión de la ruta', + DATE_FORMAT(dated, "%d/%m/%Y") 'Entrega Tickets' + FROM(SELECT r.id, + a.name, + r.created, + sub2.dated + FROM route r + LEFT JOIN vn.agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vn.agency a ON a.id = am.agencyFk + LEFT JOIN vn.workCenter w ON w.id = r.commissionWorkCenterFk + JOIN (SELECT DISTINCT routeFk, dated + FROM (SELECT COUNT(*) total, + DATE(landed) dated, + routeFk + FROM vn.ticket + WHERE routeFk + GROUP BY routeFk, DATE(shipped))sub + GROUP BY routeFk + ORDER BY routeFk, total DESC)sub2 ON sub2.routeFk = r.id + WHERE 1(r.created) + AND IF('1'='Sin asignar', w.name IS NULL, w.name='1') + HAVING MONTH(r.created) <> MONTH(sub2.dated) + ORDER BY r.id)sub2 +SELECT COUNT(*) + FROM(SELECT r.id, + a.name, + r.created, + sub2.dated + FROM route r + LEFT JOIN vn.agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vn.agency a ON a.id = am.agencyFk + LEFT JOIN vn.workCenter w ON w.id = r.commissionWorkCenterFk + JOIN (SELECT DISTINCT routeFk, dated + FROM (SELECT COUNT(*) total, + DATE(landed) dated, + routeFk + FROM vn.ticket + WHERE routeFk + GROUP BY routeFk, DATE(shipped))sub + GROUP BY routeFk + ORDER BY routeFk, total DESC)sub2 ON sub2.routeFk = r.id + WHERE 1(r.created) + AND IF('1'='Sin asignar', w.name IS NULL, w.name='1') + HAVING MONTH(r.created) <> MONTH(sub2.dated) + ORDER BY r.id)sub2 + +SELECT DISTINCT wc.name + FROM workerLabour wl + JOIN (SELECT MAX(businessFk) businessFk + FROM workerLabour wl + JOIN vn.department AS d ON d.id = wl.departmentFk + WHERE (IFNULL(ended, CURDATE()) BETWEEN util.firstDayOfYear(CURDATE()) AND CURDATE() OR (ended>CURDATE() AND started < CURDATE())) + AND d.name IN ('REPARTO') + GROUP BY wl.workerFK)sub ON sub.businessFk = wl.businessFk + JOIN workCenter wc ON wc.id = wl.workCenterFk +UNION SELECT 'Sin asignar' +SELECT + landed as time, + @saldo := @saldo + `in` - `out` saldo + FROM vn.supplierPackaging sp + JOIN (SELECT @saldo:= 0) sub + JOIN vn.supplier s ON s.id = sp.supplierFk + WHERE supplierFk = 1 + AND itemFk IN (96002,96000,2214) +ORDER BY landed +SELECT id FROM vn.supplier +SELECT + trackers.name AS metric, + estimated_hours +FROM issues i + JOIN trackers ON i.tracker_id = trackers.id + LEFT JOIN versions v ON i.fixed_version_id = v.id +WHERE [[Version]] = v.name +ORDER BY 1,2 +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'carlosjr' + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'carlosjr' + GROUP BY users.login + +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'joan' + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'joan' + GROUP BY users.login + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'alexm' + GROUP BY users.login + +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'alexm' + +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'vicent' + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'vicent' + GROUP BY users.login + +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'jgallego' + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'jgallego' + GROUP BY users.login + +SELECT + NOW() AS "time", + + issues_1.estimated_hours +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'juan' + +SELECT + NOW() AS "time", + SUM(time_entries.hours) expended + +FROM + issues + JOIN + versions v ON issues.fixed_version_id = v.id + JOIN + issue_statuses ON issues.status_id = issue_statuses.id + JOIN + users ON issues.assigned_to_id = users.id + JOIN + time_entries ON issues.id = time_entries.issue_id + JOIN + issues AS issues_1 ON issues.id = issues_1.id + WHERE [[Version]] = v.name and users.login = 'juan' + GROUP BY users.login + +select name from versions where status = 'open' and project_id = 5 +SELECT 0 'Volumen Octubre 2021' +SELECT sum(volume) '2022', now() time + FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk + WHERE 1(sv.shipped) + AND warehouseFk = 60 + +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date + + + + +SELECT SUM(m.m3) '1' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-01' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '2' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-02' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '3' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-03' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '4' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-04' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '5' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-05' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '6' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-06' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '7' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-07' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '8' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-08' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '9' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-09' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '10' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-10') + INTERVAL 1 DAY, '1-1-10') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT SUM(m.m3) '11' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-11' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '12' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-12' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '13' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-13' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '15' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-15' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '14' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-14' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha + LIMIT 1 +SELECT SUM(m.m3) '16' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-16' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '17' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-17' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '18' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-18' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '19' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-19' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '20' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-20' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '21' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-21' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '22' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-22' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '23' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-23' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '24' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-24' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '25' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-25' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '26' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-26' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '27' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-27' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '28' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-28' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '29' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-29' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '30' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-30' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '31' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-10-31' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '1' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-11-01' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '2' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-11-02' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT SUM(m.m3) '3' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + WHERE fecha = '2021-11-03' + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + GROUP BY m.fecha +SELECT sum(volume) '2022', now() time + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + WHERE sv.shipped BETWEEN '2022-10-01 00:00:00' AND '2022-10-31 23:59:59' + AND t.warehouseFk = 60 +SELECT 0 'Volumen 1 1' +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date + + + + + +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '1' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-01') + INTERVAL 1 DAY, '1-1-01') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '2' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-02') + INTERVAL 1 DAY, '1-1-02') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '3' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-03') + INTERVAL 1 DAY, '1-1-03') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '4' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-04') + INTERVAL 1 DAY, '1-1-04') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '5' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-05') + INTERVAL 1 DAY, '1-1-05') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '7' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-07') + INTERVAL 1 DAY, '1-1-07') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '6' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-06') + INTERVAL 1 DAY, '1-1-06') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '8' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-08') + INTERVAL 1 DAY, '1-1-08') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '9' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-09') + INTERVAL 1 DAY, '1-1-09') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT sum(volume) '2021', now() time + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + WHERE sv.shipped BETWEEN '2021-10-01 00:00:00' AND '2021-10-31 23:59:59' + AND t.warehouseFk = 60 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '10' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-10') + INTERVAL 1 DAY, '1-1-10') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '11' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-11') + INTERVAL 1 DAY, '1-1-11') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '12' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-12') + INTERVAL 1 DAY, '1-1-12') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '13' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-13') + INTERVAL 1 DAY, '1-1-13') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '14' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-14') + INTERVAL 1 DAY, '1-1-14') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '15' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-15') + INTERVAL 1 DAY, '1-1-15') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '16' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-16') + INTERVAL 1 DAY, '1-1-16') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '17' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-17') + INTERVAL 1 DAY, '1-1-17') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '18' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-18') + INTERVAL 1 DAY, '1-1-18') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '19' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-19') + INTERVAL 1 DAY, '1-1-19') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '20' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-20') + INTERVAL 1 DAY, '1-1-20') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '21' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-21') + INTERVAL 1 DAY, '1-1-21') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '22' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-22') + INTERVAL 1 DAY, '1-1-22') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '23' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-23') + INTERVAL 1 DAY, '1-1-23') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '24' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-24') + INTERVAL 1 DAY, '1-1-24') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '25' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-25') + INTERVAL 1 DAY, '1-1-25') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '26' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-26') + INTERVAL 1 DAY, '1-1-26') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '27' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-27') + INTERVAL 1 DAY, '1-1-27') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '28' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-28') + INTERVAL 1 DAY, '1-1-28') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '29' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-29') + INTERVAL 1 DAY, '1-1-29') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '30' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-30') + INTERVAL 1 DAY, '1-1-30') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT * + FROM (SELECT IF(LEFT('1', 9) = 'Previsión', IFNULL(floor(sum(m3)),0) * 1.15 ,IFNULL(floor(sum(m3)),0)) '31' + FROM bs.m3 m + JOIN vn.warehouse w ON w.id = m.warehouseFk + JOIN vn.province p ON p.id = m.provinceFk + WHERE fecha = IF(LEFT('1', 9) = 'Previsión', CONCAT(YEAR(CURDATE() - INTERVAL 1 YEAR), '-1-31') + INTERVAL 1 DAY, '1-1-31') + AND w.name IN('Algemesi', 'KK Silla FV', 'KK Silla PCA') + AND p.name IN(1) + UNION ALL + SELECT 0) sub + LIMIT 1 +SELECT DISTINCT `year` + FROM vn.`time` t + WHERE `year` BETWEEN 2017 AND YEAR(CURDATE()) +UNION ALL +SELECT CONCAT('Previsión ', YEAR(CURDATE())) +1 - Enero, 2 - Febrero, 3 - Marzo, 4 - Abril, 5 - Mayo, 6 - Junio, 7 - Junio, 8 - Agosto, 9 - Septiembre, 10 - Octubre, 11 - Noviembre, 12 - Diciembre +SELECT SUBSTRING('1', 5) +SELECT TRIM(LEFT('1', 2)) +SELECT name + FROM vn.province +SELECT TRIM(LEFT('1', 2)) +SELECT SUM(id < 0) Incorrectas, COUNT(id) Correctas +FROM srt.expedition +WHERE 1(created) AND stateFk = 5 + +SELECT COUNT(*) +FROM srt.expedition +WHERE 1(created) AND stateFk = 5 +SELECT sum(id mod 10 = -2) fallo2, sum(id mod 10 = 0) fallo0, sum(id mod 10 = -9) fallo9 +FROM srt.expedition +WHERE 1(created) and id < 0 AND stateFk = 5 + + + + +SELECT COUNT(*) Total,bufferFk +FROM srt.expeditionLog expLog +WHERE 1(expLog.created) AND expLog.action = "OUT" +GROUP BY bufferFk + +SELECT COUNT(*) Cantidad +FROM srt.`failureLog` fLog +WHERE 1(fLog.created) + + +SELECT COUNT(*) Desconocido,description +FROM srt.`failureLog` fLog +WHERE 1(fLog.created) +GROUP BY description + + +SELECT DATE_FORMAT(e.created, '%H:%i:%s - %d/%m') expCreation, el.bufferFk buffer, DATE_FORMAT(el.created, '%H:%i:%s - %d/%m') dateLog, el.`action`, CONCAT(el.antennaFk, " (", a.description, ")") antenna +FROM srt.expedition e +JOIN srt.expeditionLog el ON el.expeditionFk = e.id +LEFT JOIN srt.antenna a ON a.id = el.antennaFk +WHERE e.id = 1 +ORDER BY datelog +; + +SELECT CAST(lr.code AS UNSIGNED) expedition, + DATE_FORMAT(lr.created, '%H:%i:%s - %d/%m') created, + CONCAT(RIGHT(a.description,length(a.description) -locate(' ',a.description)), '(', lr.antennaFk, ')') antenna, + lr.antennaPort port, + lr.isChoosed choosed, + lr.peakRssi, + lr.seenCount +FROM srt.lastRFID lr +JOIN srt.antenna a ON a.id = lr.antennaFk +WHERE lr.created IN (SELECT DISTINCT lr2.created + FROM srt.lastRFID lr2 + WHERE TRIM(LEADING '0' FROM lr2.code) = 1) +; + +SELECT concat(a.description, " (", a.id, ")") antena, lr.antennaPort puerto, DATE_FORMAT(max(lr.created),'%d/%m %H:%i:%s') "ultima lectura", COUNT(*) "lecturas/dia" +FROM srt.lastRFID lr +left JOIN srt.antenna a ON a.id = lr.antennaFk +WHERE created >= CURDATE() + INTERVAL 4 HOUR +GROUP BY lr.antennaFk, lr.antennaPort +SELECT DATE_FORMAT(lr.created,'%d/%m %H:%i:%s') hora, code expedicion, t.routeFk mod 1000 ruta, t.clientFk, antennaFk antena, antennaPort puerto, peakRssi, seenCount, ischoosed +FROM srt.lastRFID lr +LEFT JOIN vn.expedition e on e.id = lr.code +LEFT JOIN vn.ticket t on t.id = e.ticketFk +ORDER BY lr.id desc +LIMIT 100 +; +SELECT e.id mod 1000 as Expedition, + p.id box, + e.width 'Width', p.width*10 'Width(T)', + e.`length` 'Length', p.`depth`*10 'Length(T)', + e.height 'Height', p.height*10 'Height(T)', + DATE_FORMAT(el.created, '%H:%i:%s %d/%m ') as 'Last Update', + el.antennaFk as 'Antenna' +FROM srt.expeditionLog el +JOIN srt.expedition e ON e.id = el.expeditionFk +JOIN vn.expedition e2 ON e2.id = e.id +JOIN vn.packaging p ON p.id = e2.packagingFk +WHERE el.antennaFk is not null +ORDER BY updated DESC +; + +SELECT DATE_FORMAT(fecha,'%d/%m') fecha, + total, + CAST(100 * noLectura / total AS DECIMAL(4,1)) NoLectura, + CAST(100 * (total - noLectura - lecturaCorrecta) / total AS DECIMAL(4,1)) otrosFallos, + CAST(100 * lecturaCorrecta / total AS DECIMAL(4,1)) lecturaCorrecta + FROM ( + SELECT fecha, + count(*) total, + sum(noLectura) noLectura, + sum(lecturaCorrecta) lecturaCorrecta, + /*el.antennaFk antena*/ + CONCAT(a.description, " (", el.antennaFk, ")") antena + FROM ( + SELECT date(created) fecha, + id, + (id <= 0 ) noLectura, + id > 0 lecturaCorrecta + FROM srt.expedition e + ) sub + JOIN srt.expeditionLog el ON el.expeditionFk = sub.id + LEFT JOIN srt.antenna a ON a.id = el.antennaFk + GROUP BY fecha DESC + ) sub2; +SELECT COUNT(*) Value, antennaFk +FROM srt.lastRFID lRFID +WHERE 1(lRFID.created) AND isChoosed="1" +GROUP BY antennaFk +Select id FROM srt.expedition ORDER BY created desc +SELECT DATE_FORMAT(e.created, '%H:%i:%s - %d/%m') expCreation, el.bufferFk buffer, DATE_FORMAT(el.created, '%H:%i:%s - %d/%m') dateLog, el.`action`, CONCAT(el.antennaFk, " (", a.description, ")") antenna +FROM srt.expedition e +JOIN srt.expeditionLog el ON el.expeditionFk = e.id +LEFT JOIN srt.antenna a ON a.id = el.antennaFk +WHERE e.id = 1 +ORDER BY datelog +; + +SELECT CAST(lr.code AS UNSIGNED) expedition, + DATE_FORMAT(lr.created, '%H:%i:%s - %d/%m') created, + CONCAT(RIGHT(a.description,length(a.description) -locate(' ',a.description)), '(', lr.antennaFk, ')') antenna, + lr.antennaPort port, + lr.isChoosed choosed, + lr.peakRssi, + lr.seenCount +FROM srt.lastRFID lr +JOIN srt.antenna a ON a.id = lr.antennaFk +WHERE lr.created IN (SELECT DISTINCT lr2.created + FROM srt.lastRFID lr2 + WHERE TRIM(LEADING '0' FROM lr2.code) = 1) +; + +SELECT concat(a.description, " (", a.id, ")") antena, lr.antennaPort puerto, DATE_FORMAT(max(lr.created),'%d/%m %H:%i:%s') "ultima lectura", COUNT(*) "lecturas/dia" +FROM srt.lastRFID lr +left JOIN srt.antenna a ON a.id = lr.antennaFk +WHERE created >= CURDATE() + INTERVAL 4 HOUR +GROUP BY lr.antennaFk, lr.antennaPort +SELECT DATE_FORMAT(created,'%d/%m %H:%i:%s') hora, code expedicion, antennaFk antena, antennaPort puerto, peakRssi, seenCount, ischoosed +FROM srt.lastRFID +ORDER BY id desc +LIMIT 100 +; +SELECT m.expeditionFk MOD 1000 EXP, + t.routeFk MOD 1000 route, + IFNULL(et.description, am.name) destino, + bufferFromFk, bufferToFk, ms.description, isColliding, + TIME(m.created) hhmm +FROM srt.moving m +JOIN srt.movingState ms ON ms.id = m.stateFk +LEFT JOIN vn.expedition e ON e.id = m.expeditionFk +LEFT JOIN vn.ticket t ON t.id = e.ticketFk +LEFT JOIN vn.agencyMode am ON am.id = t.agencyModeFk +LEFT JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk +LEFT JOIN vn.expeditionTruck et ON et.id = rm.expeditionTruckFk +ORDER BY hhmm + +SELECT created, value FROM util.debug WHERE variable = 'buffer_readPhotocell' AND value LIKE 'buffer:35%' ORDER BY created DESC +LIMIT 3; + +SELECT e.id mod 1000 as Expedition, + p.id box, + e.width 'Width', p.width*10 'Width(T)', + e.`length` 'Length', p.`depth`*10 'Length(T)', + e.height 'Height', p.height*10 'Height(T)', + DATE_FORMAT(el.created, '%H:%i:%s %d/%m ') as 'Last Update', + el.antennaFk as 'Antenna' +FROM srt.expeditionLog el +JOIN srt.expedition e ON e.id = el.expeditionFk +JOIN vn.expedition e2 ON e2.id = e.id +JOIN vn.packaging p ON p.id = e2.packagingFk +WHERE el.antennaFk is not null +ORDER BY updated DESC +; + +SELECT DATE_FORMAT(fecha,'%d/%m') fecha, + total, + CAST(100 * noLectura / total AS DECIMAL(4,1)) NoLectura, + CAST(100 * (total - noLectura - lecturaCorrecta) / total AS DECIMAL(4,1)) otrosFallos, + CAST(100 * lecturaCorrecta / total AS DECIMAL(4,1)) lecturaCorrecta, + antena + FROM ( + SELECT fecha, + count(*) total, + sum(noLectura) noLectura, + sum(lecturaCorrecta) lecturaCorrecta, + /*el.antennaFk antena*/ + CONCAT(a.description, " (", el.antennaFk, ")") antena + FROM ( + SELECT date(created) fecha, + id, + (id < 0 AND id MOD 10 = 0) noLectura, + id > 0 lecturaCorrecta + FROM srt.expedition e + ) sub + JOIN srt.expeditionLog el ON el.expeditionFk = sub.id + LEFT JOIN srt.antenna a ON a.id = el.antennaFk + GROUP BY fecha DESC, el.antennaFk + ) sub2; +SELECT DATE_FORMAT(e.fecha,'%d/%m') fecha, e.total cajas, f.fallos, floor (1000 * f.fallos / e.total) fallosx1000 + FROM (SELECT date(created) fecha, count(*) total + FROM srt.expedition e + GROUP BY fecha) e + LEFT JOIN (SELECT date(created) fecha, count(*) fallos + FROM srt.failureLog fl + WHERE LEFT(description,13) = 'De celluveyor' + GROUP BY fecha + ) f USING(fecha) + ORDER BY fecha desc; +SELECT DATE_FORMAT(fecha,'%d/%m') fecha, + total, + CAST(100 * noLectura / total AS DECIMAL(4,1)) NoLectura, + CAST(100 * dobleLectura / total AS DECIMAL(4,1)) dobleLectura, + CAST(100 * (total - noLectura - dobleLectura - lecturaCorrecta) / total AS DECIMAL(4,1)) otrosFallos, + CAST(100 * lecturaCorrecta / total AS DECIMAL(4,1)) lecturaCorrecta + FROM ( + SELECT fecha, + count(*) total, + sum(noLectura) noLectura, + sum(dobleLectura) dobleLectura, + sum(lecturaCorrecta) lecturaCorrecta + FROM ( + SELECT date(created) fecha, + id, + (id < 0 AND id MOD 10 = 0) noLectura, + (id < 0 AND id MOD 10 = -2) dobleLectura, + id > 0 lecturaCorrecta + FROM srt.expedition e + ) sub + GROUP BY fecha DESC + ) sub2; +SELECT count(*) as cajas, + `e`.`bufferFk` +FROM `srt`.`expedition` `e` + LEFT JOIN `srt`.`buffer` `b` ON `b`.`id` = `e`.`bufferFk` + WHERE e.bufferFk AND e.bufferFk mod 2 = 0 + group BY `e`.`bufferFk`; +SELECT `e`.`bufferFk`, + count(*) as cajas +FROM `srt`.`expedition` `e` + LEFT JOIN `srt`.`buffer` `b` ON `b`.`id` = `e`.`bufferFk` + LEFT JOIN `vn`.`expedition` `ve` ON `ve`.`id` = `e`.`id` + LEFT JOIN vn.worker w ON w.id = ve.workerFk + LEFT JOIN `vn`.`ticket` `t` ON `t`.`id` = `ve`.`ticketFk` + LEFT JOIN `vn`.`agencyMode` `am` ON `am`.`id` = `t`.`agencyModeFk` + LEFT JOIN `vn`.`zone` `z` ON `z`.`id` = `t`.`zoneFk` + LEFT JOIN `vn`.`route` `r` ON `r`.`id` = `t`.`routeFk` + LEFT JOIN vn.agencyMode am2 ON am2.id = r.agencyModeFk + LEFT JOIN `vn`.`routesMonitor` `rm` ON `rm`.`routeFk` = `t`.`routeFk` + LEFT JOIN `vn`.`expeditionTruck` `et` ON `et`.`id` = `rm`.`expeditionTruckFk` + WHERE e.bufferFk AND e.bufferFk mod 2 = 1 AND NOT b.hasWorkerWaiting + group BY `e`.`bufferFk`; + +SELECT b.id bufferFk, + b.hasWorkerWaiting, + b.isActive, + bs.description state, + bt.typeName `type`, + b.routeFk mod 10000 route, + b.dayMinute, + IFNULL(et.description, am.name) truck +FROM `srt`.`buffer` `b` + JOIN srt.bufferState bs on bs.id = b.stateFk + JOIN srt.bufferType bt on bt.id = b.typeFk + LEFT JOIN vn.routesMonitor rm on rm.routeFk = b.routeFk + LEFT JOIN vn.expeditionTruck et on et.id = rm.expeditionTruckFk + LEFT JOIN vn.agencyMode am ON am.id = b.routeFk + WHERE b.hasWorkerWaiting + order by b.id + +SELECT + `e`.`bufferFk`, e.id mod 1000 exp, + TIME_FORMAT(IFNULL(et .`ETD`, z.`hour`),'%H:%i') AS `truckETD`, + ifnull(`et`.`description`, `am`.`name`) AS `camion`, + am2.name AS zoneRuta, + `t`.`routeFk` MOD 1000 AS `routeFk` +FROM `srt`.`expedition` `e` + LEFT JOIN `srt`.`buffer` `b` ON `b`.`id` = `e`.`bufferFk` + LEFT JOIN `vn`.`expedition` `ve` ON `ve`.`id` = `e`.`id` + LEFT JOIN vn.worker w ON w.id = ve.workerFk + LEFT JOIN `vn`.`ticket` `t` ON `t`.`id` = `ve`.`ticketFk` + LEFT JOIN `vn`.`agencyMode` `am` ON `am`.`id` = `t`.`agencyModeFk` + LEFT JOIN `vn`.`zone` `z` ON `z`.`id` = `t`.`zoneFk` + LEFT JOIN `vn`.`route` `r` ON `r`.`id` = `t`.`routeFk` + LEFT JOIN vn.agencyMode am2 ON am2.id = r.agencyModeFk + LEFT JOIN `vn`.`routesMonitor` `rm` ON `rm`.`routeFk` = `t`.`routeFk` + LEFT JOIN `vn`.`expeditionTruck` `et` ON `et`.`id` = `rm`.`expeditionTruckFk` + WHERE e.bufferFk + ORDER BY + IFNULL(`e`.`bufferFk` ,0) = 0, + `e`.`bufferFk`, + `e`.`position`; +SELECT distinct DATE_FORMAT(m.created,'%d/%m %H:%i:%s') hora, expeditionFk mod 1000 expedition, bufferFromFk 'From', bufferToFk 'To' +FROM srt.movingLog m +ORDER BY m.id desc +LIMIT 10 +; +SELECT stock ,bufferName, NOW() time_sec +FROM srt.bufferFreeLength +where bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc; + + +SELECT saturacion ,bufferName, NOW() time_sec +FROM srt.bufferFreeLength +where bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc; + + +SELECT bufferName, stateFk, NOW() as time_sec +FROM srt.bufferFreeLength +where bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc + + +SELECT bufferName, typeFk, NOW() as time_sec +FROM srt.bufferFreeLength +where bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc;; + + +SELECT bufferName, saturacion ,NOW() time_sec +FROM srt.bufferFreeLength +where NOT bufferFk mod 2 +and bufferFk > 1 +order by bufferFk desc; + + +SELECT stock ,bufferName, NOW() time_sec +FROM srt.bufferFreeLength +where NOT bufferFk mod 2 +and bufferFk > 1 +order by bufferFk desc; + + +SELECT bufferName, stateFk, NOW() as time_sec +FROM srt.bufferFreeLength +where NOT bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc;; + + +SELECT bufferName, typeFk, NOW() as time_sec +FROM srt.bufferFreeLength +where NOT bufferFk mod 2 and bufferFk > 1 +order by bufferFk desc;; + + +Select id FROM srt.expedition ORDER BY created desc + SELECT IFNULL(floor(sum(volume)),0) 'Volumen Total' + FROM vn.saleVolume sv + JOIN vn.ticketState ts ON ts.ticketFk = sv.ticketFk + JOIN vn.ticket t ON t.id = ts.ticketFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE 1(sv.shipped) + AND w.code = 'ALG' + AND NOT am.name = 'ABONO' +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT CAST(SUM(volume) AS DECIMAL(10,2)), d.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user us ON us.id = w.bossFk + WHERE sv.shipped BETWEEN '2022-10-28' AND '2022-10-29 23:59:59' + AND d.name LIKE 'EQUIPO %' + GROUP BY d.name + ORDER BY SUM(volume) DESC +SELECT t.clientFk Cliente, + t.id Ticket, + MAX(tt.created) Entregado, + z.name Zona, + t.routeFk Ruta, + am.name Agencia + + FROM vn.ticket t + JOIN ticketTracking tt ON tt.ticketFk = t.id + JOIN state s ON s.id = tt.stateFk + JOIN vn.zone z ON z.id = t.zoneFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + WHERE s.code = 'DELIVERED' + AND 1(tt.created) + GROUP BY t.id +SELECT + w.code, w.firstName, w.lastName, + floor(sum(IF(c.code = 'mana', sc.value,0) * s.quantity)) Descuento, + floor(sum(IF(c.code = 'manaClaim', sc.value,0) * s.quantity)) Reclamación, + floor(sum(IF(c.code = 'autoMana', sc.value,0) * s.quantity)) Auto + FROM vn.ticket t + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + JOIN vn.worker w on w.id = cl.salesPersonFk + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE ct.code = 'MANA' + AND 1(t.shipped) + AND w.code IN (1) + GROUP BY w.id + +SELECT + a.clientFk, + cl.socialName, + floor(sum(IF(c.code = 'mana', sc.value,0) * s.quantity)) Descuento, + floor(sum(IF(c.code = 'manaClaim', sc.value,0) * s.quantity)) Reclamación, + floor(sum(IF(c.code = 'autoMana', sc.value,0) * s.quantity)) Auto + FROM vn.ticket t + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + JOIN vn.worker w on w.id = cl.salesPersonFk + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE ct.code = 'MANA' + AND 1(t.shipped) + AND w.code IN (1) + GROUP BY a.clientFk + +SELECT + s.ticketFk, + cl.socialName, + floor(sum(IF(c.code = 'mana', sc.value,0) * s.quantity)) Descuento, + floor(sum(IF(c.code = 'manaClaim', sc.value,0) * s.quantity)) Reclamación, + floor(sum(IF(c.code = 'autoMana', sc.value,0) * s.quantity)) Auto + FROM vn.ticket t + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + JOIN vn.worker w on w.id = cl.salesPersonFk + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE ct.code = 'MANA' + AND 1(t.shipped) + AND w.code IN (1) + GROUP BY s.ticketFk + +SELECT DISTINCT w.code FROM vn.worker w JOIN vn.client c ON c.salesPersonFk = w.id +SELECT + SUM(IF (año = 2020, importe,0)) AS '2020', + SUM(IF (año = 2021, importe,0)) AS '2021', + NOW() time_sec + FROM bs.vendedores v + WHERE v.año > 2019 AND v.mes=2 + +SELECT SUM(importe) total, u.name, now() time_sec + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND cn.firstShipped >= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + AND d.name LIKE 'EQUIPO%' + GROUP BY u.id + ORDER BY total DESC + +SELECT u.name, SUM(importe) amount, NOW() time_sec + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN account.user u ON u.id = c.salespersonFk + JOIN vn.agencyMode am ON am.id = t.agencyModeFk + JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk + JOIN vn.sale s ON s.ticketFk=t.id + JOIN bs.ventas v ON v.Id_Movimiento = s.id + JOIN vn.itemType it ON it.id = v.tipo_id + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND it.itemPackingTypeFk = 'F' + AND dm.code NOT IN ('AGENCY') + AND u.name NOT IN ('ismaelalcolea', 'ruben', 'salva', 'david', 'ralvarez', 'enric', 'pacorico', 'pako', 'pepe', 'antoniomp', 'juanvi') + GROUP BY u.name + ORDER BY amount DESC +SELECT SUM(importe) total, u.name, now() time_sec + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + JOIN province p ON p.id = c.provinceFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.fecha >= util.firstDayOfMonth(1()) + AND cn.firstShipped >= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + AND d.name LIKE 'EQUIPO%' + AND p.name IN ('Granada', 'Jaén', 'Córdoba', 'Cádiz', 'Huelva', 'Cáceres', 'Badajoz', 'Girona') + GROUP BY u.id + ORDER BY total DESC + +SELECT (sub1.importe-sub2.importe) / sub5.peopleMinSale diferencia, CONCAT(sub1.name,'(',sub5.peopleMinSale ,'/', sub3.countPeople,')'), now() time_sec + FROM (SELECT SUM(importe) importe, name, año, departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale, u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale OR u.id IN (9873, 5021) + )sub + GROUP BY sub.departmentFk + )sub1 + JOIN ( SELECT SUM(v.portfolioWeight) importe, d.name, wd.departmentFk + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1())-1 AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP by wd.departmentFk + ORDER by importe DESC + )sub2 ON sub1.name = sub2.name + JOIN ( SELECT COUNT(*) countPeople, wd.departmentFk + FROM vn.salesPersonSince sps + JOIN vn.workerDepartment wd ON sps.workerFk = wd.workerFk + WHERE util.firstDayOfMonth(sps.started) <= util.firstDayOfMonth(DATE_ADD(1(), INTERVAL -1 year)) + GROUP BY wd.departmentFk + ) sub3 ON sub3.departmentFk = sub1.departmentFk + LEFT JOIN (SELECT COUNT(*) peopleMinSale, sub4.departmentFk + FROM (SELECT SUM(v.portfolioWeight) importe, d.name, v.año, wd.departmentFk,(40000 * DAYOFMONTH(1()))/DAYOFMONTH(LAST_DAY(1())) minSale,u.id + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND mes = MONTH(1()) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minsale OR u.id IN (9873, 5021) + )sub4 + GROUP BY sub4.departmentFk + )sub5 ON sub5.departmentFk = sub1.departmentFk + ORDER BY diferencia DESC +/*SELECT SUM(importe) total, u.name, now() time_sec + + FROM bs.clientNewBorn cn + JOIN bs.ventas v ON DATE_ADD(cn.firstShipped, INTERVAL 1 YEAR) > v.fecha AND v.Id_Cliente = cn.clientFk + JOIN vn.client c ON c.id = v.Id_Cliente + JOIN account.user u ON u.id = c.salesPersonFk + WHERE v.fecha >= util.quarterFirstDay(YEAR(1()),1) + AND cn.firstShipped >= util.quarterFirstDay(YEAR(1())-1,1) + AND u.role = 18 AND u.name NOT IN ('ismaelalcolea', 'ruben') + GROUP BY u.id + ORDER BY total DESC + limit 20*/ +/*SELECT (sub1.importe-sub2.importe) / sub5.peopleMinSale diferencia, CONCAT(sub1.name,'(', sub5.peopleMinSale, '/', sub3.countPeople,')'), now() time_sec + FROM (SELECT SUM(importe) importe, name, departmentFk + FROM(SELECT SUM(v.importe) importe, d.name, v.año, wd.departmentFk, (200000 * DATEDIFF(1(),'2020-07-01'))/153 minSale + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minSale + ORDER BY importe DESC + ) sub + GROUP BY sub.departmentFk + ) sub1 + JOIN ( SELECT SUM(v.portfolioWeight) importe, d.name, wd.departmentFk + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1())-1 AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP by wd.departmentFk + ORDER by importe DESC + )sub2 ON sub1.name = sub2.name + JOIN (SELECT COUNT(*) countPeople, wd.departmentFk + FROM vn.salesPersonSince sps + JOIN vn.workerDepartment wd ON sps.workerFk = wd.workerFk + GROUP BY wd.departmentFk + ) sub3 ON sub3.departmentFk = sub1.departmentFk + JOIN (SELECT COUNT(*) peopleMinSale, sub4.departmentFk + FROM (SELECT SUM(v.importe) importe, d.name, v.año, wd.departmentFk, (200000 * DATEDIFF(1(),'2020-07-01'))/153 minSale + FROM bs.vendedores v + JOIN account.user u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d On d.id = wd.departmentFk + WHERE v.año = YEAR(1()) AND v.mes IN (7,8,9,10,11) AND + d.name LIKE 'EQUIPO%' + GROUP BY v.Id_Trabajador + HAVING importe >= minSale + ORDER BY importe DESC + )sub4 + GROUP BY sub4.departmentFk + ) sub5 ON sub5.departmentFk = sub1.departmentFk + ORDER BY diferencia DESC*/ +SELECT + SUM(IF (año = 2019, importe,0)) AS '2019', + SUM(IF (año = 2020, importe,0)) AS '2020', + SUM(IF (año = 2021, importe,0)) AS '2021', + NOW() time_sec + FROM bs.vendedores v + WHERE v.año > 2017 AND v.mes >= 1 AND v.mes <= 11 +SELECT SUM(sqs.amount) importe, + u.name, + NOW() time_sec + FROM bs.salesMonthlySnapshot sqs + JOIN account.user u ON u.id = sqs.workerFk + JOIN vn.`time` t ON t.dated = DATE(1()) + JOIN (SELECT salesYear + FROM `time` + WHERE `month` = MONTH(1()) AND `year` = YEAR(1()) + LIMIT 1) sub1 + WHERE sub1.salesYear = t.salesYear + GROUP by sqs.workerFk + ORDER by importe DESC + LIMIT 15 +SELECT sg.`rank` 'Rango', + sg.walletWeightFrom 'Peso cartera desde', + sg.walletWeightTo 'Peso cartera hasta', + sg1.goal 'GRADO 1 (GRUPAL)', + sg2.goal 'GRADO 2', + sg3.goal 'GRADO 3', + sg4.goal 'GRADO 4 (INCENTIVO)', + sg5.goal 'GRADO 5' + FROM vn.saleGoal sg + JOIN vn.saleGoal sg1 ON sg.`rank` = sg1.`rank` AND sg1.goalType = '1' AND sg1.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg.`rank` = sg2.`rank` AND sg2.goalType = '2' AND sg2.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg.`rank` = sg3.`rank` AND sg3.goalType = '3' AND sg3.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg.`rank` = sg4.`rank` AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg.`rank` = sg5.`rank` AND sg5.goalType = '5' AND sg5.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + + WHERE sg.year = YEAR(1()) AND sg.`month` = MONTH(1()) + GROUP BY sg.`rank`; +SELECT saleGoals.rank 'Rango', + salesPerson.name 'Comercial', + CAST(portfolioWeight.amount AS DECIMAL(10,2)) 'Peso Cartera', + CAST(@grade1PercentageEvaluableObjective := IFNULL(spg.goal, saleGoals.averageIncreaseGrade1 + portfolioWeight.amount)AS DECIMAL(10,2)) 'Grado 1 (GRUPAL)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade2 + portfolioWeight.amount) AS DECIMAL(10,2))'Grado 2', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade3 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 3', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade4 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 4 (INCENTIVO)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade5 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 5', + CAST(@grade1PercentageEvaluableObjective - (((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE())))) * 100) AS DECIMAL(10,2)) 'Cómo lo llevo', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalGroup/100) AS DECIMAL(10,2))'Objetivo Evaluable (Grupo)', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100)AS DECIMAL(10,2)) 'Objetivo Incentivo', + CAST(currentSale.amount AS DECIMAL(10,2))*0.98 'Venta hasta Hoy', + -- CAST(futureSale.amount AS DECIMAL(10,2)) 'Venta a futuro', + CAST(@grade1PercentageEvaluableObjective - (currentSale.amount)*0.98 AS DECIMAL(10,2)) 'Importe hasta Evaluable', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100) - currentSale.amount AS DECIMAL(10,2)) 'Importe hasta Incentivo', + salesPerson.equipo 'Equipo' + FROM ( + SELECT u.name,d.name 'equipo', + u.id, + IF( us.name IN('miriam','mmar'), 'miriam-mmar', us.name) bossFk + FROM vn.worker w + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + )salesPerson + LEFT JOIN ( + SELECT IFNULL(SUM(amount),0) amount, s.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + WHERE dated BETWEEN 1() AND 1() + GROUP BY salesPersonFk + )currentSale ON salesPerson.id = currentSale.salesPersonFk + LEFT JOIN ( + SELECT IFNULL(sg.rank,1) rank, + sg1.goal goalGroup, + sg4.goal goalIncentive, + IFNULL(sub.portfolioWeight,0) amount, + workerFk + FROM (SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.lastSalesPersonFk workerFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY `year`, `month`, c.lastSalesPersonFk + ) sub + LEFT JOIN account.`user` u ON u.id = sub.workerFk + LEFT JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND sub.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + JOIN vn.saleGoal sg1 ON sg1.`rank` = IFNULL(sg.`rank`,1) + AND sg1.goalType = 1 + AND sg1.`year` = YEAR(1()) + AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = IFNULL(sg.`rank`,1) + AND sg4.goalType = 4 + AND sg4.`year` = YEAR(1()) + AND sg4.`month` = MONTH(1()) + WHERE sub.`year` = YEAR(1())-1 AND sub.`month` = MONTH(1()) + GROUP BY u.id + ) portfolioWeight ON portfolioWeight.workerFk = currentSale.salesPersonFk + /*LEFT JOIN (SELECT SUM(s.quantity * sc.value) amount, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + WHERE t.shipped BETWEEN 1() AND 1() + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + GROUP BY cl.salesPersonFk + HAVING amount AND cl.salesPersonFk + ) futureSale ON futureSale.salesPersonFk = salesPerson.id*/ + LEFT JOIN ( + SELECT `rank`, + SUM(IF(goalType = 1, amount, 0)) averageIncreaseGrade1, + SUM(IF(goalType = 2, amount, 0)) averageIncreaseGrade2, + SUM(IF(goalType = 3, amount, 0)) averageIncreaseGrade3, + SUM(IF(goalType = 4, amount, 0)) averageIncreaseGrade4, + SUM(IF(goalType = 5, amount, 0)) averageIncreaseGrade5 + FROM ( + SELECT rank, + goalType, + (SUM(IFNULL(portfolioWeight.amount,0))* goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) amount + FROM (SELECT c.lastSalesPersonFk salespersonFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + JOIN vn.department d2 ON d2.id = d.parentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + AND d2.code = 'VT' + GROUP BY c.lastSalesPersonFk)portfolioWeight + JOIN saleGoal sg ON sg.`year`= YEAR(1()) AND sg.`month` = MONTH(1()) AND portfolioWeight.amount BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE sg.`rank` = sg.rank AND portfolioWeight.amount BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + AND goalType BETWEEN 1 AND 5 + AND `year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + GROUP BY rank, goalType + )sub + GROUP BY rank + ) saleGoals ON saleGoals.`rank`= portfolioWeight.rank + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = salesPerson.name + AND spg.`year` = YEAR(1()) + AND spg.`month` = MONTH(1() ) +GROUP BY salesPerson.id +ORDER BY salesPerson.name; +SELECT SUM(s.quantity * sc.value)*0.98 amount, + u.name 'COMERCIAL' + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + JOIN account.user u ON u.id = cl.salesPersonFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + AND u.name NOT IN ('pako','jstegarescu','jordi','pacorico','jfrau','ruben','ismaelalcolea') + AND ct.isBase + GROUP BY cl.salesPersonFk + HAVING amount AND cl.salesPersonFk + ORDER BY u.name ASC +SELECT u.name + FROM account.user u + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = u.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + WHERE d.name LIKE '%EQUIPO %' OR u.name IN ('ruben','ismaelalcolea','josemp') +SELECT + rank AS "Rango", + walletWeightFrom AS "Peso cartera desde", + walletWeightTo AS "Peso cartera hasta", + goal AS "Objetivo" +FROM vn.saleGoal +WHERE year=YEAR(1()) And month=MONTH(1()) AND goalType='grupo' +SELECT + rank AS "Rango", + walletWeightFrom AS "Peso cartera desde", + walletWeightTo AS "Peso cartera hasta", + goal AS "Objetivo" +FROM vn.saleGoal +WHERE year=YEAR(1()) And month=MONTH(1()) AND goalType='individual' +-- Tabla jefes +SELECT + rank AS 'Rango', + salesPersonName AS 'Comercial', + walletWeigth AS 'Peso Cartera', + percentageEvaluableObjective AS '% del Evaluable', + percentageIncentiveObjective AS '% del Incentivo', + percentageEvaluableObjective-(((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE()))))*100) AS 'Cómo lo llevo', + evaluableObjectiveGroup AS 'Objetivo Evaluable (Grupo)', + incentiveSalesObjective AS 'Objetivo Incentivo', + currentSale AS 'Venta hasta Hoy', + amountToEvaluableObjective AS 'Importe hasta Evaluable', + amountToIncentiveObjective AS 'Importe hasta Incentivo', + bossFk AS 'Equipo' + FROM( + SELECT + bossFk, + sub5.salesPersonName, + id, + rank, + walletWeigth, + evaluableObjectiveGroup, + incentiveSalesObjective, + currentSale, + evaluableObjectiveGroup-currentSale amountToEvaluableObjective, + incentiveSalesObjective-currentSale amountToIncentiveObjective, + (currentSale/evaluableObjectiveGroup)*100 AS percentageEvaluableObjective, + (currentSale/incentiveSalesObjective)*100 AS percentageIncentiveObjective, + NULL AS today, + futureSale + FROM + ( + SELECT + bossFk, + subb.id, + subb.name AS salesPersonName, + walletWeigth, + IFNULL(spg.goal, walletWeigth*goalGroup/100) AS evaluableObjectiveGroup, + IFNULL(spg.goal, walletWeigth*goalIncentive/100) AS incentiveSalesObjective, + currentSale, + futureSale, + sub3.rank AS rank + FROM + (SELECT u.name, + u.id + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %') subb + LEFT JOIN ( + SELECT + YEAR(dated) year, + MONTH(dated) month, + u.name , + IFNULL(SUM(amount),0) currentSale, + s.salesPersonFk + FROM + bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + JOIN account.user u ON u.id = s.salesPersonFk + WHERE dated BETWEEN util.firstDayOfMonth(1()) AND LAST_DAY(1()) + GROUP BY year, month, salesPersonFk + ) sub1 ON subb.id = sub1.salesPersonFk + LEFT JOIN ( + SELECT + CASE + WHEN aa.name = 'miriam' THEN 'miriam-mmar' + WHEN u.name = 'miriam' THEN 'miriam-mmar' + WHEN aa.name = 'mmar' THEN 'miriam-mmar' + WHEN aa.name = 'ricar' THEN u.name + ELSE aa.name + END AS bossFk, + u.name, + s.salesPersonFk + FROM + bs.salesByclientSalesPerson s + LEFT JOIN vn.client c ON c.id =s.salesPersonFk + LEFT JOIN account.user u ON u.id = s.salesPersonFk + LEFT JOIN vn.worker w ON w.id = u.id + LEFT JOIN account.user aa ON aa.id = w.bossFk + GROUP BY s.salesPersonFk + ) sub2 ON subb.id = sub2.salesPersonFk + LEFT JOIN ( + SELECT + sg.rank, + sg.goal goalGroup, + sg2.goal goalIncentive, + sub.walletWeigth, + sub.workerFk + FROM ( + SELECT + CASE + WHEN portfolioWeight >= sg1.walletWeightFrom + AND portfolioWeight <= sg1.walletWeightTo THEN 1 + WHEN portfolioWeight >= sg2.walletWeightFrom + AND portfolioWeight <= sg2.walletWeightTo THEN 2 + WHEN portfolioWeight >= sg3.walletWeightFrom + AND portfolioWeight <= sg3.walletWeightTo THEN 3 + WHEN portfolioWeight >= sg4.walletWeightFrom + AND portfolioWeight <= sg4.walletWeightTo THEN 4 + WHEN portfolioWeight >= sg5.walletWeightFrom + AND portfolioWeight <= sg5.walletWeightTo THEN 5 + END rank, + u.id workerFk, + IFNULL(v.portfolioWeight,0) AS walletWeigth + FROM bs.vendedores v + JOIN account.`user` u ON u.id = v.Id_Trabajador + JOIN vn.saleGoal sg1 ON sg1.`rank` = 1 AND sg1.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = 2 AND sg2.`year` = YEAR(1()) AND sg2.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg3.`rank` = 3 AND sg3.`year` = YEAR(1()) AND sg3.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = 4 AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg5.`rank` = 5 AND sg5.`year` = YEAR(1()) AND sg5.`month` = MONTH(1()) + WHERE v.año = YEAR(1())-1 AND v.mes = MONTH(1()) + GROUP BY u.id) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = 'grupo' AND sg.`year` = YEAR(1()) AND sg.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = sub.rank AND sg2.goalType = 'individual' AND sg2.`year` = YEAR(1()) AND sg2.`month` = MONTH(1()) + LEFT JOIN account.`user` u2 ON u2.id = sub.workerFk + ) sub3 ON sub3.workerFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT + SUM(IF(ct.isBase, s.quantity * sc.value, 0)) as futureSale, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND cl.typeFk IN ('Normal','handMaking','internalUse') + AND s.quantity <> 0 + AND it.code != 'PAT' + AND s.discount <> 100 + AND (i.id = 98 OR i.id = 95 OR ic.merchandise != 0) + GROUP BY cl.salesPersonFk + HAVING IFNULL(futureSale, 0) <> 0 + ) sub4 ON sub4.salesPersonFk = sub2.salesPersonFk + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = subb.name AND spg.`year` = YEAR(1()) AND spg.`month` = MONTH(1()) + ORDER BY sub1.name + ) sub5 + ) sub6 + GROUP BY salesPersonName + ORDER BY salesPersonName; +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT + rank AS "Rango", + walletWeightFrom AS "Peso cartera desde", + walletWeightTo AS "Peso cartera hasta", + goal AS "Objetivo" +FROM vn.saleGoal +WHERE year=YEAR(CURDATE()) And month=MONTH(CURDATE()) AND goalType='grupo' +SELECT + rank AS "Rango", + walletWeightFrom AS "Peso cartera desde", + walletWeightTo AS "Peso cartera hasta", + goal AS "Objetivo" +FROM vn.saleGoal +WHERE year=YEAR(CURDATE()) And month=MONTH(CURDATE()) AND goalType='individual' +-- Tabla jefes +SELECT + rank AS 'Rango', + salesPersonName AS 'Comercial', + walletWeigth AS 'Peso Cartera', + percentageEvaluableObjective AS '% del Evaluable', + percentageIncentiveObjective AS '% del Incentivo', + percentageEvaluableObjective-(((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE()))))*100) AS 'Cómo lo llevo', + evaluableObjectiveGroup AS 'Objetivo Evaluable (Grupo)', + incentiveSalesObjective AS 'Objetivo Incentivo', + currentSale AS 'Venta hasta Hoy', + futureSale AS 'Ventas Futuro', + amountToEvaluableObjective AS 'Importe hasta Evaluable', + amountToIncentiveObjective AS 'Importe hasta Incentivo', + bossFk AS 'Equipo' + FROM( + SELECT + bossFk, + sub5.salesPersonName, + id, + rank, + walletWeigth, + evaluableObjectiveGroup, + incentiveSalesObjective, + currentSale, + evaluableObjectiveGroup-currentSale amountToEvaluableObjective, + incentiveSalesObjective-currentSale amountToIncentiveObjective, + (currentSale/evaluableObjectiveGroup)*100 AS percentageEvaluableObjective, + (currentSale/incentiveSalesObjective)*100 AS percentageIncentiveObjective, + NULL AS today, + futureSale + FROM + ( + SELECT + bossFk, + subb.id, + subb.name AS salesPersonName, + walletWeigth, + IFNULL(spg.goal, walletWeigth*goalGroup/100) AS evaluableObjectiveGroup, + IFNULL(spg.goal, walletWeigth*goalIncentive/100) AS incentiveSalesObjective, + currentSale, + futureSale, + sub3.rank AS rank + FROM + (SELECT u.name, + u.id + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %') subb + LEFT JOIN ( + SELECT + YEAR(dated) year, + MONTH(dated) month, + u.name , + IFNULL(SUM(amount),0) currentSale, + s.salesPersonFk + FROM + bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + JOIN account.user u ON u.id = s.salesPersonFk + WHERE dated BETWEEN util.firstDayOfMonth(CURDATE()) AND LAST_DAY(CURDATE()) + GROUP BY year, month, salesPersonFk + ) sub1 ON subb.id = sub1.salesPersonFk + LEFT JOIN ( + SELECT + CASE + WHEN aa.name = 'miriam' THEN 'miriam-mmar' + WHEN u.name = 'miriam' THEN 'miriam-mmar' + WHEN aa.name = 'mmar' THEN 'miriam-mmar' + WHEN aa.name = 'ricar' THEN u.name + ELSE aa.name + END AS bossFk, + u.name, + s.salesPersonFk + FROM + bs.salesByclientSalesPerson s + LEFT JOIN vn.client c ON c.id =s.salesPersonFk + LEFT JOIN account.user u ON u.id = s.salesPersonFk + LEFT JOIN vn.worker w ON w.id = u.id + LEFT JOIN account.user aa ON aa.id = w.bossFk + GROUP BY s.salesPersonFk + ) sub2 ON subb.id = sub2.salesPersonFk + LEFT JOIN ( + SELECT + sg.rank, + sg.goal goalGroup, + sg2.goal goalIncentive, + sub.walletWeigth, + sub.workerFk + FROM ( + SELECT + CASE + WHEN portfolioWeight >= sg1.walletWeightFrom + AND portfolioWeight <= sg1.walletWeightTo THEN 1 + WHEN portfolioWeight >= sg2.walletWeightFrom + AND portfolioWeight <= sg2.walletWeightTo THEN 2 + WHEN portfolioWeight >= sg3.walletWeightFrom + AND portfolioWeight <= sg3.walletWeightTo THEN 3 + WHEN portfolioWeight >= sg4.walletWeightFrom + AND portfolioWeight <= sg4.walletWeightTo THEN 4 + WHEN portfolioWeight >= sg5.walletWeightFrom + AND portfolioWeight <= sg5.walletWeightTo THEN 5 + WHEN portfolioWeight >= sg6.walletWeightFrom + AND portfolioWeight <= sg6.walletWeightTo THEN 6 + END rank, + u.id workerFk, + IFNULL(v.portfolioWeight,0) AS walletWeigth + FROM bs.vendedores v + JOIN account.`user` u ON u.id = v.Id_Trabajador + JOIN vn.saleGoal sg1 ON sg1.`rank` = 1 AND sg1.`year` = YEAR(CURDATE()) AND sg1.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = 2 AND sg2.`year` = YEAR(CURDATE()) AND sg2.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg3 ON sg3.`rank` = 3 AND sg3.`year` = YEAR(CURDATE()) AND sg3.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = 4 AND sg4.`year` = YEAR(CURDATE()) AND sg4.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg5 ON sg5.`rank` = 5 AND sg5.`year` = YEAR(CURDATE()) AND sg5.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg6 ON sg6.`rank` = 6 AND sg6.`year` = YEAR(CURDATE()) AND sg6.`month` = MONTH(CURDATE()) + WHERE v.año = YEAR(CURDATE())-1 AND v.mes = MONTH(CURDATE()) + GROUP BY u.id) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = 'grupo' AND sg.`year` = YEAR(CURDATE()) AND sg.`month` = MONTH(CURDATE()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = sub.rank AND sg2.goalType = 'individual' AND sg2.`year` = YEAR(CURDATE()) AND sg2.`month` = MONTH(CURDATE()) + LEFT JOIN account.`user` u2 ON u2.id = sub.workerFk + ) sub3 ON sub3.workerFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT + SUM(IF(ct.isBase, s.quantity * sc.value, 0)) as futureSale, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND cl.typeFk IN ('Normal','handMaking','internalUse') + AND s.quantity <> 0 + AND it.code != 'PAT' + AND s.discount <> 100 + AND (i.id = 98 OR i.id = 95 OR ic.merchandise != 0) + GROUP BY cl.salesPersonFk + HAVING IFNULL(futureSale, 0) <> 0 + ) sub4 ON sub4.salesPersonFk = sub2.salesPersonFk + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = subb.name AND spg.`year` = YEAR(CURDATE()) AND spg.`month` = MONTH(CURDATE()) + ORDER BY sub1.name + ) sub5 + ) sub6 + GROUP BY salesPersonName + ORDER BY salesPersonName; +SELECT u.name + FROM vn.department d + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d.name LIKE '%EQUIPO %' +SELECT c.id 'ID Cliente', + c.name Cliente, + IFNULL(sub.amount,0) 'Base comisionable venta', + pm.name 'Modo de pago', + c.credit 'Crédito' + FROM vn.client c + JOIN account.user u ON u.id = c.salesPersonFk + LEFT JOIN vn.payMethod pm ON pm.id = c.payMethodFk + LEFT JOIN ( + SELECT SUM(s.amount) amount,s.clientFk + FROM bs.salesByclientSalesPerson s + WHERE s.dated BETWEEN '2021-10-01' AND '2021-10-31' + GROUP BY s.clientFk + )sub ON sub.clientFk = c.id + WHERE u.name = '1' +SELECT sg.`rank` 'Rango', + sg.walletWeightFrom 'Peso cartera desde', + sg.walletWeightTo 'Peso cartera hasta', + sg1.goal 'GRADO 1 (GRUPAL)', + sg2.goal 'GRADO 2', + sg3.goal 'GRADO 3', + sg4.goal 'GRADO 4 (INCENTIVO)', + sg5.goal 'GRADO 5' + FROM vn.saleGoal sg + JOIN vn.saleGoal sg1 ON sg.`rank` = sg1.`rank` AND sg1.goalType = '1' AND sg1.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg.`rank` = sg2.`rank` AND sg2.goalType = '2' AND sg2.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg.`rank` = sg3.`rank` AND sg3.goalType = '3' AND sg3.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg.`rank` = sg4.`rank` AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg.`rank` = sg5.`rank` AND sg5.goalType = '5' AND sg5.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + + WHERE sg.year = YEAR(1()) AND sg.`month` = MONTH(1()) + GROUP BY sg.`rank`; +-- Tabla jefes +SELECT rank AS 'Rango', + salesPersonName AS 'Comercial', + walletWeigth AS 'Peso Cartera', + grade1PercentageEvaluableObjective AS 'Grado 1 (GRUPAL)', + grade2 AS 'Grado 2', + grade3 AS 'Grado 3', + grade4PercentageIncentiveObjective AS 'Grado 4 (INCENTIVO)', + grade5 AS 'Grado 5', + grade1PercentageEvaluableObjective-(((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE()))))*100) AS 'Cómo lo llevo', + evaluableObjectiveGroup AS 'Objetivo Evaluable (Grupo)', + incentiveSalesObjective AS 'Objetivo Incentivo', + currentSale AS 'Venta hasta Hoy', + grade1PercentageEvaluableObjective-currentSale AS 'Importe hasta Evaluable', + amountToIncentiveObjective AS 'Importe hasta Incentivo', + bossFk AS 'Equipo' + FROM ( + SELECT bossFk, + sub5.salesPersonName, + id, + rank, + walletWeigth, + evaluableObjectiveGroup, + incentiveSalesObjective, + currentSale, + evaluableObjectiveGroup-currentSale amountToEvaluableObjective, + incentiveSalesObjective-currentSale amountToIncentiveObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade1 + walletWeigth) AS grade1PercentageEvaluableObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade2 + walletWeigth) AS grade2, + IFNULL(salesPersonGoal, averageIncreaseGrade3 + walletWeigth) AS grade3, + IFNULL(salesPersonGoal, averageIncreaseGrade4 + walletWeigth) AS grade4PercentageIncentiveObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade5 + walletWeigth) AS grade5, + NULL AS today, + futureSale + FROM ( + SELECT bossFk, + subb.id, + subb.name AS salesPersonName, + walletWeigth, + IFNULL(spg.goal, walletWeigth*goalGroup/100) AS evaluableObjectiveGroup, + IFNULL(spg.goal, walletWeigth*goalIncentive/100) AS incentiveSalesObjective, + currentSale, + futureSale, + sub3.rank AS rank, + spg.goal salesPersonGoal, + sub5.averageIncreaseGrade1, + sub5.averageIncreaseGrade2, + sub5.averageIncreaseGrade3, + sub5.averageIncreaseGrade4, + sub5.averageIncreaseGrade5 + FROM ( + SELECT u.name, + u.id + FROM vn.department d + JOIN vn.department d2 ON d2.id = d.parentFk + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d2.code = 'VT' + ) subb + LEFT JOIN ( + SELECT YEAR(dated) year, + MONTH(dated) month, + u.name, + IFNULL(SUM(amount),0) currentSale, + s.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + JOIN account.user u ON u.id = s.salesPersonFk + WHERE dated BETWEEN util.firstDayOfMonth(1()) AND LAST_DAY(1()) + GROUP BY year, month, salesPersonFk + ) sub1 ON subb.id = sub1.salesPersonFk + LEFT JOIN ( + SELECT + CASE + WHEN aa.name = 'miriam' THEN 'miriam-mmar' + WHEN u.name = 'miriam' THEN 'miriam-mmar' + WHEN aa.name = 'mmar' THEN 'miriam-mmar' + WHEN aa.name = 'ricar' THEN u.name + ELSE aa.name + END AS bossFk, + u.name, + s.salesPersonFk + FROM bs.salesByclientSalesPerson s + LEFT JOIN vn.client c ON c.id =s.salesPersonFk + LEFT JOIN account.user u ON u.id = s.salesPersonFk + LEFT JOIN vn.worker w ON w.id = u.id + LEFT JOIN account.user aa ON aa.id = w.bossFk + GROUP BY s.salesPersonFk + ) sub2 ON subb.id = sub2.salesPersonFk + LEFT JOIN ( + SELECT sg.rank, + sg.goal goalGroup, + sg4.goal goalIncentive, + sub.walletWeigth, + sub.workerFk + FROM ( + SELECT sg.`rank` , + u.id workerFk, + IFNULL(sub.portfolioWeight,0) walletWeigth + FROM ( + SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.salesPersonFk + WHERE dated BETWEEN DATE_SUB(DATE(1()), INTERVAL 1 YEAR) AND DATE_SUB(DATE(1()), INTERVAL 1 YEAR) + GROUP BY `year`, `month`, salesPersonFk + ) sub + LEFT JOIN account.`user` u ON u.id = sub.salesPersonFk + LEFT JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND sub.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE sub.`year` = YEAR(1())-1 AND sub.`month` = MONTH(1()) + GROUP BY u.id + ) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = '1' AND sg.`year` = YEAR(1()) AND sg.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = sub.rank AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + LEFT JOIN account.`user` u2 ON u2.id = sub.workerFk + ) sub3 ON sub3.workerFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT SUM(IF(ct.isBase, s.quantity * sc.value, 0)) as futureSale, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND cl.typeFk IN ('Normal','handMaking','internalUse') + AND s.quantity <> 0 + AND it.code != 'PAT' + AND s.discount <> 100 + AND (i.id = 98 OR i.id = 95 OR ic.merchandise != 0) + GROUP BY cl.salesPersonFk + HAVING IFNULL(futureSale, 0) <> 0 + ) sub4 ON sub4.salesPersonFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT sg.rank, + (SUM(sub.walletWeigth)*sg.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade1, + (SUM(sub.walletWeigth)*sg2.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade2, + (SUM(sub.walletWeigth)*sg3.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade3, + (SUM(sub.walletWeigth)*sg4.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade4, + (SUM(sub.walletWeigth)*sg5.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade5 + FROM ( + SELECT sg.`rank` , + u.id workerFk, + IFNULL(v.portfolioWeight,0) AS walletWeigth + FROM bs.vendedores v + JOIN account.`user` u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + JOIN vn.department d2 ON d2.id = d.parentFk + JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND v.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE v.año = YEAR(1())-1 + AND v.mes = MONTH(1()) + AND d2.code = 'VT' + GROUP BY u.id + ) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = '1' AND sg.`year` = YEAR(1()) AND sg.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = sub.rank AND sg2.goalType = '2' AND sg2.`year` = YEAR(1()) AND sg2.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg3.`rank` = sub.rank AND sg3.goalType = '3' AND sg3.`year` = YEAR(1()) AND sg3.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = sub.rank AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg5.`rank` = sub.rank AND sg5.goalType = '5' AND sg5.`year` = YEAR(1()) AND sg5.`month` = MONTH(1()) + JOIN account.`user` u2 ON u2.id = sub.workerFk + GROUP BY sg.`rank` + ) sub5 ON sub5.`rank`= sub3.rank + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = subb.name AND spg.`year` = YEAR(1()) AND spg.`month` = MONTH(1()) + ORDER BY sub1.name + ) sub5 + ) sub6 + GROUP BY salesPersonName + ORDER BY salesPersonName; +SELECT saleGoals.rank 'Rango', + salesPerson.name 'Comercial', + CAST( portfolioWeight.amount AS DECIMAL(10,2)) 'Peso Cartera', + CAST(@grade1PercentageEvaluableObjective := IFNULL(spg.goal, saleGoals.averageIncreaseGrade1 + portfolioWeight.amount)AS DECIMAL(10,2)) 'Grado 1 (GRUPAL)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade2 + portfolioWeight.amount) AS DECIMAL(10,2))'Grado 2', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade3 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 3', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade4 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 4 (INCENTIVO)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade5 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 5', + CAST(@grade1PercentageEvaluableObjective - (((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE())))) * 100) AS DECIMAL(10,2)) 'Cómo lo llevo', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalGroup/100) AS DECIMAL(10,2))'Objetivo Evaluable (Grupo)', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100)AS DECIMAL(10,2)) 'Objetivo Incentivo', + CAST(currentSale.amount AS DECIMAL(10,2)) 'Venta hasta Hoy', + -- CAST(futureSale.amount AS DECIMAL(10,2)) 'Venta a futuro', + CAST(@grade1PercentageEvaluableObjective - currentSale.amount AS DECIMAL(10,2)) 'Importe hasta Evaluable', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100) - currentSale.amount AS DECIMAL(10,2)) 'Importe hasta Incentivo', + salesPerson.bossFk 'Equipo' + FROM ( + SELECT u.name, + u.id, + CASE + WHEN us.name = 'miriam' OR us.name = 'mmar' + THEN 'miriam-mmar' + WHEN us.name = 'ricar' + THEN u.name + ELSE us.name + END bossFk + FROM vn.worker w + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + )salesPerson + LEFT JOIN ( + SELECT IFNULL(SUM(amount),0) amount, s.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + WHERE dated BETWEEN 1() AND 1() + GROUP BY salesPersonFk + )currentSale ON salesPerson.id = currentSale.salesPersonFk + LEFT JOIN ( + SELECT IFNULL(sg.rank,1) rank, + sg1.goal goalGroup, + sg4.goal goalIncentive, + IFNULL(sub.portfolioWeight,0) amount, + workerFk + FROM (SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.lastSalesPersonFk workerFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY `year`, `month`, c.lastSalesPersonFk + ) sub + LEFT JOIN account.`user` u ON u.id = sub.workerFk + LEFT JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND sub.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + JOIN vn.saleGoal sg1 ON sg1.`rank` = IFNULL(sg.`rank`,1) + AND sg1.goalType = 1 + AND sg1.`year` = YEAR(1()) + AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = IFNULL(sg.`rank`,1) + AND sg4.goalType = 4 + AND sg4.`year` = YEAR(1()) + AND sg4.`month` = MONTH(1()) + WHERE sub.`year` = YEAR(1())-1 AND sub.`month` = MONTH(1()) + GROUP BY u.id + ) portfolioWeight ON portfolioWeight.workerFk = currentSale.salesPersonFk + /*LEFT JOIN (SELECT SUM(s.quantity * sc.value) amount, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + WHERE t.shipped BETWEEN 1() AND 1() + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + GROUP BY cl.salesPersonFk + HAVING amount AND cl.salesPersonFk + ) futureSale ON futureSale.salesPersonFk = salesPerson.id*/ + LEFT JOIN ( + SELECT sg.`rank`, + (SUM(IFNULL(portfolioWeight.amount,0))* sg1.goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) averageIncreaseGrade1, + (SUM(IFNULL(portfolioWeight.amount,0))* sg2.goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) averageIncreaseGrade2, + (SUM(IFNULL(portfolioWeight.amount,0))* sg3.goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) averageIncreaseGrade3, + (SUM(IFNULL(portfolioWeight.amount,0))* sg4.goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) averageIncreaseGrade4, + (SUM(IFNULL(portfolioWeight.amount,0))* sg5.goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) averageIncreaseGrade5 + FROM(SELECT c.lastSalesPersonFk salespersonFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + JOIN vn.department d2 ON d2.id = d.parentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + AND d2.code = 'VT' + GROUP BY c.lastSalesPersonFk)portfolioWeight + JOIN vn.saleGoal sg ON sg.`year`= YEAR(1()) AND sg.`month` = MONTH(1()) AND portfolioWeight.amount BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + JOIN vn.saleGoal sg1 ON sg1.`rank` = sg.rank AND sg1.goalType = '1' AND sg1.`year` = YEAR(1()) AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = sg.rank AND sg2.goalType = '2' AND sg2.`year` = YEAR(1()) AND sg2.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg3.`rank` = sg.rank AND sg3.goalType = '3' AND sg3.`year` = YEAR(1()) AND sg3.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = sg.rank AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg5.`rank` = sg.rank AND sg5.goalType = '5' AND sg5.`year` = YEAR(1()) AND sg5.`month` = MONTH(1()) + GROUP BY sg.rank + ) saleGoals ON saleGoals.`rank`= portfolioWeight.rank + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = salesPerson.name + AND spg.`year` = YEAR(1()) + AND spg.`month` = MONTH(1() ) +GROUP BY salesPerson.id +ORDER BY salesPerson.name +-- Tabla jefes +SELECT rank AS 'Rango', + salesPersonName AS 'Comercial', + walletWeigth AS 'Peso Cartera', + grade1PercentageEvaluableObjective AS 'Grado 1 (GRUPAL)', + grade2 AS 'Grado 2', + grade3 AS 'Grado 3', + grade4PercentageIncentiveObjective AS 'Grado 4 (INCENTIVO)', + grade5 AS 'Grado 5', + grade1PercentageEvaluableObjective-(((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE()))))*100) AS 'Cómo lo llevo', + evaluableObjectiveGroup AS 'Objetivo Evaluable (Grupo)', + incentiveSalesObjective AS 'Objetivo Incentivo', + currentSale AS 'Venta hasta Hoy', + grade1PercentageEvaluableObjective-currentSale AS 'Importe hasta Evaluable', + amountToIncentiveObjective AS 'Importe hasta Incentivo', + bossFk AS 'Equipo' + FROM ( + SELECT bossFk, + sub5.salesPersonName, + id, + rank, + walletWeigth, + evaluableObjectiveGroup, + incentiveSalesObjective, + currentSale, + evaluableObjectiveGroup-currentSale amountToEvaluableObjective, + incentiveSalesObjective-currentSale amountToIncentiveObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade1 + walletWeigth) AS grade1PercentageEvaluableObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade2 + walletWeigth) AS grade2, + IFNULL(salesPersonGoal, averageIncreaseGrade3 + walletWeigth) AS grade3, + IFNULL(salesPersonGoal, averageIncreaseGrade4 + walletWeigth) AS grade4PercentageIncentiveObjective, + IFNULL(salesPersonGoal, averageIncreaseGrade5 + walletWeigth) AS grade5, + NULL AS today, + futureSale + FROM ( + SELECT bossFk, + subb.id, + subb.name AS salesPersonName, + walletWeigth, + IFNULL(spg.goal, walletWeigth*goalGroup/100) AS evaluableObjectiveGroup, + IFNULL(spg.goal, walletWeigth*goalIncentive/100) AS incentiveSalesObjective, + currentSale, + futureSale, + sub3.rank AS rank, + spg.goal salesPersonGoal, + sub5.averageIncreaseGrade1, + sub5.averageIncreaseGrade2, + sub5.averageIncreaseGrade3, + sub5.averageIncreaseGrade4, + sub5.averageIncreaseGrade5 + FROM ( + SELECT u.name, + u.id + FROM vn.department d + JOIN vn.department d2 ON d2.id = d.parentFk + JOIN vn.workerDepartment wd ON wd.departmentFk = d.id + JOIN account.user u on u.id = wd.workerFk + WHERE d2.code = 'VT' + ) subb + LEFT JOIN ( + SELECT YEAR(dated) year, + MONTH(dated) month, + u.name, + IFNULL(SUM(amount),0) currentSale, + s.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + JOIN account.user u ON u.id = s.salesPersonFk + WHERE dated BETWEEN util.firstDayOfMonth(1()) AND LAST_DAY(1()) + GROUP BY year, month, salesPersonFk + ) sub1 ON subb.id = sub1.salesPersonFk + LEFT JOIN ( + SELECT + CASE + WHEN aa.name = 'miriam' THEN 'miriam-mmar' + WHEN u.name = 'miriam' THEN 'miriam-mmar' + WHEN aa.name = 'mmar' THEN 'miriam-mmar' + WHEN aa.name = 'ricar' THEN u.name + ELSE aa.name + END AS bossFk, + u.name, + s.salesPersonFk + FROM bs.salesByclientSalesPerson s + LEFT JOIN vn.client c ON c.id =s.salesPersonFk + LEFT JOIN account.user u ON u.id = s.salesPersonFk + LEFT JOIN vn.worker w ON w.id = u.id + LEFT JOIN account.user aa ON aa.id = w.bossFk + GROUP BY s.salesPersonFk + ) sub2 ON subb.id = sub2.salesPersonFk + LEFT JOIN ( + SELECT sg.rank, + sg.goal goalGroup, + sg4.goal goalIncentive, + sub.walletWeigth, + sub.workerFk + FROM ( + SELECT sg.`rank` , + u.id workerFk, + IFNULL(sub.portfolioWeight,0) walletWeigth + FROM ( + SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.salesPersonFk + WHERE dated BETWEEN DATE_SUB(DATE(1()), INTERVAL 1 YEAR) AND DATE_SUB(DATE(1()), INTERVAL 1 YEAR) + GROUP BY `year`, `month`, salesPersonFk + ) sub + LEFT JOIN account.`user` u ON u.id = sub.salesPersonFk + LEFT JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND sub.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE sub.`year` = YEAR(1())-1 AND sub.`month` = MONTH(1()) + GROUP BY u.id + ) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = '1' AND sg.`year` = YEAR(1()) AND sg.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = sub.rank AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + LEFT JOIN account.`user` u2 ON u2.id = sub.workerFk + ) sub3 ON sub3.workerFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT SUM(IF(ct.isBase, s.quantity * sc.value, 0)) as futureSale, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND cl.typeFk IN ('Normal','handMaking','internalUse') + AND s.quantity <> 0 + AND it.code != 'PAT' + AND s.discount <> 100 + AND (i.id = 98 OR i.id = 95 OR ic.merchandise != 0) + GROUP BY cl.salesPersonFk + HAVING IFNULL(futureSale, 0) <> 0 + ) sub4 ON sub4.salesPersonFk = sub2.salesPersonFk + LEFT JOIN ( + SELECT sg.rank, + (SUM(sub.walletWeigth)*sg.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade1, + (SUM(sub.walletWeigth)*sg2.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade2, + (SUM(sub.walletWeigth)*sg3.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade3, + (SUM(sub.walletWeigth)*sg4.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade4, + (SUM(sub.walletWeigth)*sg5.goal/100 - SUM(sub.walletWeigth))/COUNT(sub.workerFk) averageIncreaseGrade5 + FROM ( + SELECT sg.`rank` , + u.id workerFk, + IFNULL(v.portfolioWeight,0) AS walletWeigth + FROM bs.vendedores v + JOIN account.`user` u ON u.id = v.Id_Trabajador + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + JOIN vn.department d2 ON d2.id = d.parentFk + JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND v.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE v.año = YEAR(1())-1 + AND v.mes = MONTH(1()) + AND d2.code = 'VT' + GROUP BY u.id + ) sub + JOIN vn.saleGoal sg ON sg.`rank` = sub.rank AND sg.goalType = '1' AND sg.`year` = YEAR(1()) AND sg.`month` = MONTH(1()) + JOIN vn.saleGoal sg2 ON sg2.`rank` = sub.rank AND sg2.goalType = '2' AND sg2.`year` = YEAR(1()) AND sg2.`month` = MONTH(1()) + JOIN vn.saleGoal sg3 ON sg3.`rank` = sub.rank AND sg3.goalType = '3' AND sg3.`year` = YEAR(1()) AND sg3.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = sub.rank AND sg4.goalType = '4' AND sg4.`year` = YEAR(1()) AND sg4.`month` = MONTH(1()) + JOIN vn.saleGoal sg5 ON sg5.`rank` = sub.rank AND sg5.goalType = '5' AND sg5.`year` = YEAR(1()) AND sg5.`month` = MONTH(1()) + JOIN account.`user` u2 ON u2.id = sub.workerFk + GROUP BY sg.`rank` + ) sub5 ON sub5.`rank`= sub3.rank + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = subb.name AND spg.`year` = YEAR(1()) AND spg.`month` = MONTH(1()) + ORDER BY sub1.name + ) sub5 + ) sub6 + GROUP BY salesPersonName + ORDER BY salesPersonName; +SELECT u.name + FROM account.user u + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = u.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + WHERE d.name LIKE '%EQUIPO %' OR u.name IN ('ruben','ismaelalcolea','josemp') +SELECT saleGoals.rank 'Rango', + salesPerson.name 'Comercial', + CAST( portfolioWeight.amount AS DECIMAL(10,2)) 'Peso Cartera', + CAST(@grade1PercentageEvaluableObjective := IFNULL(spg.goal, saleGoals.averageIncreaseGrade1 + portfolioWeight.amount)AS DECIMAL(10,2)) 'Grado 1 (GRUPAL)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade2 + portfolioWeight.amount) AS DECIMAL(10,2))'Grado 2', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade3 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 3', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade4 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 4 (INCENTIVO)', + CAST(IFNULL(spg.goal, saleGoals.averageIncreaseGrade5 + portfolioWeight.amount) AS DECIMAL(10,2)) 'Grado 5', + CAST(@grade1PercentageEvaluableObjective - (((DAY(CURDATE())-1)/(DAY(LAST_DAY(CURDATE())))) * 100) AS DECIMAL(10,2)) 'Cómo lo llevo', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalGroup/100) AS DECIMAL(10,2))'Objetivo Evaluable (Grupo)', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100)AS DECIMAL(10,2)) 'Objetivo Incentivo', + CAST(currentSale.amount AS DECIMAL(10,2)) 'Venta hasta Hoy', + -- CAST(futureSale.amount AS DECIMAL(10,2)) 'Venta a futuro', + CAST(@grade1PercentageEvaluableObjective - currentSale.amount AS DECIMAL(10,2)) 'Importe hasta Evaluable', + CAST(IFNULL(spg.goal, portfolioWeight.amount*goalIncentive/100) - currentSale.amount AS DECIMAL(10,2)) 'Importe hasta Incentivo', + salesPerson.bossFk 'Equipo' + FROM ( + SELECT u.name, + u.id, + CASE + WHEN us.name = 'miriam' OR us.name = 'mmar' + THEN 'miriam-mmar' + WHEN us.name = 'ricar' + THEN u.name + ELSE us.name + END bossFk + FROM vn.worker w + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user u ON u.id = w.id + JOIN account.user us ON us.id = w.bossFk + WHERE d.name LIKE 'EQUIPO %' + )salesPerson + LEFT JOIN ( + SELECT IFNULL(SUM(amount),0) amount, s.salesPersonFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.salesPersonFk + WHERE dated BETWEEN 1() AND 1() + GROUP BY salesPersonFk + )currentSale ON salesPerson.id = currentSale.salesPersonFk + LEFT JOIN ( + SELECT IFNULL(sg.rank,1) rank, + sg1.goal goalGroup, + sg4.goal goalIncentive, + IFNULL(sub.portfolioWeight,0) amount, + workerFk + FROM (SELECT YEAR(dated) `year`, MONTH(dated) `month`, u.name, SUM(amount) portfolioWeight, c.lastSalesPersonFk workerFk + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + GROUP BY `year`, `month`, c.lastSalesPersonFk + ) sub + LEFT JOIN account.`user` u ON u.id = sub.workerFk + LEFT JOIN vn.saleGoal sg ON sg.`year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + AND sub.portfolioWeight BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + JOIN vn.saleGoal sg1 ON sg1.`rank` = IFNULL(sg.`rank`,1) + AND sg1.goalType = 1 + AND sg1.`year` = YEAR(1()) + AND sg1.`month` = MONTH(1()) + JOIN vn.saleGoal sg4 ON sg4.`rank` = IFNULL(sg.`rank`,1) + AND sg4.goalType = 4 + AND sg4.`year` = YEAR(1()) + AND sg4.`month` = MONTH(1()) + WHERE sub.`year` = YEAR(1())-1 AND sub.`month` = MONTH(1()) + GROUP BY u.id + ) portfolioWeight ON portfolioWeight.workerFk = currentSale.salesPersonFk + /*LEFT JOIN (SELECT SUM(s.quantity * sc.value) amount, + cl.salesPersonFk + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + WHERE t.shipped BETWEEN 1() AND 1() + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + GROUP BY cl.salesPersonFk + HAVING amount AND cl.salesPersonFk + ) futureSale ON futureSale.salesPersonFk = salesPerson.id*/ + LEFT JOIN ( + SELECT `rank`, + SUM(IF(goalType = 1, amount, 0)) averageIncreaseGrade1, + SUM(IF(goalType = 2, amount, 0)) averageIncreaseGrade2, + SUM(IF(goalType = 3, amount, 0)) averageIncreaseGrade3, + SUM(IF(goalType = 4, amount, 0)) averageIncreaseGrade4, + SUM(IF(goalType = 5, amount, 0)) averageIncreaseGrade5 + FROM ( + SELECT rank, + goalType, + (SUM(IFNULL(portfolioWeight.amount,0))* goal/100 - SUM(IFNULL(portfolioWeight.amount,0)))/COUNT(portfolioWeight.salespersonFk) amount + FROM (SELECT c.lastSalesPersonFk salespersonFk, SUM(amount) amount + FROM bs.salesByclientSalesPerson s + JOIN vn.client c ON c.id = s.clientFk + JOIN account.user u ON u.id = c.lastSalesPersonFk + JOIN vn.workerDepartment wd ON wd.workerFk = u.id + JOIN vn.department d ON d.id = wd.departmentFk + JOIN vn.department d2 ON d2.id = d.parentFk + WHERE dated BETWEEN 1() - INTERVAL 1 YEAR AND 1() - INTERVAL 1 YEAR + AND d2.code = 'VT' + GROUP BY c.lastSalesPersonFk)portfolioWeight + JOIN saleGoal sg ON sg.`year`= YEAR(1()) AND sg.`month` = MONTH(1()) AND portfolioWeight.amount BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + WHERE sg.`rank` = sg.rank AND portfolioWeight.amount BETWEEN sg.walletWeightFrom AND sg.walletWeightTo + AND goalType BETWEEN 1 AND 5 + AND `year` = YEAR(1()) + AND sg.`month` = MONTH(1()) + GROUP BY rank, goalType + )sub + GROUP BY rank + ) saleGoals ON saleGoals.`rank`= portfolioWeight.rank + LEFT JOIN vn.salesPersonGoal spg ON spg.salesPersonName = salesPerson.name + AND spg.`year` = YEAR(1()) + AND spg.`month` = MONTH(1() ) +GROUP BY salesPerson.id +ORDER BY salesPerson.name; +SELECT SUM(s.quantity * sc.value) amount, + u.name 'COMERCIAL' + FROM vn.ticket t + STRAIGHT_JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.client cl ON cl.id = t.clientFk + JOIN account.user u ON u.id = cl.salesPersonFk + WHERE t.shipped BETWEEN CURDATE() AND LAST_DAY(CURDATE()) + AND s.quantity <> 0 + AND s.discount <> 100 + AND ic.merchandise + AND u.name NOT IN ('pako','jstegarescu','jordi','pacorico','jfrau','ruben','ismaelalcolea') + AND ct.isBase + GROUP BY cl.salesPersonFk + HAVING amount AND cl.salesPersonFk + ORDER BY u.name ASC +SELECT u.name + FROM account.user u + LEFT JOIN vn.workerDepartment wd ON wd.workerFk = u.id + LEFT JOIN vn.department d ON d.id = wd.departmentFk + WHERE d.name LIKE '%EQUIPO %' OR u.name IN ('ruben','ismaelalcolea','josemp') +SELECT Cliente, + Nombre, + Comercial, + quantity2021, + quantity2022, + quantity2023, + quantity2022 - quantity2021, + quantity2023 - quantity2022 + FROM ( +SELECT c.id Cliente, + c.name Nombre, + u.name Comercial, + SUM(quantity2021) quantity2021, + SUM(quantity2022) quantity2022, + SUM(quantity2023) quantity2023 + FROM ( + SELECT t.clientFk , + SUM(s.quantity) quantity2021 , + 0 quantity2022, + 0 quantity2023 + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2021-01-15' AND util.dayEnd('2021-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk + UNION ALL + SELECT t.clientFk, + 0, + SUM(s.quantity) , + 0 + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2022-01-15' AND util.dayEnd('2022-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk + UNION ALL + SELECT t.clientFk, + 0, + 0, + SUM(s.quantity) + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2023-01-15' AND util.dayEnd('2023-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk)sub + JOIN vn.client c ON c.id = sub.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE c.id NOT IN (200,31, 400) + GROUP BY clientFk)sub2 +SELECT Cliente, + Nombre, + Comercial, + quantity2021, + quantity2022, + quantity2023, + quantity2022 - quantity2021, + quantity2023 - quantity2022 + FROM ( +SELECT c.id Cliente, + c.name Nombre, + u.name Comercial, + SUM(quantity2021) quantity2021, + SUM(quantity2022) quantity2022, + SUM(quantity2023) quantity2023 + FROM ( + SELECT t.clientFk , + SUM(s.quantity) quantity2021 , + 0 quantity2022, + 0 quantity2023 + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2021-01-15' AND util.dayEnd('2021-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk + UNION ALL + SELECT t.clientFk, + 0, + SUM(s.quantity) , + 0 + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2022-01-15' AND util.dayEnd('2022-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk + UNION ALL + SELECT t.clientFk, + 0, + 0, + SUM(s.quantity) + FROM vn.ticket t + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE t.shipped BETWEEN '2023-01-15' AND util.dayEnd('2023-02-14') + AND it.`code` ='RSV' + GROUP BY t.clientFk)sub + JOIN vn.client c ON c.id = sub.clientFk + LEFT JOIN account.user u ON u.id = c.salesPersonFk + WHERE c.id NOT IN (200,31, 400) + GROUP BY clientFk)sub2 +WITH tBuy AS( + SELECT + b.itemFk, + sum(quantity) quantity, + sum((b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * b.quantity) / sum(b.quantity) cmp + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.worker w ON w.id = it.workerFk + JOIN vn.warehouse wh ON wh.id = tr.warehouseInFk + WHERE it.code = 'RSV' + AND 1(tr.landed) + AND wh.name = 'Algemesi' + GROUP BY b.itemFk), + tNeto AS ( + SELECT + s.id, + sum(sc.value) neto + FROM vn.sale s + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND ct.code IN ('COST', 'MARGIN') + AND wh.name = 'Algemesi' + AND s.quantity + GROUP BY s.id) +SELECT + SUM(compradas) TotalCompradas, + SUM(vendidas) TotalVendidas, + SUM(vendidas) / sum(Compradas) PorcentajeVendido, + sum(coste * vendidas) TotalCosteVendidas, + sum(neto * vendidas) TotalVenta, + sum(neto * vendidas) - sum(coste * vendidas) TotalMargen, + (sum(neto * vendidas) - sum(coste * vendidas)) / sum(neto * vendidas) Porcentaje + FROM ( + SELECT + s.itemFk, + i.longName, + i.`size`, + i.subName, + tb.quantity compradas, + sum(s.quantity) vendidas, + tb.cmp coste, + SUM(s.quantity * tn.neto) / sum(s.quantity) neto, + SUM(s.quantity * tn.neto) / sum(s.quantity) / tb.cmp -1 margen + FROM vn.sale s + JOIN tBuy tb ON tb.itemFk = s.itemFk + JOIN tNeto tn ON tn.id = s.id + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND wh.name = 'Algemesi' + AND s.quantity + GROUP BY s.itemFk) sub +SELECT s.itemFk, i.longName, i.`size`, i.subName, sum(quantity) tallos + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE it.code = 1 + AND s.quantity + AND 1(t.shipped) + GROUP BY s.itemFk; +SELECT sum(quantity * (i.`size` = 40)) `40` + ,sum(quantity * (i.`size` = 50)) `50` + ,sum(quantity * (i.`size` = 60)) `60` + ,sum(quantity * (i.`size` = 70)) `70` + ,sum(quantity * (i.`size` = 80)) `80` + ,sum(quantity * (i.`size` = 90)) `90` + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + WHERE it.code = 'RSV' + AND s.quantity + AND 1(t.shipped); +WITH tSales AS + ( + SELECT + clientFk, + SUM(IF(año = YEAR(CURDATE())-1, venta,0)) ly, + SUM(IF(año = YEAR(CURDATE()), venta,0)) cy + FROM ( + SELECT t.clientFk, sum(s.quantity * s.price) venta, YEAR(t.shipped) año + FROM vn.sale s + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE it.code = 'RSV' + AND s.quantity + AND s.price + AND ( + t.shipped BETWEEN TIMESTAMPADD(YEAR,-1,FROM_UNIXTIME(1 / 1000)) + AND TIMESTAMPADD(YEAR,-1,FROM_UNIXTIME(1 / 1000)) + OR + 1(t.shipped) + ) + GROUP BY t.clientFk, año + ) sub + GROUP BY clientFk + HAVING ly >= 1 OR cy > 0 + ) + SELECT + SUM(ly > 0 AND cy = 0) SIN_COMPRA, + SUM(ly = 0 AND cy > 0) NUEVOS, + SUM(ly > 0 AND cy > 0) CON_COMPRA + FROM tSales +WITH tBuy AS( + SELECT + b.itemFk, + sum(quantity) quantity, + sum((b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * b.quantity) / sum(b.quantity) cmp + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.worker w ON w.id = it.workerFk + JOIN vn.warehouse wh ON wh.id = tr.warehouseInFk + WHERE it.code = 'RSV' + AND 1(tr.landed) + AND wh.name = 'Algemesi' + GROUP BY b.itemFk), + tNeto AS ( + SELECT + s.id, + sum(sc.value) neto + FROM vn.sale s + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND ct.code IN ('COST', 'MARGIN') + AND wh.name = 'Algemesi' + AND s.quantity + GROUP BY s.id) + SELECT + s.itemFk, + i.longName, + i.`size`, + i.subName, + tb.quantity compradas, + sum(s.quantity) vendidas, + tb.cmp coste, + SUM(s.quantity * tn.neto) / sum(s.quantity) neto, + SUM(s.quantity * tn.neto) / sum(s.quantity) / tb.cmp -1 margen + FROM vn.sale s + JOIN tBuy tb ON tb.itemFk = s.itemFk + JOIN tNeto tn ON tn.id = s.id + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND wh.name = 'Algemesi' + AND s.quantity + GROUP BY s.itemFk; + +WITH tBuy AS( + SELECT + b.itemFk, + sum(quantity) quantity, + sum((b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * b.quantity) / sum(b.quantity) cmp + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.worker w ON w.id = it.workerFk + JOIN vn.warehouse wh ON wh.id = tr.warehouseInFk + WHERE it.code = 'RSV' + AND 1(tr.landed) + AND wh.name = 'Algemesi' + AND b.itemFk = 1 + GROUP BY b.itemFk), + tNeto AS ( + SELECT + s.id, + sum(sc.value) neto + FROM vn.sale s + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND ct.code IN ('COST', 'MARGIN') + AND wh.name = 'Algemesi' + AND s.itemFk = 1 + GROUP BY s.id) +SELECT + s.ticketFk, + t.nickname Cliente, + s.quantity quantity, + tn.neto, + tb.cmp, + tn.neto / tb.cmp -1 margen + FROM vn.sale s + JOIN tNeto tn ON tn.id = s.id + JOIN tBuy tb ON tb.itemFk = s.itemFk + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + JOIN vn.worker w on w.id = it.workerFk + WHERE s.itemFk = 1 + AND 1(t.shipped) + AND wh.name = 'Algemesi' + GROUP BY s.id; + + + SELECT + clientFk, + name CLIENTE, + SUM(IF(año = YEAR(CURDATE())-1, venta,0)) lastYear, + SUM(IF(año = YEAR(CURDATE()), venta,0)) currentYear + FROM ( + SELECT t.clientFk, c.name, sum(s.quantity * s.price) venta, YEAR(t.shipped) año + FROM vn.sale s + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client c ON c.id = a.clientFk + WHERE it.code = 'RSV' + AND c.isRelevant + AND s.quantity + AND s.price + AND ( + t.shipped BETWEEN TIMESTAMPADD(YEAR,-1,FROM_UNIXTIME(1 / 1000)) + AND TIMESTAMPADD(YEAR,-1,FROM_UNIXTIME(1 / 1000)) + OR + 1(t.shipped) + ) + GROUP BY t.clientFk, año + ) sub + GROUP BY clientFk + HAVING currentYear < lastYear / 2 AND lastYear >= 1 +SELECT code FROM vn.itemType +SELECT DISTINCT itemFk + FROM vn.sale s + JOIN vn.ticket t on t.id = s.ticketFk + JOIN vn.warehouse wh ON wh.id = t.warehouseFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + WHERE it.code = 'RSV' + AND 1(t.shipped) + AND wh.name = 'Algemesi' + AND s.quantity +0 +WITH versionData AS ( + SELECT + v.id, + v.status, + v."name", + v.effective_date rc, + COALESCE( + TO_DATE(SUBSTRING(v.description, '^GA (\d{4}-\d{2}-\d{2})$'), 'YYYY-MM-DD'), + v.effective_date + 1 + ) ga + FROM versions v + JOIN projects p ON p.id = v.project_id + WHERE p.identifier = '1' + AND 1(v.effective_date) + ORDER BY v.effective_date +), versionCalendar AS ( + SELECT *, + rc - current_date rcDays, + ga - current_date gaDays + FROM versionData +), minDays AS ( + SELECT MIN(days) minDays + FROM ( + SELECT rcDays days FROM versionCalendar + UNION + SELECT gaDays FROM versionCalendar + ) t + WHERE days > 0 +) +SELECT id, status, "name", + rc, CASE + WHEN rcDays < 0 THEN 'published' + WHEN rcDays = 0 THEN 'today' + WHEN rcDays = minDays THEN 'next' + ELSE 'comming' + END rcStatus, + ga, CASE + WHEN gaDays < 0 THEN 'published' + WHEN gaDays = 0 THEN 'today' + WHEN gaDays = minDays THEN 'next' + ELSE 'comming' + END gaStatus + FROM versionCalendar + JOIN minDays ON TRUE + +7 +development +SELECT * + FROM mdbVersion + ORDER BY app, FIELD(branchFk,'master','test','dev') +SELECT mv.app, mv.branchFk, sub.`version` + FROM mdbVersion mv + JOIN (SELECT app, MAX(`version`) `version` + FROM mdbVersion + GROUP BY app + ) sub ON sub.`version` = mv.`version` +SELECT mvt.app, + mvt.`version`, + mvt.branchFk, + mvt.fromVersion, + sub.branchFk fromBranch, + u.name, + mvt.created + FROM mdbVersionTree mvt + JOIN account.`user` u ON u.id = mvt.userFk + LEFT JOIN ( + SELECT * + FROM mdbVersionTree mvt + ) sub ON sub.`version` = mvt.fromVersion + AND sub.app = mvt.app + ORDER BY mvt.created DESC +SELECT COUNT(app), + branchFk, + DATE(created) time + FROM mdbVersionTree + GROUP BY DATE(created), branchFk +SELECT COUNT(*) 'Nº Ofertas' + FROM edi.supplyResponse sr + WHERE sr.NumberOfUnits > 0 + AND sr.EmbalageCode != 999; +SELECT IFNULL(SUM(filter), 0) 'Nº Ofertas Filtradas' +FROM edi.offerList ol + +SELECT IFNULL(IF(VNH=Algemesí, Algemesí, null), null) 'Nº Of. Filtr. Perdidas' +FROM ( SELECT SUM(filter) - SUM(VNH) VNH, + SUM(filter) - SUM(Algemesi) Algemesí, + IF(SUM(ol.vnh) = SUM(ol.algemesi), 1,0) + FROM edi.offerList ol) ol +SELECT v.name Nombre, + sum(sr.NumberOfUnits > 0) Stock, + sum(sr.NumberOfUnits = 0) 'Sin stock' + FROM edi.supplyResponse sr + JOIN edi.marketPlace mp ON mp.id = sr.MarketPlaceID + JOIN edi.deliveryInformation di ON di.supplyResponseID = sr.ID + JOIN edi.VMPSettings v ON v.VMPID = sr.vmpID + WHERE created > curdate() AND mp.name LIKE 'VMP%' + GROUP BY sr.vmpID; +SELECT IFNULL(sub.numErrors/sub.numOrders * 100, 0) errorsPercent +FROM ( + SELECT SUM(po.OrderStatus = 3) numErrors, COUNT(*) numOrders + FROM edi.putOrder po + WHERE po.OrderTradeLineDateTime BETWEEN IF(HOUR(NOW()) >= 12, TIMESTAMPADD(HOUR , 12, CURDATE()) ,TIMESTAMPADD(HOUR , -12, CURDATE()) ) + AND IF(HOUR(NOW()) >= 12, NOW() ,TIMESTAMPADD(HOUR , 12, CURDATE())) + )sub; +SELECT SUM(po.OrderStatus = 3) Error, + SUM(po.OrderStatus = 0) 'No procesado', + SUM(po.OrderStatus = 1) Pendiente, + SUM(po.OrderStatus = 2) OK + FROM edi.putOrder po + WHERE po.OrderTradeLineDateTime BETWEEN IF(HOUR(NOW()) >= 12, TIMESTAMPADD(HOUR , 12, CURDATE()) ,TIMESTAMPADD(HOUR , -12, CURDATE()) ) + AND IF(HOUR(NOW()) >= 12, NOW() ,TIMESTAMPADD(HOUR , 12, CURDATE())); + +SELECT COUNT(DISTINCT vmpID) numConexionesConOferta +FROM edi.supplyResponse sr +WHERE sr.NumberOfUnits > 0 + AND sr.EmbalageCode != 999; + +SELECT COUNT(DISTINCT so.vmpID) +FROM edi.supplyOffer so +SELECT (SELECT COUNT(*) FROM edi.offerList) - COUNT(DISTINCT so.vmpID) + FROM edi.supplyOffer so +SELECT v.name Proveedor, sum(po.OrderStatus = 3) Errores, sum(po.OrderStatus = 2) Ventas, sum((po.OrderStatus = 2)*po.quantity * sr.Price * sr.NumberOfItemsPerCask) Euros + FROM edi.putOrder po + JOIN edi.supplyResponse sr ON sr.ID = po.supplyResponseID + JOIN edi.VMPSettings v ON v.VMPID = sr.vmpID + WHERE po.OrderTradeLineDateTime BETWEEN IF(HOUR(NOW()) >= 12, TIMESTAMPADD(HOUR , 12, CURDATE()) ,TIMESTAMPADD(HOUR , -12, CURDATE()) ) + AND IF(HOUR(NOW()) >= 12, NOW() ,TIMESTAMPADD(HOUR , 12, CURDATE())) + GROUP BY v.name; +SELECT TIME(MAX(l.created)) 'Último refresh:' + FROM edi.`log` l + WHERE l.tableName = 'floramondo_offerRefresh' +SELECT supplier Proveedores, + total 'Total de Ofertas', + IFNULL(filter, '') 'Ofertas Filtradas', + IFNULL(vnh, '') VNH, + IFNULL(algemesi, '') Algemesi + FROM edi.offerList +SELECT (TIME(MAX(l.created)) + INTERVAL 6 MINUTE) - TIME(CURTIME()) 'Próximo refresh:' + FROM edi.`log` l + WHERE l.tableName = 'floramondo_offerRefresh' +SELECT COUNT(po.deliveryInformationID) Compras, + po.created time +FROM edi.putOrder po +WHERE po.OrderTradeLineDateTime BETWEEN IF(HOUR(NOW()) >= 12, TIMESTAMPADD(HOUR , 12, CURDATE()) ,TIMESTAMPADD(HOUR , -12, CURDATE()) ) + AND IF(HOUR(NOW()) >= 12, NOW() ,TIMESTAMPADD(HOUR , 12, CURDATE())) + AND OrderStatus <> 3 +GROUP BY hour(created) +SELECT created AS time_sec, + CAST(fieldValue AS decimal(10,0)) Fotos + FROM edi.log + WHERE tableName = 'itemImageQueue'; +SELECT FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * b.stickers) / 1000000) 'TOTAl METROS' + FROM vn.buy b + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + LEFT JOIN + (SELECT ish.itemFk, ish.shelvingFk + FROM vn.itemShelving ish + JOIN vn.shelving sh ON sh.code = ish.shelvingFk + WHERE ish.created > curdate() + AND sh.parkingFk IS NULL + GROUP BY ish.itemFk + ) ish ON ish.itemFk = b.itemFk + LEFT JOIN + (SELECT ish.itemFk, sh.code, p.code parkingCode + FROM vn.itemShelving ish + JOIN vn.shelving sh ON sh.code = ish.shelvingFk + JOIN vn.parking p ON p.id = sh.parkingFk + JOIN vn.sector s ON s.id = p.sectorFk + JOIN vn.warehouse w on w.id = s.warehouseFk + WHERE w.name = '1' + GROUP BY ish.itemFk + ) pk ON pk.itemFk = b.itemFk + WHERE w.name = '1' + AND tr.landed = '1' + AND NOT e.isRaid + AND ic.merchandise + +SELECT IF(i.isLaid, 'ACOMODADO', 'EN CARROS') 'GÉNERO', + ic.name REINO, + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * (b.stickers - LEAST(b.stickers, b.printedStickers))) / 1000000) 'Pendientes' , + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * LEAST(b.stickers, b.printedStickers) * (ish.itemFk IS NULL) * (pk.itemFk IS NULL)) / 1000000) 'Etiquetados', + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * LEAST(b.stickers, b.printedStickers) * (ish.itemFk IS NOT NULL)) / 1000000) 'Ubicados', + FLOOR(SUM(IF(p.volume > 0, + p.volume, + p.width * p.depth * IF(IFNULL(p.height,0), p.height, IFNULL(i.size,60) + 10) + ) * LEAST(b.stickers, b.printedStickers) * (pk.itemFk IS NOT NULL )) / 1000000) 'Aparcados' + FROM vn.buy b + JOIN vn.packaging p ON p.id = b.packageFk + JOIN vn.item i ON i.id = b.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + LEFT JOIN + (SELECT ish.itemFk + FROM vn.itemShelving ish + JOIN vn.shelving sh ON sh.code = ish.shelvingFk + JOIN vn.parking p ON p.id = sh.parkingFk + JOIN vn.sector s ON s.id = p.sectorFk + WHERE ish.created BETWEEN '1' AND TIMESTAMPADD(HOUR,23,'1') + AND s.code = 'VNH' + GROUP BY ish.itemFk + ) ish ON ish.itemFk = b.itemFk + LEFT JOIN + (SELECT ish.itemFk + FROM vn.itemShelving ish + JOIN vn.shelving sh ON sh.code = ish.shelvingFk + JOIN vn.parking p ON p.id = sh.parkingFk + JOIN vn.sector s ON s.id = p.sectorFk + WHERE ish.created BETWEEN '1' AND TIMESTAMPADD(HOUR,23,'1') + AND s.code = 'EN TRANSITO' + GROUP BY ish.itemFk + ) pk ON pk.itemFk = b.itemFk + WHERE w.name = '1' + AND b.quantity > 0 + AND tr.landed = '1' + AND NOT e.isRaid + AND ic.merchandise + GROUP BY (i.isLaid = 0), it.categoryFk ; + + +SELECT name + FROM vn.warehouse + WHERE code IS NOT NULL +SELECT left(dated, 10) + FROM vn.time + WHERE dated > TIMESTAMPADD(WEEK, -1, CURDATE()) +WITH tBuy AS ( + SELECT b.itemFk, sum(b.printedStickers) printedStickers, b.packing + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + WHERE w.name = 'VNH' + AND 1(tr.landed) + GROUP BY b.itemFk) +SELECT addressFk, + CONSIGNATARIO, + city CIUDAD, + SUM(ETIQUETAS) ETIQUETAS, + SUM(FALTAN) FALTAN, + SUM(IMPRESAS) IMPRESAS + FROM ( + SELECT + t.addressFk , + t.nickname CONSIGNATARIO, + s.itemFk ITEM, + s.concept DESCRIPCION, + sum(s.quantity) / b.packing ETIQUETAS, + GREATEST(0,(sum(s.quantity) / b.packing) - b.printedStickers) FALTAN, + b.printedStickers IMPRESAS, + a.city + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.province p ON p.id = a.provinceFk + JOIN vn.country ct ON ct.id = p.countryFk + LEFT JOIN tBuy b ON b.itemFk = s.itemFk + WHERE 1(t.shipped) + AND w.name = 'VNH' + AND s.quantity > 0 + AND c.isRelevant + AND ct.country = 'Francia' + GROUP BY s.itemFk, t.addressFk) sub + GROUP BY addressFk; + SELECT DISTINCT t.addressFk + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + WHERE 1(t.shipped) + AND w.name = 'VNH' + AND c.isRelevant; +SELECT id FROM country WHERE country = 'Francia' +WITH tBuy AS ( + SELECT b.itemFk, sum(b.printedStickers) printedStickers, b.packing + FROM vn.buy b + JOIN vn.entry e ON e.id = b.entryFk + JOIN vn.travel tr ON tr.id = e.travelFk + JOIN vn.warehouse w ON w.id = tr.warehouseInFk + WHERE w.name = 'VNH' + AND 1(tr.landed) + GROUP BY b.itemFk) +SELECT + t.addressFk CONSIGNATARIO, + s.itemFk ITEM, + s.concept DESCRIPCION, + sum(s.quantity) / b.packing ETIQUETAS, + GREATEST(0,(sum(s.quantity) / b.packing) - b.printedStickers) FALTAN, + b.printedStickers IMPRESAS + FROM vn.ticket t + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.warehouse w ON w.id = t.warehouseFk + JOIN vn.sale s ON s.ticketFk = t.id + LEFT JOIN tBuy b ON b.itemFk = s.itemFk + WHERE 1(t.shipped) + AND w.name = 'VNH' + AND s.quantity > 0 + AND c.isRelevant + AND t.addressFk = 1 + GROUP BY s.itemFk, t.addressFk; +SELECT z.id ID, z.name ZONA, am.name AGENCIA, z.price 'PRECIO CAJA' + FROM vn.zone z + JOIN vn.agencyMode am ON am.id = z.agencyModeFK + WHERE am.name NOT IN ('EMBALAJES', 'REC FRESHTRANS', 'ARJAN ERIKS', 'BUCHNER', 'OTRA AGENCIA', 'REC ARJAN ERIKS', 'REC TIMETRANS','XPO LOGISTICS', 'FRESHTRANS', 'REC_HOLANDA', 'SERRA', 'BREEWEL', 'REC BREEWEL', 'REC DE WIT', 'DE WIT', 'ENVIALIA', 'SENDING', 'WEMATRANS', 'SCHETTINO', 'SALVENSEN', 'TIPSA', 'VDL A DOMICILIO', 'REC SJAAK DE WIT', 'ABONO', 'SJAAK DEWIT', 'VN_REPARTO+') + AND z.id NOT IN (496, 511, 678, 27, 711, 316, 675, 393, 548, 552, 551, 458, 341, 692, 739) +ORDER BY z.name DESC +SELECT + date AS "time", + id +FROM errorLogApp +WHERE + 1(date) +ORDER BY date +SELECT CAST(SUM(volume) AS DECIMAL(10,2)), d.name + FROM vn.saleVolume sv + JOIN vn.ticket t ON t.id = sv.ticketFk + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + JOIN vn.department d ON d.id = b.departmentFk + JOIN account.user us ON us.id = w.bossFk + WHERE sv.shipped BETWEEN '2022-10-28' AND '2022-10-29' + AND d.name LIKE 'EQUIPO %' + GROUP BY d.name + ORDER BY SUM(volume) DESC