【发布时间】:2013-12-05 22:08:54
【问题描述】:
我有这段代码,来自Tom Mac,但我想进行一些更改,但我不明白它是如何工作的。
我正在尝试通过添加UNION ALL SELECT date1, date2 FROM person 来同时添加两个日期列,并尝试将其插入现有的选择函数之间。
我将如何将同一张表中的这两列添加到我的结果中?
这是我正在尝试调整的提供的代码:
select person as Names,
sum(splitScore) as Split_Count,
sum(activeScore) as Active_Count
from
(
select Name1 as person,
case when Name2 is null then 1 else 0.5 end as splitScore,
case when Active='True' and Name2 is null then 1 when Active='True' and Name2 is not null then 0.5 else 0 end as activeScore
from person where Name1 is not null
union all
select Name2 as person,
case when Name1 is null then 1 else 0.5 end as splitScore,
case when Active='True' and Name1 is null then 1 when Active='True' and Name1 is not null then 0.5 else 0 end as activeScore
from person where Name2 is not null
) t inner join roster s on s.name = t.log
group by person
;
【问题讨论】:
-
联合中的两个“选择”必须有匹配的列。因此,将这些字段添加到两者以及外部选择中。另外,将它们添加到组中
-
当你想尝试自己做这样的事情时......只需拆开查询。首先使用最嵌套的部分并添加您需要的信息。尝试了解连接和聚合,确保每次将部件添加回或一起添加时都得到正确的结果。
标签: mysql sql select union alias