Link to home
Start Free TrialLog in
Avatar of n00b0101
n00b0101

asked on

mssql - group by & order by?

I keep getting this error:
Incorrect syntax near the keyword 'ORDER'.

Basically, I'm trying to find all the rows grouped by email that have a count = 4.  I want those 4 to be ordered by the time field so that I can pull the entries in chronological order.  
insert into TableB
( qid, rep, time, email, idmarker )
( 
select TOP 2 qid, rep, time, email, idmarker
FROM TableA
WHERE email IN 
(
select email FROM 
tableA
group by email 
having COUNT(*) = 4
)
ORDER BY time 
)
GO

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

you have a hanging (  The correct is below:

insert into TableB
( qid, rep, time, email, idmarker )
(
 select TOP 2 qid, rep, time, email, idmarker FROM TableA
WHERE email IN
       (select email FROM tableA group by email having COUNT(*) = 4)
)
ORDER BY time
)
GO
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial