views:

51

answers:

3

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

A: 
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).

Pointy
Did you try your code? (This may be a rhetorical question.)
Aaron Bertrand
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 Pitz
@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'
sam
+1  A: 

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

SQLMenace
Why would he need to alias the subquery??
Pointy
@Pointy : Because that is how TSQL works and why he was getting the error.
Hogan
The From of the first select needs the alias
Joe Pitz
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.
Pointy
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
Joe Pitz
Also I agree with @SQLMenace - given that there'll be a max date for each "id" value, why not just count the distinct "id" values?
Pointy
@Pointy: SQL Server and MySQL both require you to define an alias for a derived table, regardless if they are used or not.
OMG Ponies
OK then everybody, I concede :-)
Pointy
Still not working. I have a history table that saves in xml format. All I want is to first get the total count of all records with maximum date storage history.
sam
after defining an alias it says No column was specified for column 2 of 'a'.
sam
A: 
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
Hogan
Solved the problem by just defining max(B.date_stored) as MaxDate
sam