在我的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 :(得分:2)
这应该这样做
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
CAST((select COUNT(*) from Hire_Response WHERE HireID = a.HireID AND
(HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select case when COUNT(*) = 0 then 1 else COUNT(*) end from Hire_Response WHERE HireID = a.HireID) as FLOAT) as mySpecialColumn
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
答案 1 :(得分:1)
您只需将计算添加到您的选择语句中,我已经在下面添加了计算,我还为您计算了一个列名:
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
CAST((select COUNT(*) from Hire_Response WHERE HireID = a.HierID AND
(HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select COUNT(*) from Hire_Response WHERE HireID = a.HierID) as FLOAT) AS [Calculation]
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
答案 2 :(得分:0)
您需要为列添加别名。 这是假设您的计算正确参考您需要的列。
select
(Calculated Column expression) as CalculatedColumn,
*
from
TableName
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
(CAST((select COUNT(*) from Hire_Response WHERE HireID = a.HireIDAND (HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select COUNT(*) from Hire_Response WHERE HireID = a.HireID) as FLOAT)) as CalculatedColumn
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
答案 3 :(得分:0)
在没有数据结构示例的情况下试图弄清楚目标,但这里就是这样。我添加了一个新的JOIN和GROUP BY子句:
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
sum(case when hr.HireResponse in (0, 1) then 1.00 else 0.00 end) as Numerator,
count(hr.*) as Denominator,
sum(case when hr.HireResponse in (0, 1) then 1.00 else 0.00 end) / count(hr.*) as Fraction
from
NewHire a join
Position b on a.Position_ID = b.Position_ID join
WorkPeriod c on a.hireID = c.HireID inner join
Hire_Response hr on a.HireID = hr.HireID
where
a.Archived = 0
and c.InquiryID is not null
group by
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name
order by
a.HireID DESC, a.HireLastName, a.HireFirstName