我使用以下查询将逐年工资分组到一个合同记录中。
SELECT playerid,contractid,term,min(season) as yearone,exp_year,notes
FROM salaries
GROUP BY playerid, contractid
HAVING yearone <= 2014 AND exp_year > 2014
如您所见,我将所有记录分组在playerid和contractid上。分组结果中的所有字段都具有相同的数据,但“notes”除外,它可以包含注释或为NULL。
我想要实现的是以下两个选项之一:
OR
我不确定这是可能的,但我会喜欢一些输入/指导。
答案 0 :(得分:1)
要检查是否有任何记录包含注释,您可以编写一个case
,根据是否有注释,返回1或0。如果至少有一个音符,则返回该值的最大值。您也可以将其相加以获得音符数。
SELECT
playerid,
contractid,
term,
min(season) as yearone,
exp_year,
notes,
max(case when notes is not null then 1 else 0 end) as HASNOTES,
sum(case when notes is not null then 1 else 0 end) as NUMBEROFNOTES
FROM salaries
GROUP BY playerid, contractid
HAVING yearone <= 2014 AND exp_year > 2014
显然你可以将该值移动到HAVING
子句,导致查询不返回那些带注释的记录:
HAVING
max(case when notes is not null then 1 else 0 end) = 0