sql server - How to merge a select statement with a calculated value as new columns? -
this question has answer here:
in sql server code, have select statement
select distinct a.hirelastname, a.hirefirstname, a.hireid, a.position_id, a.barnumber, a.archived, a.datearchived, b.position_name newhire join position b on a.position_id = b.position_id join workperiod c on a.hireid = c.hireid a.archived = 0 , c.inquiryid not null order a.hireid desc, a.hirelastname, a.hirefirstname
and want add new column it. column not column table, used store float
calculation make existing columns.
the number calculated this: @acc
a.hireid
above select statement.
cast((select count(*) hire_response hireid = @acc , (hireresponse = 0 or hireresponse = 1)) float) / cast((select count(*) hire_response hireid = @acc) float)
how can this? thanks.
you can use code gbn did on other question sub-query
select distinct a.hirelastname, a.hirefirstname, a.hireid, a.position_id, a.barnumber, a.archived, a.datearchived, b.position_name, d.percentage newhire 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) hr.hireid end) / nullif(count(hr.hireid), 0) , 0) percentage newhire nh left join hire_response hr on nh.hireid = hr.hireid group nh.hireid) d on a.hireid = d.hireid a.archived = 0 , c.inquiryid not null order a.hireid desc, a.hirelastname, a.hirefirstname
... add column percentage
current query. improved, should give god idea of how can bring results of questions together.
Comments
Post a Comment