Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T SQL - Group By/Having Question

Posted on 2007-11-23
2
Medium Priority
?
3,332 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

722 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