Mysql 组命令

发布于 2024-12-07 20:11:35 字数 3475 浏览 1 评论 0原文

我有一个查询,根据审核日期提取报告,但我很困惑 GROUP 命令如何无法处理我期望的输出。

这是我的查询,

SELECT prd.fldemployeeno `EmployeeNo`,
       prd.fldorderid `OrderNo`,
       prd.fldstarttime `TimeProcessed`,
       COUNT(qua.seqid) `ErrorCount`,
       COALESCE(qua.fldstarttime,(SELECT fldstarttime FROM tblproductionitl p
                                   WHERE (p.fldglobalid = prd.fldglobalid)
                                     AND p.fldprojectgroup=prd.fldprojectgroup
                                     AND p.fldstarttime > prd.fldstarttime
                                     AND prd.fldemployeeno != p.fldemployeeno
                                   LIMIT 0,1)) AS `AuditDate`
  FROM tblproductionitl prd
 INNER JOIN tblisauditeditl aud
    ON prd.fldglobalid=aud.fldid
  LEFT JOIN tblqualityaudit qua
    ON prd.fldglobalid=qua.fldid
   AND prd.fldstarttime=qua.fldprodstarttime
 GROUP BY prd.fldemployeeno,prd.fldorderid
 HAVING `AuditDate` BETWEEN '2011-10-04 00:00:00' AND '2011-10-04 23:59:59'
 ORDER BY `AuditDate`

其输出是

+-------------+---------------+---------------------+------------+---------------------+
| EmployeeNo  |   OrderNo     | TimeProcessed       | ErrorCount |  AuditDate          |
+-------------+---------------+---------------------+------------+---------------------+
| PSAA50577   | 20110930n01   | 2011-10-04 10:41:23 |   3        | 2011-10-04 10:44:07 |   
| PSAA50576   | 20111003n01   | 2011-10-03 11:39:52 |   1        | 2011-10-04 10:58:48 |
| PSAA50515   | 20110930n01   | 2011-10-04 10:44:07 |   1        | 2011-10-04 11:12:03 |
| PSAA50577   | 20111003n02   | 2011-10-03 12:22:33 |   1        | 2011-10-04 16:47:16 |
| PSAA50577   | 20110930n10   | 2011-10-01 18:27:09 |   1        | 2011-10-04 18:29:29 |
+-------------+---------------+---------------------+------------+---------------------+

然后我删除了 GROUP 命令中的 prd.fldorderid ,以便报表仅从 EmployeeNo 进行分组。但输出仅返回 1 行而不是 3 行。请参阅下面的查询和输出。

SELECT prd.fldemployeeno `EmployeeNo`,
       prd.fldorderid `OrderNo`,
       prd.fldstarttime `TimeProcessed`,
       COUNT(qua.seqid) `ErrorCount`,
       COALESCE(qua.fldstarttime,(SELECT fldstarttime FROM tblproductionitl p
                                   WHERE (p.fldglobalid = prd.fldglobalid)
                                     AND p.fldprojectgroup=prd.fldprojectgroup
                                     AND p.fldstarttime > prd.fldstarttime
                                     AND prd.fldemployeeno != p.fldemployeeno
                                   LIMIT 0,1)) AS `AuditDate`
  FROM tblproductionitl prd
 INNER JOIN tblisauditeditl aud
    ON prd.fldglobalid=aud.fldid
  LEFT JOIN tblqualityaudit qua
    ON prd.fldglobalid=qua.fldid
   AND prd.fldstarttime=qua.fldprodstarttime
 GROUP BY prd.fldemployeeno
 HAVING `AuditDate` BETWEEN '2011-10-04 00:00:00' AND '2011-10-04 23:59:59'
 ORDER BY `AuditDate`

该查询的输出是:

+------------+--------------+---------------------+--------------+---------------------+
| EmployeeNo |  OrderNo     | TimeProcessed       | ErrorCount   |  AuditDate          |
+------------+--------------+---------------------+--------------+---------------------+
| PSAA50576  |  20111003n01 | 2011-10-03 11:39:52 | 1            | 2011-10-04 10:58:48 |
+------------+--------------+---------------------+--------------+---------------------+

任何人都可以帮助我分析第二个查询中如何仅返回 1 行以及如何对员工号的输出基础进行分组。

I have a query that pulled out the report depends on Audit Date, but I'm very confuse on how the GROUP command is not working on what I'm expecting for the output.

Here is my queries,

SELECT prd.fldemployeeno `EmployeeNo`,
       prd.fldorderid `OrderNo`,
       prd.fldstarttime `TimeProcessed`,
       COUNT(qua.seqid) `ErrorCount`,
       COALESCE(qua.fldstarttime,(SELECT fldstarttime FROM tblproductionitl p
                                   WHERE (p.fldglobalid = prd.fldglobalid)
                                     AND p.fldprojectgroup=prd.fldprojectgroup
                                     AND p.fldstarttime > prd.fldstarttime
                                     AND prd.fldemployeeno != p.fldemployeeno
                                   LIMIT 0,1)) AS `AuditDate`
  FROM tblproductionitl prd
 INNER JOIN tblisauditeditl aud
    ON prd.fldglobalid=aud.fldid
  LEFT JOIN tblqualityaudit qua
    ON prd.fldglobalid=qua.fldid
   AND prd.fldstarttime=qua.fldprodstarttime
 GROUP BY prd.fldemployeeno,prd.fldorderid
 HAVING `AuditDate` BETWEEN '2011-10-04 00:00:00' AND '2011-10-04 23:59:59'
 ORDER BY `AuditDate`

