MySQL,有条件地添加列

时间:2014-03-19 20:09:24

标签: mysql sql

我有两个表,第一个包含有关患者的数据,它看起来像这样。

id       patient       sex         impact
------------------------------------------
1        Bill Jones    male        .1     
2        Sarah Smith   female      .4

第二个拥有"乘数"。这些乘数将用于乘以上表中的影响。

id       type        type_value     multiplier
-----------------------------------------------
1        patient     Bill Jones     .5
2        sex         male           .3
3        sex         male           .8
4        sex         female         .7

我正在尝试运行将返回以下内容的查询:

patient        patient_total        sex_total       new_impact
-------------------------------------------------------------------------------
Bill Jones      .5                   1.1             .16
Sarah Smith      0                   .7              .28

每位患者的新影响为(patient_total + sex_total) * impact

以下是创建声明:

--
-- Table structure for table `impact`
--

CREATE TABLE IF NOT EXISTS `impact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `patient` varchar(20) NOT NULL,
  `sex` varchar(7) NOT NULL,
  `impact` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `impact`
--

INSERT INTO `impact` (`id`, `patient`, `sex`, `impact`) VALUES
(1, 'Bill Jones', 'male', 0.1),
(2, 'Sarah Smith', 'female', 0.4);


--
-- Table structure for table `multipliers`
--

CREATE TABLE IF NOT EXISTS `multipliers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(20) NOT NULL,
  `type_value` varchar(60) NOT NULL,
  `multiplier` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;

--
-- Dumping data for table `multipliers`
--

INSERT INTO `multipliers` (`id`, `type`, `type_value`, `multiplier`) VALUES
(1, 'patient', 'Bill Jones', 0.5),
(2, 'sex', 'male', 0.3),
(3, 'sex', 'male', 0.8),
(13, 'sex', 'female', 0.7);

我已经尝试了以下查询的多次迭代,但无法使其工作:/

更新了查询 - 为sex_total列工作,而不是为patient_total列:/

select p.patient, ifnull(sum(ipatient.multiplier), 0) as patient_total, ifnull(sum(isex.multiplier), 0) as sex_total, (ifnull(sum(ipatient.multiplier), 0) + ifnull(sum(isex.multiplier), 0) * p.impact) as new_impact
from impact p 
left outer join multipliers ipatient
on ipatient.type = 'patient' and ipatient.type_value = p.patient 
left outer join multipliers isex
on isex.type = 'sex' and isex.type_value = p.sex 
group by p.patient

有人可以帮忙吗?

由于

2 个答案:

答案 0 :(得分:3)

您只需要加入影响行,然后汇总结果。以下是两个连接,一个用于每种类型的影响:

select p.patient, sum(ipatient.multiplier) as patient_total,
       sum(isex.multiplier) as sex_total,
       (sum(ipatient.multiplier) * sum(isex.multiplier) * p.imact
       ) as new_impact
from impact p left outer join
     multipliers ipatient
     on ipatient.type = 'patient' and ipatient.type_value = p.patient left outer join
     multipliers isex
     on isex.type = 'sex' and isex.type_value = p.sex 
group by p.patient;

如果可能有某些行与患者姓名或性别无匹配,则可能需要coalesce()

编辑:

哑。哑。哑。上述操作失败,因为isex将行相乘,因此它会影响ipatient的sum()。这个版本有效:

select p.patient,
       sum(case when m.type = 'patient' then m.multiplier else 0 end) as patient_total,
       sum(case when m.type = 'sex' then m.multiplier else 0 end) as sex_total,
       (sum(m.multiplier)* p.impact
       ) as new_impact
from impact p left outer join
     multipliers m
     on m.type = 'patient' and m.type_value = p.patient or
        m.type = 'sex' and m.type_value = p.sex 
group by p.patient;

你可以看到它here

如果您不喜欢float中的所有小数位,您可以切换到数字/小数数据类型。

答案 1 :(得分:0)

此查询将为您提供所需的列,但您必须自己进行乘法。

SELECT i.id, i.patient, i.impact,
    (SELECT IFNULL(SUM(multiplier), 0) FROM multipliers 
       WHERE type='patient' AND type_value=i.patient) `patient_total`,
    (SELECT IFNULL(SUM(multiplier), 0) FROM multipliers 
       WHERE type='sex' AND type_value=i.sex) `sex_total`
FROM impact i