2012-03-07 23 views
0

最新のスキャン日を検索しようとしています(最新の2つは2012年3月5日と2012年3月1日です...) 2012年3月5日、私はこれを自動化する必要があるので、新しいスキャンが実行されるたびに最新の日付が取得されます。最新の日付を選択する+内部結合

私はIPで2つのテーブルに参加しています。以下のクエリを実行し、エラーが発生します「SQL構文を確認してくださいそれは、集計関数またはGROUP BY句に含まれていないため、列qryReportsCondensedPatchesAggregate.ScanNameが選択リストでは無効です。」

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 
+7

使用別名を持つように見えるものです!!!! –

答えて

2

あなたが使用している場合、あなたのエラーは、あなたの問題を語っていますAggregate機能MAX()SUM()など、集計を使用しないフィールドはGROUP BYに含める必要があります。

以下

は、クエリがエイリアスとGROUP BY

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

ありがとうございます。 "SQLの構文をチェックしてください。列" qryReportsCondensedPatchesAggregate.ScanDate "は、集計関数またはGROUP BY句に含まれていないため、ORDER BY句では無効です。 – Ben

+0

これは、それをGROUP BY句にも追加する必要があることを意味します。私は私の答えを編集しました。 – Taryn

+0

ありがとうございます、今すぐ表示されます。しかし、私は2012年3月5日の最新の日付を取得していません。私はまだ3/1/2012を得ています。私は間違ったトラックにこのためのMAXの集計関数を使用していますか? – Ben

関連する問題