Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query with Count Embedded

Posted on 2004-10-08
2
Medium Priority
?
618 Views
Last Modified: 2008-02-26
Here's what I want to do spelled out in "English".  I'm having trouble converting this to SQL ...

SELECT USER_ID, DATE FROM CLASSES_TAKEN WHERE USER_ID IN (SELECT USER_ID FROM AWARDS WHERE USER_ID APPEARS ONLY ONCE IN THE AWARDS TABLE)

Obviously "appears only once in the Awards table" isn't valid SQL Syntax, but that's the spelled out version.

Help!
0
Comment
Question by:UPRRDevelopers
2 Comments
 
LVL 2

Accepted Solution

by:
dramacqueen earned 200 total points
ID: 12259335
Hi UPRRDevelopers,

You need a nested sql something like
SELECT USER_ID, DATE FROM CLASSES_TAKEN WHERE USER_ID IN (
    SELECT USER_ID FROM AWARDS WHERE (USER_ID, 1) in (
        select user_id, count(*) from awards
        group by user_id)
    );
NB you can use this for any particular count of records by changing the '1' in the 2nd line
Hope it helps, have a nice weekend.
0
 
LVL 15

Expert Comment

by:andrewst
ID: 12259389
More simply:

SELECT USER_ID, DATE FROM CLASSES_TAKEN WHERE USER_ID IN (
        select user_id
        from awards
        group by user_id
        having count(*) = 1);
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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

877 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