Solved

T SQL - Group By/Having Question

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now