0

I am getting Incorrect syntax near the keyword 'select' after executing the following code.

declare @c int

SELECT @c = COUNT(*) 
  FROM (select id, max(date_stored)
         from table B
   INNER JOIN table P ON B.id = P.id
        where id = 3)

select @c

I want to select total no of records having max stored dates in database. Can any one plz tell what I am doing wrong

1
  • It defeats the purpose of table aliases if you don't consistently use them. Which table is the id value coming from, because not specifying will result in an "ambiguous column reference" error seeing that the column exists in both tables. Commented Mar 8, 2010 at 0:02

3 Answers 3

1

you need to alias the subquery

declare @c int
SELECT @c = COUNT(*) FROM
(
    select id, max(date_stored)
    from
    table B
    INNER JOIN
    table P
    ON
    B.id = P.id
    where
     id = 3
) x --alias
select @c

I still don't understand why toy are doing the group by and max in the subquery if all you need is a count

Sign up to request clarification or add additional context in comments.

10 Comments

Why would he need to alias the subquery??
@Pointy : Because that is how TSQL works and why he was getting the error.
The From of the first select needs the alias
Well that sounds pretty dubious to me, since the alias is never referenced. However I don't have SQL Server around to play with anymore, and it's been well over a year since I've had to deal with it.
Type a simple nested select with a from and leave off the alias, then put the alias back on. The first select is using the alias
|
0
declare @c int
SELECT @c = COUNT(aName.[ID]) FROM
(
    select [B.id], max(B.date_stored)
    from table B
    INNER JOIN table P ON B.id = P.id
    where B.id = 3
    -- group by [id] ???
) aName

select @c

1 Comment

Solved the problem by just defining max(B.date_stored) as MaxDate
0
SET @C = SELECT COUNT(*) FROM ...

Also, do you need to do a GROUP BY in that inner SELECT? What is the "max()" call supposed to mean? What table is that "date_stored" field in? I think you should get that inner query worked out first, and then move on to getting the counts.

If you're trying to count records where the date value is equal to the newest of all date values for a given "id" value, then you're going to need to make that explicit (unless SQL Server 2005 has some neat new feature I don't know about).

2 Comments

max(date_stored) is being used to only count the latest date_stored for a given id, assuming there is more than one row for each id. date_stored is in table b. also a table alias, when doing a join you must identify which column you are referencing.
@joe yes each id has more columns for date ..so i just want to get the max date column. But it still wont works...After defining an alias it says No column was specified for column 2 of 'aName'

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.