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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:47:46+00:00 2026-05-11T10:47:46+00:00

I was given a database design which stores information about an organization and any

  • 0

I was given a database design which stores information about an organization and any changes that have happened or will happen to the organizations. Since pretty much anything can change about an organization, there is one table that only contains the unique OrganizationID’s in a table called ‘Organizations’. The changes to that organization all have an effective date and follow a similar design pattern such as the following, the location change:

Table: organization_locations  organization_id (int, not null) - Relates back to the Organizations.ID column. location_id (int, not null) - Relates to Locations.ID eff_date (datetime, not null) - The date this change becomes effective  Table: Locations  ID (int, pk, identity, not null) - ID of the location Name (varchar(255), not null) - Name of the location ... Other miscellaneous columns that aren't important for this discussion ... 

E.G. Organizations may just contain two rows which would simply hold the id’s 1 and 2 respectively. Locations may have 3 locations (id, name):

1, Location1 2, Location2 3, Location3  organization_locations (organization_id, location_id, eff_date):  1, 1, 1/1/2000  <--- Organization 1 is starting at location 1 1, 2, 1/1/2010  <--- On 1/1/2010, organization 1 moves to location 2 (from location 1) 1, 3, 1/1/2011 <--- On 1/1/2011, organization 1 moves to location 3 (in this case from location 2) 

I already have a large and possibly overly complex query for specifying a date and getting back an organizations status at that given time but I feel like there’s probably an easier way. However, the problem at hand is the following:

From this schema, how can I answer a question such as ‘What organizations will move from Location 1 to another location and what organizations will move to Location 1 from another location in the given timeframe: date1 to date2?’

A similar question that could also answer the first is: How can I query each organization’s location changes (easy) while showing the PREVIOUS location they are moving from (hard?) ?

Note: Including the LINQ tag in case there’s an easy way to do it in LINQ I can go that route.

  • 1 1 Answer
  • 3 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-11T10:47:46+00:00Added an answer on May 11, 2026 at 10:47 am

    To Location 1:

    SELECT  DISTINCT organization_id FROM    organization_locations ol WHERE   ol.eff_date BETWEEN @date1 AND @date2         AND ol.location = 1 

    From Location 1:

    SELECT  DISTINCT organization_id FROM    (         SELECT organization_id,                (                SELECT  TOP 1 location_id                FROM    organization_locations oln                WHERE   oln.organization_id = ol.organization_id                        AND oln.eff_date < ol.eff_date                ORDER BY                        organization_id DESC, eff_date DESC                ) AS previous_location         FROM   organization_locations ol         WHERE  eff_date BETWEEN @date1 AND @date2         ) olo WHERE   previous_location = 1 

    Show previous location:

    SELECT ol.*,        (        SELECT  TOP 1 location_id        FROM    organization_locations oln        WHERE   oln.organization_id = ol.organization_id                AND oln.eff_date < ol.eff_date                AND location_id = 1        ORDER BY                organization_id DESC, eff_date DESC        ) AS previous_location FROM   organization_locations ol 

    Having a UNIQUE INDEX on (organization_id, eff_date) will help you a lot.

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

Sidebar

Related Questions

I have been given the task to design a database to store a lot
I have a legacy database with a pretty evil design that I need to
I have a simple application which stores the data into the sql server database
Given a database with two tables X and Y , I have a query
Given a database that has numerous repeating columns used for auditing and versioning, what
I was given a MySQL database file that I need to restore as a
I'm building a Wine review site and have run into a database design problem
I have an access database given to me where all the dates are stored
I have a design question. I have a nested repeater structure that is 4
I know that key-value pairs aren't good database design, aren't normalized etc however in

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.