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.
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.
This query will pull all distinct customers that have prepaid time with us (Block Time/Event code 201)
$query1 = "SELECT [Customer Name], MAX([Incident #])
FROM [Incidents]
WHERE [Event Code] = 201
GROUP BY [Customer Name]
ORDER BY [Customer Name] ASC";
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.
while( odbc_fetch_row( $query1 ))
{
$query2 = "SELECT [Customer Name]
FROM [Incidents]
WHERE [Customer Name] = '$customer_name'
AND [Incident #] > $incident_num
AND [Event Code] = 203";
if( !odbc_fetch_row( $query2 )) {
//list customers
}
}
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.
How about:
You do not need order by with group by. I am a little suspicious of customer name and wonder about customer id.