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

The Archive Base Latest Questions

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

I have two tables A and B; Table A: Cust_ID, Col_A, Col_B 1001, 1,

  • 0

I have two tables A and B;

Table A:
Cust_ID, Col_A, Col_B
1001, 1, 2
1002, 3, 4
1003, 2, 1
1004, 4, 3

Table B:
Col_A_Weight, Col_B_Weight
0.1111111, 0.22222

What i’m trying to do is create a new Table C using T-SQL, where

Output = Col_A*Col_A_Weight + Col_B*Col_B_Weight

Table C:

Cust_ID, Output
1001, 1*0.1111111 + 2*0.22222 
1002, 3*0.1111111 + 4*0.22222
1003, 2*0.1111111 + 1*0.22222
1004, 4*0.1111111 + 3*0.22222

So that final Table C is

Cust_ID Output
1001, 0.5555511
1002, 1.2222133
1003, 0.4444422
1004, 1.1111044

Given that there are 200 Col_A, Col_B, Col_C, Col_D etc and corresponding weight columns, how to use a clever join so that the columns are referenced dynamically (given that they correspond across both tables). Since Table B has no Cust_ID field, this makes it a little more trickier.

How can this be done?

  • 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-23T11:31:20+00:00Added an answer on May 23, 2026 at 11:31 am
    USE tempdb;
    GO
    
    CREATE TABLE dbo.A
    (
        Cust_ID INT PRIMARY KEY,
        Col_A INT,
        Col_B INT,
        Col_C INT,
        Col_D INT,
        Col_E INT
    );
    
    INSERT dbo.A
        SELECT 1001, 1, 2, 2, 2, 2
        UNION SELECT 1002, 3, 4, 3, 3, 3
        UNION SELECT 1003, 2, 1, 5, 2, 1
        UNION SELECT 1004, 4, 3, 2, 3, 4;
    
    CREATE TABLE dbo.B
    (
        Col_A_Weight DECIMAL(10,9),
        Col_B_Weight DECIMAL(10,9),
        Col_C_Weight DECIMAL(10,9),
        Col_D_Weight DECIMAL(10,9),
        Col_E_Weight DECIMAL(10,9)
    );
    
    INSERT dbo.B
        SELECT 0.1111111, 0.22222, 0.3333, 0.444, 0.55;
    
    DECLARE @sql NVARCHAR(MAX) = N'IF OBJECT_ID(''dbo.C'') IS NOT NULL
    BEGIN
        DROP TABLE dbo.C;
    END
    SELECT A.Cust_ID, Output = ';
    
    SELECT @sql += 'B.' + name + '*' 
        + REPLACE(name, '_Weight', '') + ' + '
    FROM sys.columns 
    WHERE [object_id] = OBJECT_ID('dbo.B');
    
    SELECT @sql = LEFT(@sql, LEN(@sql)-2)
        + ' INTO dbo.C
            FROM dbo.A AS A
            CROSS JOIN dbo.B AS B;';
    
    EXEC sp_executeSQL @sql;
    
    SELECT * FROM dbo.C;
    
    GO
    DROP TABLE dbo.A, dbo.B;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Table public.tags_to_entities Column | Type | Modifiers --------+---------+----------- tid |
I have two tables: Table items: | id | name | description | |
I have two tables: table A id | level_ID | col_m | col_n table
I have two tables (Table A, Table B) joined with a join table (TableAB)
I have two tables Table A type_uid, allowed_type_uid 9,1 9,2 9,4 1,1 1,2 24,1
Assume I have two tables: Table MY_ENTITY ID: PK OTHER_ID: FK to table OTHER
I have two tables : one table it matchs and the other is teams.
I have two tables: a schedule table that contains information about how an employee
I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I have two tables Table FOO FooUniqueID| Year| Name| Worth| --------------------------- 1 2008 Bob

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.