【问题标题】:Query to select Single Best Record based on a Column Value查询以根据列值选择单个最佳记录
【发布时间】:2013-09-01 09:47:04
【问题描述】:

查询为每个 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 查询中使用的相应列之前使用表别名”但我觉得它已经满足了。我被击中了,需要你的帮助。

【问题讨论】:

标签: sql database zoho


【解决方案1】:

我对 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。

【讨论】:

  • 嗨,戈登,谢谢,尽管它对 Zoho Reports 不起作用,我终于通过两个单独的查询实现了这一点 Q1 创建了一个查询表 QT1:选择“名称 ID”、“日期 n 时间” , Min("Error Count") MinErrCnt From "MyTable" Group By "Name ID", Date("Date n Time") and another QueryTable QT2 Select a.* From "MyTable" a inner join "Q1" b ON a. "Name ID" = b."Name ID" AND a."Error Count" = b."MinErrCnt" AND a."Date n Time" = b."Date n Time" Group by a."Date n Time", a.“姓名 ID”帮助我得到了结果。谢谢 Satish K
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多