gpt4 book ai didi

需要 MySQL 查询总和和组建议

转载 作者:行者123 更新时间:2023-11-30 00:34:11 25 4
gpt4 key购买 nike

我尝试了一段时间来运行查询,但这比我想象的要困难。我有以下 3 个表:

Stores:
+--+-------+--------------+-------+---------+---------+----+---------------+---+------+
|ID|Company|Address |PostCod|Latitude |Longitude|Stat| Division |Gro| When |
+--+-------+--------------+-------+---------+---------+----+---------------+---+------+
|17|Company|Site Address 3|WF1 5NT|53.666340|-1.487857|OPEN|Test Division 2|arl|2014-01-31 14:36:04
|18|Company|Site Address 3|WF1 5NT|53.666340|-1.487857|OPEN|Test Division 2|arl|2014-01-31 14:36:04
|19|Company|Site Address 3|WF1 5NT|53.666340|-1.487857|OPEN|Test Division 2|arl|2014-01-31 14:36:04
|20|Company|Site Address 3|WF1 5NT|53.666340|-1.487857|OPEN|Test Division 2|arl|2014-01-31 14:36:04
+--+-------+--------------+-------+---------+---------+----+---------------+---+------+

Jobs:
+--------+-------------------------------------------+-------+------------+------+--------+
| client | description | freq | from | till |job_id |
+--------+-------------------------------------------+-------+------------+------+--------+
| 17 | Weekly external and internal window clean | 7 | 2013-10-01 | NULL | 17 |
| 18 | Weekly external and internal window clean | 7 | 2013-10-01 | NULL | 18 |
| 19 | Weekly external and internal window clean | 7 | 2013-10-01 | NULL | 19 |
| 20 | Weekly external and internal window clean | 7 | 2013-10-01 | NULL | 20 |
| 17 | 4 weekly fascia and upper floor windows | 28 | 2013-10-01 | NULL | 645 |
| 18 | 4 weekly fascia and upper floor windows | 28 | 2013-10-01 | NULL | 646 |
| 19 | 4 weekly fascia and upper floor windows | 28 | 2013-10-01 | NULL | 647 |
| 20 | 4 weekly fascia and upper floor windows | 28 | 2013-10-01 | NULL | 648 |
+--------+-------------------------------------------+-------+------------+------+--------+


Job_hist
+------+-------------------+-----------------+---------+------+--------------+-----------------------------------------+-------------------------------+------+-----+-----------------------+------+
|Job_id| last | user | Company | LINK | Signedy_by | Job Description | Address | Accu |Days | Possition |Uniqid|
+------+-------------------+-----------------+---------+------+--------------+-----------------------------------------+-------------------------------+------+-----+-----------------------+------+
|17 |2013-10-01 09:35:37|ARL_Operative_013|Santander| LINK |tony moore |Weekly external window clean | AN WELLING 14 BR (DA16 3PP) |739.00|10 |Branch Manager |132 |
|20 |2013-10-02 12:27:51|ARL_Operative_013|Santander| LINK |alex goodman |Weekly external window clean | AN HAROLD HILL 69 FR (RM3 8XA)|55.00 |6 |Store Assistant Manager|268 |
|19 |2013-10-03 09:14:19|ARL_Operative_013|Santander| LINK |darren pickett|Weekly external window clean | AN WOOLWICH 41 PS (SE18 6JD) |50.00 |5 |Other |332 |
|18 |2013-10-03 09:54:49|ARL_Operative_013|Santander| LINK |james lawrence|Weekly external window clean | AN ELTHAM 73 EHS (SE9 1UW) |49.00 |7 |Other |346 |
|17 |2013-10-08 09:05:16|ARL_Operative_013|Santander| LINK |tony moore |Weekly external and internal window clean| AN WELLING 14 BR (DA16 3PP) |67.00 |6 |Branch Manager |697 |
+------+-------------------+-----------------+---------+------+--------------+-----------------------------------------+-------------------------------+------+-----+-----------------------+------+

