Solved

mssql - group by & order by?

Posted on 2009-07-01
2
252 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:n00b0101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 24755794
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
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24755807
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

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question