2014-07-26 1 views
0

이 쿼리는 정상적으로 실행되지만 where 절을 마지막 부분을 제외한 모든 subselect에 추가하면 오류가 발생합니다. 예제와 오류가 아래에 나와 있습니다. 나는 다음과 같이이 오류를 생성 할 때 당신이 날이 구문 오류?구문 errow when added where 절에서 sub select

SELECT Getdate()           AS PullTime, 
     (SELECT Min(sqlagentstatus) 
     FROM dbo.tracksqlagentstatus)      AS SQLAgentStatus, 
     (SELECT Max(memorygrants) 
     FROM dbo.trackmemorygrants)      AS MemoryGrants, 
     (SELECT Avg(pagelifeexpectancy) 
     FROM trackple)         AS PLE, 
     (SELECT Avg(freepages) 
     FROM dbo.trackfreepages)       AS FreePages, 
     (SELECT Avg(sqlprocesscpu) 
     FROM dbo.trackcpu 
     WHERE capturetime > Dateadd(minute, -5, Getdate()))AS CPU 

를 해결하는 데 도움이 수 있습니까, 하위 쿼리의 일부에 where 절을 추가해야하지만. 이 오류를 해결하기 위해 내가 뭘 할 수 있는지 아십니까? 수신

SELECT Getdate()            AS PullTime, 
     (SELECT Min(sqlagentstatus) 
     FROM dbo.tracksqlagentstatus)      AS SQLAgentStatus, 
     (SELECT Max(memorygrants) 
     FROM dbo.trackmemorygrants)      AS MemoryGrants, 
     (SELECT Avg(pagelifeexpectancy) 
     FROM trackple)          AS PLE, 
     (SELECT Avg(freepages) 
     FROM dbo.trackfreepages)       AS FreePages 
     WHERE capturetime > Dateadd(minute, -5, Getdate()), 
     (SELECT Avg(sqlprocesscpu) 
     FROM dbo.trackcpu 
     WHERE capturetime > Dateadd(minute, -5, Getdate())) AS CPU 

오류 메시지 :

Msg 102, Level 15, State 1, Line 12 
Incorrect syntax near ','. 
Msg 156, Level 15, State 1, Line 15 
Incorrect syntax near the keyword 'as'. 

이 Microsoft SQL Server 데이터베이스입니다.

답변

1

하위 쿼리의 WHERE가 하위 쿼리 외부에 있습니다. 또한 WHERE가 필요한 경우 하위 쿼리에서 별칭을 사용해야합니다. 작동해야 함 :

SELECT Getdate()           AS PullTime, 
    (SELECT Min(sqlagentstatus) 
    FROM dbo.tracksqlagentstatus)      AS SQLAgentStatus 
    , 
    (SELECT Max(memorygrants) 
    FROM dbo.trackmemorygrants)       AS MemoryGrants, 
    (SELECT Avg(pagelifeexpectancy) 
    FROM trackple)          AS PLE, 
    (SELECT Avg(freepages) 
    FROM dbo.trackfreepages X 
    WHERE X.capturetime > Dateadd(minute, -5, Getdate())) AS FreePages, 
    (SELECT Avg(sqlprocesscpu) 
    FROM dbo.trackcpu 
    WHERE capturetime > Dateadd(minute, -5, Getdate())) AS CPU