我需要编写一个查询,根据除法进行一些性能计算。目标是:

  1. 计算给定时间范围内安排的清洁次数(简单版本),获取开始日期和结束日期,计算天数并除以商店freq

  2. 在此期间按时完成了多少计划清洁。我们从 job_hist 中获取这些数据(其中 job_hist.jod_id=jobs.job_id 和 job_hist.last 落在间隔内,并且 job_hist.days <= freq

  3. 按时完成清洁的百分比

  4. 有多少次清理工作延迟完成(与第 2 点中的逻辑相同,只是 job_hist> freq)

  5. 较晚完成的清洁百分比

  6. 错过了多少次(预定 - 按时完成 - 延迟完成)

  7. 错过的百分比

所有这些都应该按部门分组,因此结果应该如下所示:

+------------------------------+---------+------+-----------------+---------+--------------------+------+-----------------+
|division |scheduled|ontime|ontime_percentage|completed|completed_percentage|missed|missed_percentage
+------------------------------+---------+------+-----------------+---------+--------------------+------+-----------------+
|fsdfoihsdfljksdlgjdfsligsgfsfd|16282 |10404 |63.90% |10825 |66.48% |5457 |33.52%
|Test Division 2 |259 |129 |49.81% |133 |51.35% |126 |48.65%
|Test Division 3 |30 |15 |50.00% |15 |50.00% |15 |50.00%
+------------------------------+---------+------+-----------------+---------+--------------------+------+-----------------+

现在我不太擅长 SQL 查询,但我已经成功地组合了以下查询:

SELECT `stores`.`division`, 
SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) as `scheduled`,
count(`name`) as `ontime`,
concat(round(( count(`name`)/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS ontime_percentage,
count(`user`) as `completed`,
concat(round(( count(`user`)/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS completed_percentage,
(SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-count(`user`)) as `missed`,
concat(round(( (SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-count(`user`)) /SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS missed_percentage
from `stores` left join `jobs` on `stores`.`id`=`jobs`.`client`
left join (select `user`,`job_id` from `job_hist` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till') as `myquery` on `myquery`.`job_id`=`jobs`.`job_id`
left join (select `name`,`job_hist`.`job_id` from `job_hist` left join `jobs` on `jobs`.`job_id`=`job_hist`.`job_id` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till' and `job_hist`.`days`<=`jobs`.`freq`) as `myquery2` on `myquery2`.`job_id`=`jobs`.`job_id`
where `stores`.`owner`='$group'
group by `division`

$till、$from、$group 是一些 PHP 变量,$till 和 $from 是字符串格式的日期,$group 是字符串。

现在查询运行良好,但是(总是有一个但是),Scheduled 字段正在汇总不同的值,我的意思是有时它使值加倍,有时它使值增加三倍。

我已阅读THIS但我仍然无法理解它。我或多或少明白我的一些结果翻了一番,但仍然不知道我需要改变什么。

在有人建议加入 stores.divisionjob_hist.division 之间的划分之前,这是不可行的,由于部门可以及时更改,并且它们会在 stores 中更新,但不会在 job_hist 中更新。job_hist 不应随时修改,因此仅包含历史数据。

更新

好的,我设法调整我的查询,到目前为止,我看起来得到了正确的数据,但在发布我自己的答案之前,我需要运行更多测试来确定。无论如何,这是我更新的查询:

SELECT `stores`.`division`, 
SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) as `scheduled`,
sum(`ontime`),
concat(round(( sum(`ontime`)/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS ontime_percentage,
sum(`completed`-`ontime`),
concat(round(( sum(`completed`-`ontime`)/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS completed_percentage,
(SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-sum(`completed`)) as `missed`,
concat(round(( (SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-sum(`completed`)) /SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS missed_percentage from `stores`
left join `jobs` on `stores`.`id`=`jobs`.`client`
left join (select count(`user`) as `completed`,`job_id` from `job_hist` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till' group by `job_id`) as `myquery` on `myquery`.`job_id`=`jobs`.`job_id`
left join (select count(`name`) as `ontime`,`job_hist`.`job_id` from `job_hist` left join `jobs` on `jobs`.`job_id`=`job_hist`.`job_id` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till' and `job_hist`.`days`<=`jobs`.`freq` group by `job_id`) as `myquery2` on `myquery2`.`job_id`=`jobs`.`job_id`
where `stores`.`owner`='$group'
group by `stores`.`division`

我愿意接受更好的方式来运行此查询。

最佳答案

问题出在分组上,因此这里是正确返回数据的查询:

SELECT `stores`.`division`, 
SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) as `scheduled`,
sum(`ontime`),
concat(round(( sum(`ontime`)/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS ontime_percentage,
sum(`completed`)-sum(`ontime`),
concat(round(( (sum(`completed`)-sum(`ontime`))/SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS completed_percentage,
(SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-sum(`completed`)) as `missed`,
concat(round(( (SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`)-sum(`completed`)) /SUM(DATEDIFF(LEAST(IFNULL(`till`,CURDATE()),'$till'),GREATEST(`from`,'$from')) DIV `freq`) * 100 ),2),'%') AS missed_percentage from `stores`
left join `jobs` on `stores`.`id`=`jobs`.`client`
left join (select count(`user`) as `completed`,`job_id` from `job_hist` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till' group by `job_id`) as `myquery` on `myquery`.`job_id`=`jobs`.`job_id`
left join (select count(`name`) as `ontime`,`job_hist`.`job_id` from `job_hist` left join `jobs` on `jobs`.`job_id`=`job_hist`.`job_id` where `job_hist`.`last`>='$from' and `job_hist`.`last`<='$till' and `job_hist`.`days`<=`jobs`.`freq` group by `job_id`) as `myquery2` on `myquery2`.`job_id`=`jobs`.`job_id`
where `stores`.`owner`='$group'
group by `stores`.`division`

如果有人有更好的解决方案,请发布。

关于需要 MySQL 查询总和和组建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22302992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com