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

The Archive Base Latest Questions

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

I have a table in my SQL Server where I stage my datawarehouse extract

  • 0

I have a table in my SQL Server where I “stage” my datawarehouse extract from our ERP system.

From this staging table (table name: DBO.DWUSD_LIVE) , I build my dimensions and load my fact data.

An example DIMENSION table is called “SHIPTO”, this dimensions has the following columns:

"shipto_id
"shipto"
"salpha"
"ssalpha"
"shipto address"
"shipto name"
"shipto city"

Right now I have an SSIS package that does a SELECT DISTINCT across the above columns to retrieve the “unique” data, then through the SSIS package I assign the “shipto_id” surrogate key to.

An example of my current TSQL Query is:

SELECT DISTINCT
"shipto", "salpha", "ssalpha", "shipto address", "shipto name", "shipto city"
FROM DBO.DWUSD_LIVE

This works great but is not “speedy”, some dimensions have 10 columns and doing a distinct select on those is not ideal.

In this dimension, my “Business Key” columns are “SHIPTO”, “SALPHA”, and “SSALPHA”.

So if I do:

SELECT DISTINCT
"shipto", "salpha", "ssalpha"
FROM DBO.DWUSD_LIVE

It yields the same results as:

SELECT DISTINCT
"shipto", "salpha", "ssalpha", "shipto address", "shipto name", "shipto city"
FROM DBO.DWUSD_LIVE

Is there a better way to do this TSQL QUERY? I need all the columns, but only DISTINCT on the business key columns.

Your help is appreciated.

Below is an image of how my project is setup in SSIS, the Dimensions is a SCD 1.

  • 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-12T03:19:17+00:00Added an answer on June 12, 2026 at 3:19 am

    I would start by splitting this into two operations: generating the surrogate key and populating the dimension table. The first step will then be a DISTINCT on only 3 columns, and the second step will become a JOIN. Indexing the columns used in both operations might then give you some improvement.

    You can combine the DISTINCT with NOT EXISTS to avoid processing rows that have already been mapped, something like this:

    insert into dbo.KeyMappingTable (shipto, salpha, ssalpha)
    select distinct shipto, salpha, ssalpha
    from dbo.Source
    where not exists (
        select *
        from dbo.KeyMappingTable
        where shipto = dbo.Source.shipto and salpha = dbo.Source.salpha and ssalpha = dbo.Source.ssalpha
     )
    

    Then you have the mapping, so you can do this:

    insert into dbo.DimShipTo (shipto_id, shipto /*, etc. */)
    select
        m.shipto_id,
        s.shipto -- etc.
    from
        dbo.KeyMappingTable m
        join dbo.Source s
        on m.shipto = s.shipto and m.salpha = s.salpha and m.ssalpha = s.ssalpha
    where
        not exists (
            select *
            from dbo.DimShipTo
            where shipto_id = m.shipto_id
        )
    

    You should also look at MERGE, which is convenient if you’re using a Type 1 dimension and just want to update addresses or other attributes when they change (and it’s a useful command in general). But it’s only available from SQL Server 2008; you didn’t mention what version or edition of SQL Server you’re using.

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

Sidebar

Related Questions

I have a table in SQL Server 2008 Table Name : tbl_device Table Structure:
I have a table in SQL Server. This table has an image field and
I have following table (SQL Server) Table name is LandParcels Blockid ParcelNo Stateorprivate ========================
SQL Server 2005. In our application, we have an entity with a parent table
I have a trigger in a table in SQL Server 2000, this is the
Using SQL Server 2000. I have a table that receives a dump from a
I have a table in SQL Server called [Donations] that contains donations given by
I have a table in SQL Server 2005 which has a date time field.
I have a table in SQL server: Categories -------------- CategoryID (uniqueidentifier) ParentCategoryID (uniqueidentifier) allow
I have a table in SQL Server 2005. alt text http://www.techpint.com/sites/default/files/images/table.JPG I want to

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.