在我的sql server代码中,我有这个select语句
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name
from NewHire a
join Position b on a.Position_ID = b.Position_ID
join WorkPeriod c on a.hireID = c.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
我想为它添加一个新列。但是,此列不是表中的列,它仅用于存储我从现有列进行的计算中的float
。
我得到的数字是这样计算的:
@acc
是上述选择语句中的a.HireID
。
CAST((select COUNT(*) from Hire_Response WHERE HireID = @acc AND (HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select COUNT(*) from Hire_Response WHERE HireID = @acc) as FLOAT)
我该怎么做? 感谢。
答案 0 :(得分:1)
您可以在other question上使用GBN为您执行的代码作为子查询
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
d.percentage
from NewHire a
inner join Position b on a.Position_ID = b.Position_ID
inner join WorkPeriod c on a.hireID = c.HireID
left join (select NH.HireID, ISNULL(1E0 * COUNT(CASE WHEN HireResponse IN (0,1) THEN HR.HireID END) / NULLIF(COUNT(HR.HireID), 0) , 0) AS percentage
from NewHire NH LEFT JOIN Hire_Response HR ON NH.HireID = HR.HireID
group by NH.HireID) d
ON a.HireID = d.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
...这会将列percentage
添加到您当前的查询中。这可能会有所改进,但它应该让你神清楚楚如何将问题的结果结合在一起。