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 6694429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:06:02+00:00 2026-05-26T06:06:02+00:00

Below is some SQL to pull a list of clients. As you can see

  • 0

Below is some SQL to pull a list of clients. As you can see I added the lines annotated by “– ADDED”–a new field, a join and an order by. I’m trying to display one client per row in my result set, but for each client, I want to display the most recent order. What’s the best way to do this? Should I put a where in my JOIN or use the TOP 1? I suppose there are a few ways, but I want to make it as efficient as possible.

SELECT 
    CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
    CONVERT(VARCHAR,client.name) As [Client Name], 
    client.clientid As [Id],
    CONVERT(CHAR(10),[ecom_order].[order_date],101)  -- ADDED
FROM  
    client 
    JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
    JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
    -- keep left outer join since some clients won't have any orders, thus no last order
    LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid -- ADDED
WHERE  
    client.name LIKE '%_%' 
    AND 
    client.name <= (
        SELECT MAX(maxsubquery.ordercolumn) FROM (
            SELECT 
                TOP 10 client.name AS ordercolumn 
            FROM  
                client 
                JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
            WHERE  
                client.name LIKE '%_%' 
            ORDER BY client.name
        ) 
        AS maxsubquery)
 ORDER BY 
    client.name,
    [ecom_order].order_date DESC -- ADDED

Test Data Result Set Example:

enter image description here

  • 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-26T06:06:03+00:00Added an answer on May 26, 2026 at 6:06 am

    Try this

       SELECT 
            CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
            CONVERT(VARCHAR,client.name) As [Client Name], 
            client.clientid As [Id],
          -- Change to get latest
            CONVERT(CHAR(10),Max([ecom_order].[order_date]),101)       
         FROM  client 
            JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
            JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
            -- keep left outer join since some clients won't have any orders, 
         -- thus no last order
            LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid 
        WHERE  
            client.name LIKE '%_%' 
            AND 
            client.name <= (
                SELECT MAX(maxsubquery.ordercolumn) FROM (
                    SELECT 
                        TOP 10 client.name AS ordercolumn 
                    FROM  
                        client 
                        JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                        JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
                    WHERE  
                        client.name LIKE '%_%' 
                    ORDER BY client.name
                ) 
                AS maxsubquery)
         GROUP BY client.identifyingnumber,client.name,client.client_id
         ORDER BY 
            client.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below is some code I am using to translate a map array into SQL
I am having some problems turning the SQL below into a Zend Db query.
I just found some sql query build like this in my project: return (new
I am generating the sql statement below based on some coldfusion logic, but it
So lets say I have some code like below to pull data from another
Below is some html I found in this jquery tooltip tutorial, the contents inside
Below is some code I've written that is effective, but makes too many database
Below is some sample code. The window does not resize (python3.2, on a Mac,
I've been using the idiom below for some time now. And it seems to
When I use a conditional statement targeting IE6 and below with some PHP code

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.