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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:35:21+00:00 2026-06-15T08:35:21+00:00

Hi am trying to import data into my data warehouse and to simplify importing

  • 0

Hi am trying to import data into my data warehouse and to simplify importing some data I want to make a new column in a table that will keep track of the most current value as I get new data.

My tables look like the following:

CREATE TABLE CustomerFact(
 Id INT IDENTITY PRIMARY KEY
,CustomerId NVARCHAR(20) NOT NULL --Current Id of a Customer
);

--Track changes to CustomerId
CREATE TABLE CustomerHistoryFact(
 Id INT IDENTITY PRIMARY KEY
,CurrentCustomerId NVARCHAR(20) --Most recent Customer Id
,CustomerId NVARCHAR(20) NOT NULL --Customer Id at time of record insert
,PrvsCustomerId NVARCHAR(20) NOT NULL --Customer Id at time of transaction
,PrvsCustomerIdEffectiveDate DATE NOT NULL --Date when PrvsCustomerId was effective
,PrvsCustomerIdObsoleteDate DATE NOT NULL --When the PrvsCustomerId was expired and replaced with a new Customer Id
);

The CustomerId, PrvsCustomerId, PrvsCustomerIdEffectiveDate, and PrvsCustomerIdObsoleteDate are all external. I want to assign a CurrentCustomerId whenever a new record is inserted.

This is determined by when a new CustomerHistoryFact is inserted, check the PrvsCustomerId with the CurrentCustomerId of all other records. If there is a match then the CustomerId of the new record becomes the CurrentCustomerId of all previous records or else the record is inserted and the CustomerId becomes the CurrentCustomerId.

  • 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-15T08:35:22+00:00Added an answer on June 15, 2026 at 8:35 am

    Seems like a twisted database design. Why can’t you just keep on inserting CustomerId in CustomerHistoryFact table, everytime the CustomerFact record is updated? You can write a trigger on CustomerFact table for insert/update, and make an entry into CustomerHistoryFact table, if the latest CustomerId sorted by CustomerIdEffectiveDate desc is different from the customerId in Inserted/Deleted table. That way, you have a history of CustomerId’s for starting from current CustomerId to back to in time.

    CREATE TABLE CustomerFact(
     Id INT IDENTITY PRIMARY KEY
    ,CustomerId NVARCHAR(20) NOT NULL --Current Id of a Customer
    );
    
    --Track changes to CustomerId
    CREATE TABLE CustomerHistoryFact(
     Id INT IDENTITY PRIMARY KEY
    ,CustomerId NVARCHAR(20) --Most recent Customer Id
    ,PrevCustomerId NVARCHAR(20) NOT NULL --Customer Id at time of record insert
    ,UpdatedDate DATETIME NOT NULL --Date when PrvsCustomerId was effective
    );
    
    CREATE TRIGGER dbo.CustomerFact_IU
    ON dbo.CustomerFact
    AFTER INSERT, UPDATE
    AS 
    BEGIN
        SET NOCOUNT ON;
    
        --
        -- Check if this is an INSERT, UPDATE.
        -- 
        DECLARE @action as char(1);
        DECLARE @oldCustomerId NVARCHAR(20);
    
        SET @action = 'I'; -- Set Action to Insert by default.
        IF EXISTS(SELECT * FROM DELETED)
        SELECT @oldCustomerId = customerId from DELETED
        BEGIN
            SET @action = 
                CASE
                    WHEN EXISTS(SELECT * FROM INSERTED) THEN 'U' -- Set Action to Updated.
                    ELSE 'D' -- Set Action to Deleted.       
                END
        END
        ELSE 
            IF NOT EXISTS(SELECT * FROM INSERTED) RETURN; -- Nothing updated or inserted.
    
        IF @action = 'I' OR @action = 'U'
        BEGIN
             INSERT INTO dbo.CustomerHistoryFact
             SELECT CustomerId, @oldCustomerId, GETUTCDATE() FROM INSERTED 
        END 
    
        END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to import some Shapefile mapping data into Sql2008. Before I do that,
I am trying to import data into a new database, but some of the
Im trying push some data into a CRM system via an XML import. I
I am trying to import an excel file into a data table using GemBox
So I'm trying to import some sales data into my MySQL database. The data
I am trying to import data from a column in a .xslx file into
I'm trying to import some data into my database. So I've created a temporary
I'm trying to import some data from excel into a database . i got
I'm trying to import some data into R and not having much luck grouping
I'm trying to import about 10K rows of UTF-8 encoded data into a new

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.