SQL - Distinct plus more


 
Thread Tools Search this Thread
Top Forums Web Development SQL - Distinct plus more
# 1  
Old 01-03-2011
SQL - Distinct plus more

Hi all,

I have an interesting and I am sure simple question for yau'll.

Basically this is what I am after:

The table:
CREATE TABLE places (id INT, city VARCHAR(24), name VARCHAR(24));

The data:
id = 1, city = canberra, name = aaron
id = 2, city = canberra, name = andrew
id = 3, city = canberra, name = john
id = 4, city = perth, name = grahame
id = 5, city = perth, name = alex
id = 6, city = perth, name = bob

The result:
canberra

Why: ?!?!?
I want to return the distinct values of "city", but, not if there is a person in that city named "bob".

Help would be appreciated.

Thanks,
# 2  
Old 01-03-2011
What query have you tried?
# 3  
Old 01-03-2011
Solved:

SELECT DISTINCT(x.city) FROM places x WHERE NOT EXISTS (SELECT 1 FROM places y WHERE y.name = 'bob' AND x.city = y.city);
# 4  
Old 01-03-2011
Out of curiosity, what would be the results of:
Code:
SELECT DISTINCT(city) FROM places WHERE name <> "bob"

Does "perth" show up?
# 5  
Old 01-03-2011
Those are the lines I was originally thinking along... but,

That will simply display the distinct city values, and only not show ones where "bob" is the only name associated.

I have now verified this with a test DB.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need the distinct of these lines

Red Hat Enterprise Linux 5.4 I have a text file (error log file) , which has occurences of an error message like ORA-01652: unable to extend temp segment by 8 in tablespace xxxxxThere are around 3000 error messages like this in the error log file. But there are only 7 or 8 distinct... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

Find distinct values

Hi, I have two files of the following format file1 chr1:345-456 chr2:123-456 chr2:455-678 chr3:456-789 chr3:444-555 file2 chr1:345-456 chr2:123-456 chr3:456-789 output (2 Replies)
Discussion started by: jacobs.smith
2 Replies

3. Shell Programming and Scripting

Get the distinct of a particular field

From the below ps output , I want the distinct values of the third field (ie. I need the distinct PPIDs) $ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Nov 10 - 48:49 /etc/init root 1769576 1 0 Nov 10 - 0:07... (1 Reply)
Discussion started by: polavan
1 Replies

4. UNIX for Dummies Questions & Answers

Grep distinct

Hello, I have a file which looks like: %_SPE_RDP_NUM_ECH(7)*************% %_SPE_RDP_NUM_ECH(70)************% %_SPE_RDP_NUM_ECH(71)************% %_SPE_RDP_NUM_ECH(72)************% %_SPE_RDP_NUM_ECH(8)*************% %_SPE_RDP_NUM_ECH(9)*************% %_SPE_FLUXPREV_PRES1_MT_HT(2)****%... (5 Replies)
Discussion started by: mvalonso
5 Replies

5. Shell Programming and Scripting

Search distinct files

Hello , Can anyone help me with my below query I am trying to find a text in directory of files via below command grep -i cmps_cgs_crs_rfnc_id * But it returns multiple times same file name i.e if the text found in a file 4 times the file name shown 4 times in the o/p Is... (1 Reply)
Discussion started by: Pratik4891
1 Replies

6. Shell Programming and Scripting

distinct values of all the fields

I am a beginner to scripting, please help me in this regard. How do I create a script that provides a count of distinct values of all the fields in the pipe delimited file ? I have 20 different files with multiple columns in each file. I needed to write a generic script where I give the number... (2 Replies)
Discussion started by: vukkusila
2 Replies

7. UNIX for Dummies Questions & Answers

distinct values of all the fields

I am a beginner to scripting, please help me in this regard. How do I create a script that provides a count of distinct values of all the fields in the pipe delimited file ? I have 20 different files with multiple columns in each file. I needed to write a generic script where I give the number... (1 Reply)
Discussion started by: vukkusila
1 Replies

8. Shell Programming and Scripting

grep distinct values

this is a little more complex than that. I have a text file and I need to find all the distinct words that appear in a line after the word TABLESPACE when I grep for just the word tablespace, I get: how do i parse this a little better so i have a smaller file to read? This is just an... (4 Replies)
Discussion started by: guessingo
4 Replies

9. UNIX for Dummies Questions & Answers

how to pick distinct records..........

How can i pick distinct records which consists of duplicate data from a ASCII file using UNIX commands (3 Replies)
Discussion started by: ss4u
3 Replies
Login or Register to Ask a Question