And the output of this is

+-------------+---------------+---------------------+------------+---------------------+
| EmployeeNo  |   OrderNo     | TimeProcessed       | ErrorCount |  AuditDate          |
+-------------+---------------+---------------------+------------+---------------------+
| PSAA50577   | 20110930n01   | 2011-10-04 10:41:23 |   3        | 2011-10-04 10:44:07 |   
| PSAA50576   | 20111003n01   | 2011-10-03 11:39:52 |   1        | 2011-10-04 10:58:48 |
| PSAA50515   | 20110930n01   | 2011-10-04 10:44:07 |   1        | 2011-10-04 11:12:03 |
| PSAA50577   | 20111003n02   | 2011-10-03 12:22:33 |   1        | 2011-10-04 16:47:16 |
| PSAA50577   | 20110930n10   | 2011-10-01 18:27:09 |   1        | 2011-10-04 18:29:29 |
+-------------+---------------+---------------------+------------+---------------------+

And then I have removed the prd.fldorderid in the GROUP command so that the report will be grouped base from EmployeeNo only. But the output returns only 1 row instead of three. Please see below query and output.

SELECT prd.fldemployeeno `EmployeeNo`,
       prd.fldorderid `OrderNo`,
       prd.fldstarttime `TimeProcessed`,
       COUNT(qua.seqid) `ErrorCount`,
       COALESCE(qua.fldstarttime,(SELECT fldstarttime FROM tblproductionitl p
                                   WHERE (p.fldglobalid = prd.fldglobalid)
                                     AND p.fldprojectgroup=prd.fldprojectgroup
                                     AND p.fldstarttime > prd.fldstarttime
                                     AND prd.fldemployeeno != p.fldemployeeno
                                   LIMIT 0,1)) AS `AuditDate`
  FROM tblproductionitl prd
 INNER JOIN tblisauditeditl aud
    ON prd.fldglobalid=aud.fldid
  LEFT JOIN tblqualityaudit qua
    ON prd.fldglobalid=qua.fldid
   AND prd.fldstarttime=qua.fldprodstarttime
 GROUP BY prd.fldemployeeno
 HAVING `AuditDate` BETWEEN '2011-10-04 00:00:00' AND '2011-10-04 23:59:59'
 ORDER BY `AuditDate`

And the output for this query is:

+------------+--------------+---------------------+--------------+---------------------+
| EmployeeNo |  OrderNo     | TimeProcessed       | ErrorCount   |  AuditDate          |
+------------+--------------+---------------------+--------------+---------------------+
| PSAA50576  |  20111003n01 | 2011-10-03 11:39:52 | 1            | 2011-10-04 10:58:48 |
+------------+--------------+---------------------+--------------+---------------------+

Can anyone help me to analyze this on how only 1 row returned in second query and on how could I group the output base from Employee no.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-12-14 20:11:35

您应该检查分组的方式,来自 MySQL文档

MySQL扩展了GROUP BY的使用,使得选择列表可以引用
未在 GROUP BY 子句中命名的非聚合列。这意味着
前面的查询在 MySQL 中是合法的。您可以使用此功能
通过避免不必要的列排序来获得更好的性能
分组。然而,这主要是当每个值中的所有值
未在 GROUP BY 中命名的非聚合列对于每个列都是相同的
团体。服务器可以自由地从每个组中选择任何值,因此
除非它们相同,否则所选择的值是不确定的。
此外,从每个组中选择值不能
受添加 ORDER BY 子句的影响。结果集排序
在选择值之后发生,ORDER BY 不影响
服务器选择哪些值

编辑哪些值来解释。举个例子:

  SELECT column1
       , column2
GROUP BY column1

如果给定一个column1值,表可以有不同的column2,这是不安全的,因为无论何时执行查询,您都可能获得不同的column2值。
在您的子查询中,您正在执行此操作,您应该重写查询以避免它。

You should check the way you are grouping, from MySQL doc:

MySQL extends the use of GROUP BY so that the select list can refer to
nonaggregated columns not named in the GROUP BY clause. This means
that the preceding query is legal in MySQL. You can use this feature
to get better performance by avoiding unnecessary column sorting and
grouping. However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY are the same for each
group. The server is free to choose any value from each group, so
unless they are the same, the values chosen are indeterminate.
Furthermore, the selection of values from each group cannot be
influenced by adding an ORDER BY clause. Sorting of the result set
occurs after values have been chosen, and ORDER BY does not affect
which values the server chooses

Edit to explain. Given this example:

  SELECT column1
       , column2
GROUP BY column1

If the table can have different column2 given one value of column1 this is unsafe because anytime you execute the query you could get a different value of column2.
In your subquery you are doing this and you should rewrite the query to avoid it.

野稚 2024-12-14 20:11:35

Group by 折叠给定列中具有相同值的所有行。

请注意,对于单行,ErrorCount 为 1。
这通常意味着实际上只有 1 行满足该查询的条件。
换句话说,无论有没有分组依据,输出都是相同的。

尝试将 ErrorCount 的定义更改为 count(*) as ErrorCount
如果 errorcount 仍为 1。您的基础数据已更改,因此仅选择了一行。

Group by collapses all rows with the same value in the given column.

Note though that ErrorCount is 1 for you single row.
That normally means that there really is only 1 row that meets the criteria of that query.
In other words the output will be the same with and without the group by.

Try changing the definition of ErrorCount to count(*) as ErrorCount.
If errorcount is still 1. Your underlying data has changed so that there's only one row being selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文