Solved

Query with Count Embedded

Posted on 2004-10-08
2
609 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 50 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

707 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

16 Experts available now in Live!

Get 1:1 Help Now