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
Solved

Query with Count Embedded

Posted on 2004-10-08
2
613 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting a row into a column 2 60
grant user/role question 11 32
Oracle Query - Convert letters to numbers and display the difference 3 30
Database Design Dilemma 6 58
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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…
Via a live example, show how to take different types of Oracle backups using RMAN.

792 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