Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:47:23+00:00 2026-05-27T17:47:23+00:00

I’ve searched day and night about syntax for subqueries and I still seem to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:47:24+00:00Added an answer on May 27, 2026 at 5:47 pm

    How about:

       SELECT [Customer Name], MAX([Incident #])
       FROM [Incidents] a
       WHERE [Event Code] = 201
       AND  [Customer Name] Not IN 
          (SELECT [Customer Name]
          FROM [Incidents] b
          WHERE b.[Customer Name] = a.[Customer Name]
          AND b.[Incident #] > a.[Incident #]
          AND [Event Code] = 203)
       GROUP BY [Customer Name]
    

    You do not need order by with group by. I am a little suspicious of customer name and wonder about customer id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.