查询为每个 NameID 选择单个最佳记录,按日期分组,Zoho Reports 下的 My DataTable。
----->
ID Name ID Name Others Colmns Date n Time Error Count Best Unique Record for the Date
1 W0026 Hari x ¦ x ¦ 2013,08,30 14:09:18 13
2 W0027 Johnson x ¦ x ¦ 2013,08,30 14:01:44 0 < This Record for Date 30th
3 W0029 Prem x ¦ x ¦ 2013,08,30 14:04:04 2
4 W0038 Philip x ¦ x ¦ 2013,08,30 14:00:20 0 < This Record for Date 30th
5 W0039 Amit x ¦ x ¦ 2013,08,30 14:08:03 6 <Can Select Eihter of record ID's( 5 and 10) as Error Count of both ID's is Same, for Date 30th
6 W0026 Hari x ¦ x ¦ 2013,08,30 8:09:18 10 < This Record for Date 30th
7 W0027 Johnson x ¦ x ¦ 2013,08,30 8:01:44 4
8 W0029 Prem x ¦ x ¦ 2013,08,30 8:04:04 0 < This Record for Date 30th
9 W0038 Philip x ¦ x ¦ 2013,08,30 8:00:20 1
10 W0039 Amit x ¦ x ¦ 2013,08,30 8:08:03 6
11 W0026 Hari x ¦ x ¦ 2013,08,29 14:09:18 5 < This Record for Date 29th
12 W0027 Johnson x ¦ x ¦ 2013,08,29 14:01:44 1
13 W0029 Prem x ¦ x ¦ 2013,08,29 14:04:04 1 < Latest or Any one if Error Count is Same (between ID 5 and 10) for Date 29th
14 W0038 Philip x ¦ x ¦ 2013,08,29 14:00:20 0 < This Record for Date 29th
15 W0039 Amit x ¦ x ¦ 2013,08,29 14:08:03 6
16 W0026 Hari x ¦ x ¦ 2013,08,29 8:09:18 8
17 W0027 Johnson x ¦ x ¦ 2013,08,29 8:01:44 0 < This Record for Date 29th
18 W0029 Prem x ¦ x ¦ 2013,08,29 8:04:04 1
19 W0038 Philip x ¦ x ¦ 2013,08,29 8:00:20 1
20 W0039 Amit x ¦ x ¦ 2013,08,29 8:08:03 0 < This Record for Date 29th
-------> 在每个工作日期,我为每个名称 ID 获得 2 条记录。我需要查询出最佳记录(完整行)。
根据日期分组的“错误计数”列下的最小值(更好的记录)选择最佳记录,结果按名称 ID 排序。如下面的输出表所示。
xxxxx>>>> 查询的预期输出
ID Name ID Name x ¦ x ¦ Date & Time Error Count Comment
6 W0026 Hari x ¦ x ¦ 2013,08,30 8:09:18 10
2 W0027 Johnson x ¦ x ¦ 2013,08,30 14:01:44 0
8 W0029 Prem x ¦ x ¦ 2013,08,30 8:04:04 0 < BEST in Each Name ID on 30th
4 W0038 Philip x ¦ x ¦ 2013,08,30 14:00:20 0
5 W0039 Amit x ¦ x ¦ 2013,08,30 14:08:03 6
11 W0026 Hari x ¦ x ¦ 2013,08,29 14:09:18 5
17 W0027 Johnson x ¦ x ¦ 2013,08,29 8:01:44 0
13 W0029 Prem x ¦ x ¦ 2013,08,29 14:04:04 1 < BEST in Each Name ID on 29th
14 W0038 Philip x ¦ x ¦ 2013,08,29 14:00:20 0
20 W0039 Amit
x ¦ x ¦ 2013,08,29 8:08:03 0
xxxxxxx>>>
我使用的是 Zoho Reports(Entry Free Edition),Zoho Reports 支持多种方言的简单 SELECT SQL 查询,如 ANSI、Oracle、Microsoft SQL Server、IBM DB2、MySQL、Sybase、PostgreSQL 和 Informix 方言。我们可以执行用任何这种方言编写的查询。
以下是我的查询,我觉得有更好的查询方式,请建议。(仅供参考:目前 zohoReports 不支持 FROM 子句中的 SELECT 查询)
SELECT myTable.* FROM "myTable"
WHERE myTable."ID"= (SELECT T."ID"=myTable."ID"
FROM "myTable" AS T
WHERE T."Error Count" < myTable."Error Count"
ORDER BY myTable."Error Count" DESC
LIMIT 1)
GROUP BY myTable."Name ID", DATE(myTable."Date n Time")
对于上述查询,我收到错误,因为“只要定义了表别名,请在 SELECT 查询中使用的相应列之前使用表别名”但我觉得它已经满足了。我被击中了,需要你的帮助。