1

注意:我正在使用 SQL 2000。

我想通过为此加入执行类似的操作来选择 t2.JobCompletionTime 的 MAX 日期的行,但我知道这不是正确的代码/语法,因为它不起作用......但我希望很清楚我是什么我正在尝试做。

LEFT OUTER JOIN (
  SELECT *
  FROM DSM_StandardProcedureActivityView
  WHERE
  ( (PackageName = 'Adobe Acrobat 10'  AND PackageVersion = '-1.0') AND
    (MAX (JobCompletionTime))
  )
) t2 ON t1.UUID = t2.UUID

我现在的完整工作 SQL 代码是:

SELECT 
t1.Business,
t1.[Sub-Business],
t1.Pole,
t1.DomainManager,
t1.ScalabilityServer,
t1.Location,
t1.Country,
t1.Name,
t3.VolumeName,
t3.VolumeFreeInMB,
t3.VolumeSizeInMB,
FROM (
  SELECT *
  FROM DSM_StandardGroupMembersView
  WHERE 
  (  GroupName = 'Adobe Acrobat'
  )
) t1
LEFT OUTER JOIN (
  SELECT *
  FROM DSM_StandardProcedureActivityView
  WHERE
  ( (PackageName = 'Adobe Acrobat 10'  AND PackageVersion = '-1.0')
  )
) t2 ON t1.UUID = t2.UUID
LEFT OUTER JOIN (
  SELECT *
  FROM DSM_StandardHardwareDiskUsageView
  WHERE VolumeName = 'C:\'
) t3 ON t1.Name = t3.Name
ORDER BY t1.Business, t1.[Sub-Business], t1.Pole, t1.DomainManager, t1.ScalabilityServer, t1.Country, t1.Location, [Job Status], t1.Name
4

3 回答 3

1

您发布了太多不相关的 SQL,我无法涉足,所以我将修复您的摘录:

LEFT OUTER JOIN (
  SELECT * FROM (
    SELECT *
    FROM DSM_StandardProcedureActivityView
    WHERE PackageName = 'Adobe Acrobat 10'
    AND PackageVersion = '-1.0'
    ORDER BY JobCompletionTime DESC) x
   ) y
   GROUP BY PackageName, PackageVersion
) t2 ON t1.UUID = t2.UUID

这里发生的是内部查询首先获取按最新 JobCompletionTime 排序的行,下一级查询在匹配列上使用 group by,在 mysql 中,它为您提供第一个匹配行(最新的,因为我们对行进行了排序)

于 2012-05-01T13:32:43.470 回答
1

一个名为 ROW_NUMBER() 的排名函数可以在这里为您提供帮助

LEFT OUTER JOIN (
    SELECT 
        *, 
        rowNumber = ROW_NUMBER() OVER (ORDER BY JobCompletionTime DESC)
    FROM DSM_StandardProcedureActivityView
    WHERE PackageName = 'Adobe Acrobat 10'
    AND PackageVersion = '-1.0'
) t2 ON t1.UUID = t2.UUID AND rowNumber = 1

按 JobCompletionTime 排序(降序)应将最高值作为第 1 行;因此在连接子句中添加了额外的子句。

于 2012-05-01T13:38:07.950 回答
1

逐步完成此操作,以使其在此处可能会出现解释错误的地方更加明显...

第 1 步:稍微美化您的查询 + 省略,ORDER BY因为您可以稍后再次轻松添加该查询

SELECT t1.Business,
       t1.[Sub-Business],
       t1.Pole,
       t1.DomainManager,
       t1.ScalabilityServer,
       t1.Location,
       t1.Country,
       t1.Name,
       t3.VolumeName,
       t3.VolumeFreeInMB,
       t3.VolumeSizeInMB

  FROM DSM_StandardGroupMembersView  t1

  LEFT OUTER JOIN DSM_StandardProcedureActivityView t2
               ON t2.PackageName = 'Adobe Acrobat 10'  
              AND t2.PackageVersion = '-1.0'
              AND t2.UUID = t1.UUID

  LEFT OUTER JOIN DSM_StandardHardwareDiskUsageView t3
               ON t3.VolumeName = 'C:'
              AND t3.Name = t1.Name

 WHERE t1.GroupName = 'Adobe Acrobat'

我理解您的问题的方式是您希望将 DSM_StandardProcedureActivityView t2 中的记录限制为仅代表最新情况的记录(基于 t2.JobCompletionTime 字段)。对 ?现在要么我们先把它分组,然后加入那些匹配的(通过 UUID),或者我们反过来想,首先让列表限制为所需的 UUID,然后去掉所有那些有更新的 UUID版本。由于您使用的是 SQL2000,我建议使用后者,因为我不确定查询优化器是否能够优化前者。(随意测试=)

这看起来像这样:

SELECT t1.Business,
       t1.[Sub-Business],
       t1.Pole,
       t1.DomainManager,
       t1.ScalabilityServer,
       t1.Location,
       t1.Country,
       t1.Name,
       t3.VolumeName,
       t3.VolumeFreeInMB,
       t3.VolumeSizeInMB

  FROM DSM_StandardGroupMembersView  t1

  LEFT OUTER JOIN DSM_StandardProcedureActivityView t2
               ON t2.PackageName = 'Adobe Acrobat 10'  
              AND t2.PackageVersion = '-1.0'
              AND t2.UUID = t1.UUID
              -- limit to latest records only!
              AND NOT EXISTS ( SELECT *
                                 FROM DSM_StandardProcedureActivityView t2_newer
                                WHERE t2_newer.UUID = t2.UUID
                                  AND t2_newer.JobCompletionTime > t2.JobCompletionTime )

  LEFT OUTER JOIN DSM_StandardHardwareDiskUsageView t3
               ON t3.VolumeName = 'C:'
              AND t3.Name = t1.Name

 WHERE t1.GroupName = 'Adobe Acrobat'

希望这可以帮助。

于 2012-05-03T08:06:14.497 回答