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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:16:59+00:00 2026-06-12T15:16:59+00:00

I need help with a SQL script. I have an orders table with a

  • 0

I need help with a SQL script.

I have an orders table with a card_number column and I have another table called card_range which stores card_number ranges per client with a column to flag whether it’s active or not active.

When I select data from the orders table I would like to exclude all card_number ranges which are inactive

Eg. orders table

order_id   card_number
1          101
2          102
3          201
4          301

card_range table, active 0 = no, 1 = yes

start_card_number end_card_number active
101               199             0
201               299             1
301               399             0

So the only data I want to return from the orders table is

order_id card_number
3        201

The script that I’m trying to figure out is the looping thru the card_range table…thanks

  • 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-12T15:17:01+00:00Added an answer on June 12, 2026 at 3:17 pm

    Assuming that card ranges don’t overlap:

    declare @orders as table ( order_id int, card_number int )
    insert into @orders ( order_id, card_number ) values
      ( 1, 101 ), ( 2, 102 ), ( 3, 201 ), ( 4, 301 )
    
    declare @card_range as table ( start_card_number int, end_card_number int, active bit )
    insert into @card_range ( start_card_number, end_card_number, active ) values
      ( 101, 199, 0 ), ( 201, 299, 1 ), ( 301, 399, 0 )
    
    select order_id, card_number
      from @orders as o inner join
        @card_range as cr on
          cr.start_card_number <= o.card_number and o.card_number <= cr.end_card_number and
          cr.active = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help/guide for sql select query, I have 2 table stock and stock_history, in
I have a sql script to create a new database which i need to
I need help with the SQL ORDER BY clause. In my table below I
I have an interesting SQL problem that I need help with. Here is the
I'm not good at t-sql, so I need help. I have this code I
I have to add many registries to a table and I need help since
I have a script which handles the naming of parent/child elements on another page.
I need help on creating updating SQL script TableA colA colB colC colD colE
I have a problem with my script SQL , please help me. Ex: I
I am using SQL Server 2005. I have a table called Product. Now I

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.