选择最近的日期+内部联接

时间:2012-03-07 15:08:47

标签: sql

我正在尝试查询最近的扫描日期(最近的两个是2012年3月5日和2012年3月1日......我只想从2012年3月5日返回记录,但是我需要将其自动化,因此每次运行新扫描时,查询只会拉动最近的日期。

我也是通过IP加入这两个表。我把下面的查询放在一起,并收到错误“请检查SQL语法。列qryReportsCondensedPatchesAggregate.ScanName在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。”

SELECT qryReportsCondensedPatchesAggregate.ScanName,
    qryReportsCondensedPatchesAggregate.pspplMSSeverity,
    qryReportsCondensedPatchesAggregate.smachIPAddress,
    qryReportsScanSummaryWithDetailsAggregate.PatchMissing, 
    MAX(qryReportsCondensedPatchesAggregate.ScanDate)
FROM qryReportsCondensedPatchesAggregate
INNER JOIN qryReportsScanSummaryWithDetailsAggregate
ON qryReportsCondensedPatchesAggregate.smachIPAddress=qryReportsScanSummaryWithDetailsAggregate.smachIPAddress
WHERE qryReportsCondensedPatchesAggregate.ScanName LIKE '%mgmt%' 
    AND qryReportsCondensedPatchesAggregate.ScanDate>= dateadd(day,-30,getdate())
ORDER BY qryReportsCondensedPatchesAggregate.ScanDate DESC

编辑:以下是返回内容的一些示例数据:

Column1 smachIPAddress   ScanName   pspplMSSeverity PatchMissing

3/5/2012 15:24  162.1.1.1   Mgmt Servers OU 0   1
3/5/2012 15:24  162.1.1.1   Mgmt Servers OU 0   1
3/5/2012 15:24  1162.1.1.1  Mgmt Servers OU 0   1
3/5/2012 15:24  162.1.1.1   Mgmt Servers OU 0   2
3/5/2012 15:24  162.1.1.1   Mgmt Servers OU 0   1
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   2
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   4
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   2
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   3
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   1
3/1/2012 17:29  162.1.1.1   Mgmt Servers OU 0   3

第一张表:

table_name  ordinal_position    column_name data_type   is_nullable character_maximum_length
qryReportsCondensedPatchesAggregate 1   MachineGroup    nvarchar    YES 276
qryReportsCondensedPatchesAggregate 2   ScanID  int NO  
qryReportsCondensedPatchesAggregate 3   ScanUser    nvarchar    YES 4000
qryReportsCondensedPatchesAggregate 4   ScanDate    datetime    NO  
qryReportsCondensedPatchesAggregate 5   ScanName    nvarchar    YES 255
qryReportsCondensedPatchesAggregate 6   itemType    int YES 
qryReportsCondensedPatchesAggregate 7   smachID int NO  
qryReportsCondensedPatchesAggregate 8   smachName   nvarchar    NO  255
qryReportsCondensedPatchesAggregate 9   smachIPAddress  nvarchar    YES 39
qryReportsCondensedPatchesAggregate 10  smachDomainName nvarchar    YES 255
qryReportsCondensedPatchesAggregate 11  smachIPDecimal  float   YES 
qryReportsCondensedPatchesAggregate 12  smachAnnotation ntext   YES 1073741823
qryReportsCondensedPatchesAggregate 13  smachNotFoundReasonErrorNum smallint    YES 
qryReportsCondensedPatchesAggregate 14  smachServerType int YES 
qryReportsCondensedPatchesAggregate 15  mmOSID  int YES 
qryReportsCondensedPatchesAggregate 16  patchBulletinID nvarchar    NO  255
qryReportsCondensedPatchesAggregate 17  patchBulletinURL    nvarchar    YES 4000
qryReportsCondensedPatchesAggregate 18  patchQNumbers   nvarchar    NO  255
qryReportsCondensedPatchesAggregate 19  patchLevel  int YES 
qryReportsCondensedPatchesAggregate 20  pspplMSSeverity int YES 
qryReportsCondensedPatchesAggregate 21  pspplCriticality    int YES 
qryReportsCondensedPatchesAggregate 22  pspplAnnotation nvarchar    YES 4000
qryReportsCondensedPatchesAggregate 23  prodID  int NO  
qryReportsCondensedPatchesAggregate 24  spplspID    int NO  
qryReportsCondensedPatchesAggregate 25  scanType    int YES

第二桌:

table_name  ordinal_position    column_name data_type   is_nullable character_maximum_length
qryReportsScanSummaryWithDetailsAggregate   1   ScanID  int NO  
qryReportsScanSummaryWithDetailsAggregate   2   scanType    int YES 
qryReportsScanSummaryWithDetailsAggregate   3   ScanName    nvarchar    YES 255
qryReportsScanSummaryWithDetailsAggregate   4   ScanDate    datetime    NO  
qryReportsScanSummaryWithDetailsAggregate   5   ScanUser    nvarchar    YES 4000
qryReportsScanSummaryWithDetailsAggregate   6   consoleID   int YES 
qryReportsScanSummaryWithDetailsAggregate   7   consoleName nvarchar    YES 255
qryReportsScanSummaryWithDetailsAggregate   8   smachName   nvarchar    NO  255
qryReportsScanSummaryWithDetailsAggregate   9   smachIPAddress  nvarchar    YES 39
qryReportsScanSummaryWithDetailsAggregate   10  scantName   nvarchar    YES 255
qryReportsScanSummaryWithDetailsAggregate   11  FirstPatchGroupName nvarchar    YES 255
qryReportsScanSummaryWithDetailsAggregate   12  CountPatchGroups    int YES 
qryReportsScanSummaryWithDetailsAggregate   13  smachAgent  smallint    NO  
qryReportsScanSummaryWithDetailsAggregate   14  smachDomainName nvarchar    YES 255
qryReportsScanSummaryWithDetailsAggregate   15  smachIPDecimal  float   YES 
qryReportsScanSummaryWithDetailsAggregate   16  smachID int NO  
qryReportsScanSummaryWithDetailsAggregate   17  smachListType   int YES 
qryReportsScanSummaryWithDetailsAggregate   18  ScanXMLVersion  nvarchar    YES 50
qryReportsScanSummaryWithDetailsAggregate   19  ScanXMLDate datetime    YES 
qryReportsScanSummaryWithDetailsAggregate   20  itemLanguageID  nvarchar    YES 4
qryReportsScanSummaryWithDetailsAggregate   21  PatchFoundOrInstalled   int YES 
qryReportsScanSummaryWithDetailsAggregate   22  PatchMissing    int YES 
qryReportsScanSummaryWithDetailsAggregate   23  ServicePackMissing  int YES 
qryReportsScanSummaryWithDetailsAggregate   24  CountofMachinesScanned  int NO  
qryReportsScanSummaryWithDetailsAggregate   25  CountofMachinesNotScanned   int NO  
qryReportsScanSummaryWithDetailsAggregate   26  TotalMachines   int YES 
qryReportsScanSummaryWithDetailsAggregate   27  ScanFoundPatches    int YES 
qryReportsScanSummaryWithDetailsAggregate   28  ScanMissingPatches  int YES 
qryReportsScanSummaryWithDetailsAggregate   29  ScanServicePackMissing  int YES 
qryReportsScanSummaryWithDetailsAggregate   30  CountOfGroups   int YES 
qryReportsScanSummaryWithDetailsAggregate   31  grpName nvarchar    YES 255

1 个答案:

答案 0 :(得分:2)

您的错误告诉您问题,如果您使用Aggregate函数MAX()SUM()等,则不使用聚合的字段必须包含在{{1}中}。

以下是使用别名和GROUP BY

查询的内容
GROUP BY