title: SQL 2005 linked Access 2003 DB advanced subquery using PHP
body: <p>I've searched day and night about syntax for subqueries and I still seem to be in the dark about them. On top of that, it seems that I may need more of an advanced subquery to complete this. I will try and explain this to the best of my ability.</p>
  
  <p>We use an Access DB for our ticketing system (I know) and it was built back in 1997. It is using SQL Server 2005 for the backend. I'm currently writing a script in PHP to email customers when their block time, they have bought in advance, has dropped below zero. With the little coding experience I have with SQL and ODBC, I was able to create a couple queries that work for now. I need to add more data to these queries but I'm pretty much at my limit.</p>
  
  <p>This query will pull all distinct customers that have prepaid time with us (Block Time/Event code 201)</p>
  
  <pre><code>$query1 = "SELECT [Customer Name], MAX([Incident #])
             FROM [Incidents]
             WHERE [Event Code] = 201
             GROUP BY [Customer Name]
             ORDER BY [Customer Name] ASC";
  </code></pre>
  
  <p>Because I have limited knowledge on sub queries, I have another query that has to find if there is a newer 'Incident #' that has an 'Event Code' of 203. If so, do not include them.</p>
  
  <pre><code>while( odbc_fetch_row( $query1 ))
  {
     $query2 = "SELECT [Customer Name]
                FROM [Incidents]
                WHERE [Customer Name] = '$customer_name'
                AND [Incident #] &gt; $incident_num
                AND [Event Code] = 203";
  
     if( !odbc_fetch_row( $query2 )) {
        //list customers
     }
  }
  </code></pre>
  
  <p>These queries work like a charm but, I would like to merge them into one and also include the columns 'Start Date' and 'Time Used' in the outer query so that I can access that data as well.</p>