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

  • Home
  • SEARCH
  • 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 143047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:02:00+00:00 2026-05-11T08:02:00+00:00

Table layout: CREATE TABLE t_order (id INT, custId INT, order DATE) I’m looking for

  • 0

Table layout:

CREATE TABLE t_order  (id INT, custId INT, order DATE) 

I’m looking for a SQL command to select a maximum of one row per order (the customer who owns the order is identified by a field named custId).

I want to select ONE of the customer’s orders (doesn’t matter which one, say sorted by id) if there is no order date given for any of the rows.

I want to retrieve an empty Resultset for the customerId, if there is already a record with given order date.

Here is an example. Per customer there should be one order at most (one without a date given). Orders that have already a date value should not appear at all.

   +---------------------------------------------------------+   |id    |    custId       |  date                          |   +---------------------------------------------------------+   | 1              10         NULL                          |   | 2              11         2008-11-11                    |   | 3              12         2008-10-23                    |   | 4              11         NULL                          |   | 5              13         NULL                          |   | 6              13         NULL                          |   +---------------------------------------------------------+                            |                            |                            |      Result                          \ |  /                           \  /   +---------------------------------------------------------+   |id    |    custId       |  date                          |   +---------------------------------------------------------+   | 1              10         NULL                          |   |                                                         |   |                                                         |   |                                                         |   | 5              13         NULL                          |   |                                                         |   +---------------------------------------------------------+                                 powered be JavE 

Edit: I’ve choosen glavić’s answer as the correct one, because it provides the correct result with slightly modified data:

   +---------------------------------------------------------+   |id    |    custId       |  date                          |   +---------------------------------------------------------+   | 1              10         NULL                          |   | 2              11         2008-11-11                    |   | 3              12         2008-10-23                    |   | 4              11         NULL                          |   | 5              13         NULL                          |   | 6              13         NULL                          |   | 7              11         NULL                          |   +---------------------------------------------------------+ 

Sfossen’s answer will not work when customers appear more than twice because of its where clause constraint a.id != b.id.

Quassnoi’s answer does not work for me, as I run server version 4.0.24 which yields the following error: alt text http://img25.imageshack.us/img25/8186/picture1vyj.png

  • 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. 2026-05-11T08:02:01+00:00Added an answer on May 11, 2026 at 8:02 am

    Try this:

    SELECT to1.* FROM t_order AS to1 WHERE     to1.date IS NULL AND      to1.custId NOT IN (         SELECT to2.custId         FROM t_order AS to2         WHERE to2.date IS NOT NULL         GROUP BY to2.custId     ) GROUP BY to1.custId 

    For MySQL 4:

    SELECT to1.* FROM t_order AS to1 LEFT JOIN t_order AS to2 ON     to2.custId = to1.custId AND     to2.date IS NOT NULL WHERE     to1.date IS NULL AND      to2.id IS NULL GROUP BY to1.custId 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If its just the CASE statement in LINQ your after… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer I cannot unnderstand but it worked by adding the following… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer You're probably right, but on the other hand if everyone… May 11, 2026 at 7:08 pm

Related Questions

Table layout: CREATE TABLE t_order (id INT, custId INT, order DATE) I'm looking for
I'm in the process of working on a user profile system for a website
I have an HTML table that looks like this: ------------------------------------------------- |Column 1 |Column 2
EDIT : To people building tagging systems. Don't read this. It is not what
I'm having an embedded device which keeps a list of inner tables. I would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.