2013-05-14 68 views
0

在我的SQL Server的代碼,我有這樣的select語句如何將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是上述select語句中的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

請不要在同一個問題上發佈多個網站。如有需要,我們可以提出問題。另外,請不要在DBA.SAE上發佈這樣的基本問題。 – JNK 2013-05-14 16:26:23

回答

1

您可以使用GBN爲你做的代碼對你other question作爲一個子查詢

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添加到當前的查詢。這可能會得到改善,但它應該給你一個上帝的想法,你可以如何將你的問題的結果放在一起。

+0

那有用,但是'1E0'是什麼? – omega 2013-05-14 16:13:11

+0

這是數字'1'的科學記數法。我懷疑@gbn把它放在那裏,以便將呈現給你的百分比格式 – RoKa 2013-05-14 16:23:28