1

在我的 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

4 回答 4

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
于 2013-05-14T16:14:23.803 回答
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
于 2013-05-14T16:15:29.177 回答
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
于 2013-05-14T16:13:51.350 回答
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
于 2013-05-14T16:19:04.903 回答