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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:23:49+00:00 2026-05-13T13:23:49+00:00

Is there anyway to link one value to another based on it’s column name?

  • 0

Is there anyway to link one value to another based on it’s column name?
For e.g. in the following non-normalized table, I need to consider the value from the column state_amt_1 from another table if the state_cd_1 is ‘TX’ and add that value to the state_amt_2 if the state_cd_2 is ‘TX’ as well.

create table states (state_id int not null auto_increment, state_cd_1 varchar(100), state_cd_2 varchar(100), state_cd_3 varchar(100), state_cd_4 varchar(100), state_cd_5 varchar(100), primary key (state_id));
insert into states values (NULL, 'TX', NULL, NULL, NULL , 'OR');
insert into states values (NULL, 'OR', 'UT', NULL, NULL , NULL);
insert into states values (NULL, 'AZ', 'OR', 'UT', NULL , 'AZ');

create table amount (amt_id int not null auto_increment, state_amt_1 varchar(100), state_amt_2 varchar(100), state_amt_3 varchar(100), state_amt_4 varchar(100), state_amt_5 varchar(100), primary key (amt_id));
insert into amount  values (NULL, '100', 900, NULL, NULL , '200');
insert into amount values (NULL, '250', '300', NULL, NULL , NULL);
insert into amount values (NULL, '400', '290', '350', NULL , '450');

(11:34) mysql>select * from states;
+----------+------------+------------+------------+------------+------------+
| state_id | state_cd_1 | state_cd_2 | state_cd_3 | state_cd_4 | state_cd_5 |
+----------+------------+------------+------------+------------+------------+
|        1 | TX         | NULL       | NULL       | NULL       | OR         | 
|        2 | OR         | UT         | NULL       | NULL       | NULL       | 
|        3 | AZ         | OR         | UT         | NULL       | AZ         | 
+----------+------------+------------+------------+------------+------------+

(11:34) mysql>select * from amount;
+--------+-------------+-------------+-------------+-------------+-------------+
| amt_id | state_amt_1 | state_amt_2 | state_amt_3 | state_amt_4 | state_amt_5 |
+--------+-------------+-------------+-------------+-------------+-------------+
|      1 | 100         | 900         | NULL        | NULL        | 200         | 
|      2 | 250         | 300         | NULL        | NULL        | NULL        | 
|      3 | 400         | 290         | 350         | NULL        | 450         | 
+--------+-------------+-------------+-------------+-------------+-------------+

Expected result:

TX 100
OR 740
UT 650
AZ 850

OR

TX OR UT AZ
100 740 650 850

How to calculate:

TX: 100
OR: 200+250+290=740
UT: 300+350=650
AZ: 400+450=850

Note:
The names of states may change.

  • 1 1 Answer
  • 1 View
  • 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-13T13:23:49+00:00Added an answer on May 13, 2026 at 1:23 pm

    It’s ugly, but you can do it with unions like this:

    SELECT st, sum(amt) FROM (
    (
     SELECT s.state_cd_1 AS st, a.state_amt_1 AS amt
     FROM states AS s JOIN amount AS a ON s.state_id = a.amt_id
     WHERE s.state_cd_1 IS NOT NULL AND a.state_amt_1 IS NOT NULL
    ) UNION ALL (
     SELECT s.state_cd_2 AS st, a.state_amt_2 AS amt
     FROM states AS s JOIN amount AS a ON s.state_id = a.amt_id
     WHERE s.state_cd_2 IS NOT NULL AND a.state_amt_2 IS NOT NULL
    ) UNION ALL (
     SELECT s.state_cd_3 AS st, a.state_amt_3 AS amt
     FROM states AS s JOIN amount AS a ON s.state_id = a.amt_id
     WHERE s.state_cd_3 IS NOT NULL AND a.state_amt_3 IS NOT NULL
    ) UNION ALL (
     SELECT s.state_cd_4 AS st, a.state_amt_4 AS amt
     FROM states AS s JOIN amount AS a ON s.state_id = a.amt_id 
     WHERE s.state_cd_4 IS NOT NULL AND a.state_amt_4 IS NOT NULL
    ) UNION ALL (
     SELECT s.state_cd_5 AS st, a.state_amt_5 AS amt 
     FROM states AS s JOIN amount AS a ON s.state_id = a.amt_id 
     WHERE s.state_cd_5 IS NOT NULL AND a.state_amt_5 IS NOT NULL
    )) AS joined GROUP BY st;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway to specify more than one MergeSection value for the MSBuild LINK
Is there anyway to open the twitter app from an html link on an
Is there anyways to add tooltips to a non hyper link? ie . I
Is there any way to link batch script (.bat files) so that they run
Is there any way to write a valid header/link/image? Something like <a href=index.html><h1 id=logo>Company
JSFiddle Is there any way to make something like the X on that link
I have a link as bellow. Is there any way to change info@example.co.uk with
Is there anyway or any addin for VS2010 that can remove all the comments
Is there anyway to highlight the initial text in a textbox on a web
Is there anyway to get the Facebook ID of a user without forcing them

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.