?
Solved

Help with Query of Queries...I think

Posted on 2006-06-19
3
Medium Priority
?
191 Views
Last Modified: 2013-12-24
I have the following two queries.

<!--- Get all sessions from category --->
<CFQUERY NAME="rsCatSessions" DATASOURCE="DB">
      SELECT Quiz, QString
      FROM QCat, Quiz
      WHERE mcat = '#URL.cat#' AND scat = '#URL.scat#' AND QCat.QID = Quiz.Qstring
      ORDER BY Quiz ASC
</CFQUERY>

<!--- Get the passed sessions from the category --->
<CFQUERY NAME="rsTakenPass" DATASOURCE="DB">
      SELECT DISTINCT Scores.QString
        FROM Scores
      WHERE Scores.QString = #rsCatSessions.Qstring# AND username = '#session.username#' AND score >= 90
</CFQUERY>

The first query returns a set of ID's that I match up later with their friendly names. The data may look like:
500
501
502
503

The second query compares what was returned in the first query to the scores table to see what has been passed. The data may look like:
500
501

I need help with a query that will show those items that exist in the first query and NOT in the second. That data would look like:
502
503

Thanks in advance!
0
Comment
Question by:Activar
[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
3 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 16933392
Hi Activar,


Ultimately, I think this depends upon your database, but try ...

<CFQUERY NAME="rsCatCombined" DATASOURCE="DB">
     SELECT
    Quiz,
    QString
     FROM
    QCat,
    Quiz
     WHERE
    mcat = '#URL.cat#' AND
    scat = '#URL.scat#' AND
    QCat.QID = Quiz.Qstring AND
    QCat.QID NOT IN
        (
        SELECT DISTINCT
            Scores.QString
        FROM
            Scores
        WHERE
            Scores.QString = #rsCatSessions.Qstring# AND
            username = '#session.username#' AND
            score >= 90
        )
     ORDER BY
    Quiz ASC
</CFQUERY>

I'm not sure of the ID columns.

The sub-select needs to ONLY return 1 column of IDs to be rejected. Nothing else. The IDs need to be the ones that are like 500 and 501.


Regards,

Richard Quadling.
0
 

Author Comment

by:Activar
ID: 16934374
Richard,

Thanks for the response. While I did not get the results I was looking for from you solution (no fault of your code, just inadequate SQL experience on my end) it did get me looking in the right direction which was just what I needed.

I ended up storing the values of the second query to a list which I qualified the NOT IN statement against. This seems to work like a charm.

<CFQUERY NAME="rsCatCombined" DATASOURCE="DB">
      SELECT Quiz, QString
      FROM QCat, Quiz
      WHERE
      mcat = '#URL.cat#' AND
      scat = '#URL.scat#' AND
        QCat.QID = Quiz.Qstring AND
      QCat.QID NOT IN (#passList#)
      ORDER BY QString ASC
</CFQUERY>

Thanks again
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16934386
If #passList# is generated by a SELECT statement, then put that statement in where you have #passList#.

This is known as a sub-query.

If you don't actually need #passList# iteslf AND the query works, then you've saved yourself a trip to the server.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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