Query to select Single Best Record for each NameID, grouped by Date, My DataTable under Zoho Reports.
----->
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
-------> Where On each Working Date I get 2 Records for every Name ID. I need to Query out the Best record(Complete Row).
Best Record is selected based on the least value(Better Record) under column “Error Count” Grouped on Date and Result Sorted on Name ID . As shown in OutPut Table below.
xxxxx>>>> Output expected from Query
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>>>
I am using the Zoho Reports(Entry Free Edition), Zoho Reports supports Simple SELECT SQL Querying in multiple dialects like ANSI, Oracle, Microsoft SQL Server, IBM DB2, MySQL, Sybase, PostgreSQL and Informix dialects. we can execute queries written in any of this dialects.
Below is my Query , I feel there is Better way to Query, Pls Suggest. (FYI: As of now zohoReports does not support SELECT query in FROM clause)
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")
For Above Query I am getting Error as "Whenever a table alias is defined, kindly use table alias name before the respective columns used in SELECT query" But I have feel it is met. I am Struck here , need your Help.
I don't know anything about Zoho. However, the following idea should work in all the databases you mention:
Two database differences are relevant. The first is the conversion from a
datetime
to adate
. That might be database dependent. The second is limiting the results to one row. Different database have different ways of doing this.This is using something called a correlated subquery. It fetches all rows that match on the date and name, and then returns the id from the row with the lowest error count.