查询以根据列值选择单个最佳记录

时间:2013-09-01 09:47:04

标签: sql database zoho

查询为每个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

-------&GT; 在每个工作日期,我为每个名称ID获得2条记录。 我需要查询最佳记录(完整行)。

根据日期上分组的“错误计数”列和名称ID上排序的结果,根据最小值(更好记录)选择最佳记录。如下面的OutPut表所示。

XXXXX&GT;&GT;&GT;&GT;             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&GT;&GT;&GT;

我正在使用Zoho Reports(免费入门版),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")

对于Above Query,我得到错误为“每当定义一个表别名时,请在SELECT查询中使用的相应列之前使用表别名”但我觉得它已经满足。我在这里很开心,需要你的帮助。

1 个答案:

答案 0 :(得分:0)

我对Zoho一无所知。但是,以下想法应该适用于您提到的所有数据库:

SELECT myTable.*
FROM "myTable"
WHERE myTable."ID" = (SELECT T."ID"
                      FROM "myTable" T
                      WHERE cast(T."date n time" as date) < cast(myTable."date n time" as date) and
                            T.Name = myTable.Name
                      ORDER BY myTable."Error Count"
                      LIMIT 1
                     ) 

两个数据库差异是相关的。第一种是从datetimedate的转换。这可能与数据库有关。第二个是将结果限制为一行。不同的数据库有不同的方法。

这是使用称为相关子查询的东西。它获取与日期和名称匹配的所有行,然后从错误计数最低的行返回id。