Solved

Query with Count Embedded

Posted on 2004-10-08
2
615 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
[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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

710 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