If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > data based on unique values in another column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-09, 14:42
joe robles joe robles is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
Red face data based on unique values in another column

Hi all,

A table has two columns - Dept_no. and Emp_count. The Dept_no values are unique but not the Emp_count values. I have a requirement to retireve the unique Dept_nos whose Emp_counts are unique. I tried DISTINCT but in vain.

Is there a way to accomplish this?

Exiting data:

Code:
Dept_no     Emp_count
1                10
2                20
3                30
4                30
5                30
6                40

Expected result:

Code:
Dept_no
1
2
3
6
Any help is highly appreciated.

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 07-06-09, 14:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Quote:
Originally Posted by joe robles
I have a requirement to retireve the unique Dept_nos whose Emp_counts are unique.
"stupidest requirement of the day" award, but here you go anyway...
Code:
SELECT MIN(Dept_no) AS Dept_no
     , Emp_count
  FROM daTable
GROUP
    BY Emp_count
HAVING COUNT(*) = 1
by the way, in your sample data, only departments 1, 2, and 6 should be returned

dept 3 should ~not~ be returned, because 30 is ~not~ unique in the Emp_count column
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 07-06-09 at 15:00.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On