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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:17:07+00:00 2026-06-04T03:17:07+00:00

I have a SQL Server 2005 table which lists customers and their order lines

  • 0

I have a SQL Server 2005 table which lists customers and their order lines like this…

Cust  | Code 
Cust1   N001
Cust2   N001
Cust2   N003
Cust2   N004
Cust3   N001
Cust3   N002
Cust3   N003
Cust3   N004

I need the results to list each customer (Once) and whether they have ordered ANY items between N002 and N004. The results would therefore look like this…

Cust  | Ordered?
Cust1   False
Cust2   True
Cust3   True

I have tried various combinations of All / ANY etc. The closest I got was using two queries. In the first I would add the following to get a true/false statement to the end of each line …

SELECT 
    Cust, Code, 
    CASE 
        WHEN Code BETWEEN 'N002' AND 'N004' THEN 'True' 
        ELSE 'False' 
    END AS Expr1 

This gives the following…

Cust  | Code | EXPR1
Cust1   N001   FALSE
Cust2   N001   FALSE
Cust2   N003   TRUE
Cust2   N004   TRUE
Cust3   N001   FALSE
Cust3   N002   TRUE
Cust3   N003   TRUE
Cust3   N004   TRUE

Then using a second query and grouping the results I get

Cust  | Ordered?
Cust1   False
Cust2   False
Cust2   True
Cust3   False
Cust3   True

So could any one help to achieve simply…

Cust  | Ordered?
Cust1   False
Cust2   True
Cust3   True
  • 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-06-04T03:17:08+00:00Added an answer on June 4, 2026 at 3:17 am

    My solution is to add a variable specifying whether the value is in range, and then checking that in the case statement:

    SELECT Cust,
           (case when sum(inrange) > 0 THEN 'True' ELSE 'False' END) AS count
    from (select t.*,
                 (case when code between 'N002' and 'N004' then 1 else 0 end) as inrange
          from t
         ) t
    group by cust
    

    You can actually combine the two case statements into one, if you don’t care about the readability of your code.

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

Sidebar

Related Questions

In SQL Server 2005 I have a table cm_production that lists all the code
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a table in SQL Server 2005 which has a date time field.
I have a sql server 2005 table called ZipCode, which has all the US
SQL Server 2005 I have a table which returns ID name prop value --------------------------
I have a sql server 2005 database which contains a user table.. Is it
in context of SQL Server 2005, I have a table for which the primary
We have a database (SQL Server 2005) which we would like to get under
I have a SQL Server (2005) table which has nearly 100+ million rows in
I'm using SQL SERVER 2005 and have a table which stores challans datewise. It

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.