Solved

T SQL - Group By/Having Question

Posted on 2007-11-23
2
3,310 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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