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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:31:58+00:00 2026-05-23T09:31:58+00:00

I have a data table where there’s a list of columns (boiled down to

  • 0

I have a data table where there’s a list of columns (boiled down to the pertinent ones for this example):

users(
  usr_pkey int identity(1, 1) primary key,
  usr_name nvarchar(64),
  ...,
)

accounts(
  acc_pkey int identity(1, 1) primary key,
  usr_key int foreign_key references users(usr_pkey),
  acc_effective datetime,
  acc_expires datetime,
  acc_active bit,
  ...,
)

From this table I’m looking to grab all records where:

  • The account belongs to the specified user and
  • In the first instance:
    • the account is active and today’s date falls between the account’s effective and expiry date or
  • In the second instance:
    • if no records were identified by the first instance, the record with the most recent expiry date.

So – if an active record exists where today’s date falls between the account’s effective and expiry dates, I want that record. Only if no match was found do I want any account for this user having the most recent expiry date.

  • 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-23T09:31:59+00:00Added an answer on May 23, 2026 at 9:31 am

    Here’s one solution I’ve found:

    select top 1 *
    from accounts
    where usr_key = @specified_user
    order by
      acc_active desc,
      case 
        when getdate() between acc_effective and acc_expires then 0
        else 1
      end,
      acc_expires desc
    

    This would effectively order the records in the right priority sequence allowing me to pick the top one off the list

    Strictly speaking, it doesn’t achieve exclusive or, but it could be applied to this data set to achieve the same end.

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

Sidebar

Related Questions

I have Access table with fields, in which there is some data and path
I have a table in my data base that has three columns: Screen, Icon,
I have several data s in MySQL database. In my table there is a
I have a data table 44 columns wide that I need to write to
If i have data in my MySQL like this table: data(TEXT) foo (hal) foo
I have a scenario where I get a data table with 65 columns and
I have a data.table DT with a column named RF and many columns with
I have a data table with columns a, b, c. a and b are
If I have a data table like this id, Name, Address, Status How would
i have a problem i have some data in my data table there are

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.