I am trying query for the most recent scan date (the two most recent ones are 3/5/2012 and 3/1/2012 … I’d like to only return records from 3/5/2012, but I need this to be automated so every time a new scan is run the query is only pulling the most recent date.
I’m also joining the two tables by IP. I put together the query below and am receiving the error “Please check the SQL syntax. Column qryReportsCondensedPatchesAggregate.ScanName is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.”
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
Edit: Here is some sample data of what is being returned:
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
First table:
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
2nd table:
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
Your error is telling you the problem, if you are using an
AggregatefunctionMAX(),SUM(), etc the fields not using the Aggregate must be included in aGROUP BY.Below is what your query looks like with aliases and a
GROUP BY