开发者

calculate sum based on column name

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