Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all
I have a problem in my sql query
when I use the
(select MsgTable.ID, MAX(MsgTable.Time) as t from MsgTable group by MsgTable.ID)

I get the correct reply
but when I use
(select MsgTable.ID, MAX(MsgTable.Time) as t from MsgTable group by MsgTable.ID) as G

I get following error
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'as'.

I want to use 'G' in a JOIN statement
please help me to figure out this ,

What I have tried:

(select MsgTable.ID, MAX(MsgTable.Time) as t from MsgTable group by MsgTable.ID) As G
Posted
Updated 25-Aug-18 19:48pm

1 solution

Take out "As" after the inner SELECT:
SQL
SELECT a.X, b.Y from MyTable a
JOIN (SELECT ID, x AS Y FROM MyTable) B
ON a.ID = b.ID
 
Share this answer
 
Comments
saide_a 26-Aug-18 1:58am    
its not work for me
OriginalGriff 26-Aug-18 2:04am    
"its not work for me" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

Show us exactly what you typed!
saide_a 26-Aug-18 2:13am    
what does 'MyTable a' mean? and also B in second line?
I want to get answer from this query
Select D.[ID],D.[UID],D.[Code],D.[Time] From MsgTable AS D
INNER JOIN
(select MsgTable.ID, MAX(MsgTable.Time) from MsgTable group by MsgTable.ID) As G
On D.Time = G.Time AND D.ID = G.ID
Order By D.[ID],D.[Time]
saide_a 26-Aug-18 2:30am    
what I tried in your suggested query:
Select D.[ID],D.[UID],D.[Code],D.[Time] From MsgTable D
INNER JOIN
(select MsgTable.ID, MAX(MsgTable.Time) from MsgTable group by MsgTable.ID) G
On D.Time = G.Time AND D.ID = G.ID
Order By D.[ID],D.[Time]
and the error
No column was specified for column 2 of 'G'.
OriginalGriff 26-Aug-18 3:14am    
Well yes.
What are you trying to do?
Think about your inputs, and how they relate, and exactly what you want as output. You don't have any column "TIME" in your G results, and you don't use anything from your inner select in your result set!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900