Solved

T SQL - Group By/Having Question

Posted on 2007-11-23
2
3,315 Views
Last Modified: 2008-10-03
If i run the code below then i'll get the error returned - can anyone suggest the easiest way around this...

Thanks


Msg 116, Level 16, State 1, Line 4
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Select * from 
[users]
where email in 
(
	SELECT email, COUNT(email) AS NumOccurrences, companyid
	FROM [users]
	GROUP BY email, companyid
	HAVING (COUNT(email) > 1 )     
)

Open in new window

0
Comment
Question by:paulCardiff
[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 23

Accepted Solution

by:
Ashish Patel earned 500 total points
ID: 20338152
Try this.

Select * from
[users]
where email in
(
      SELECT email  FROM [users]
      GROUP BY email, companyid
      HAVING (COUNT(email) > 1 )    
)
0
 

Expert Comment

by:jourik
ID: 22631746
For clarification:

When stating a subquery to define a column criteria, it has to output one column only.
Otherwise, the parent query won't know what column to pick.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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