0

在我的 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我从现有列中进行的计算。

我得到的数字是这样计算的: @acca.HireID来自上面的 select 语句。

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)

我怎样才能做到这一点?谢谢。

4

1 回答 1

1

您可以使用 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到您当前的查询中。这可能会有所改进,但它应该让您对如何将问题的结果汇总在一起有一个很好的想法。

于 2013-05-14T15:55:06.060 回答