1

When I run this, it works.

   select avg(SQLProcessCPU)
    from   dbo.TrackCPU
    where  CaptureTime > DATEADD(MINUTE, -5, GETDATE())

But, when I run it with an: 'as' command. it throws an error. Does anyone know why? Please help.

select avg(SQLProcessCPU)
    from   dbo.TrackCPU
    where  CaptureTime > DATEADD(MINUTE, -5, GETDATE()) as CPU

Error Message: Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'as'

4
  • As stands for Alias. But alias of what in your case?
    – Sebas
    Jul 26, 2014 at 4:47
  • 2
    You can use AS after an expression in the select-list, or after a table expression in the FROM clause. You can't use AS in the WHERE clause -- unless it is inside a sub-select statement in the WHERE clause, of course. Jul 26, 2014 at 4:48
  • Which DBMS are you using? Looks like it might be SQL Server, but it is always as well to say. Jul 26, 2014 at 4:54
  • Yes, SQL Server is the database that I am using. I do not need an explaination of what this is doing, but help in figuring out how to fix the syntax error. Can someone help with the syntax error?
    – Linda Lu
    Jul 26, 2014 at 15:03

4 Answers 4

4

try this code

select avg(SQLProcessCPU)  as CPU
    from   dbo.TrackCPU
    where  CaptureTime > DATEADD(MINUTE, -5, GETDATE())
0

The (optional) AS keyword is used to assign an alias (ie a temporary "name" for the life of the query) to a selected column or table.

Try this:

select avg(SQLProcessCPU) AS CPU
...

Or, because it's optional, just

select avg(SQLProcessCPU) CPU
...
2
  • It is a syntax problem. That I need a solution to for. For example this query works and returns values, but, when I try to run an individual portion of it as I shared in the beginning of this post, it does not work.
    – Linda Lu
    Jul 26, 2014 at 13:33
  • Example: 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
    – Linda Lu
    Jul 26, 2014 at 13:33
0

'AS' command can be used only in select condition or while considering a query as table, 'AS' command cannot be used in where condition

0

As is used as an alias to the column we are retrieving, so use as after your column name which you want to retrieve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.