如何在一个查询中为一个结果编写两个select语句,我想将一个结果的两个select语句组合为avg
结果查询如: -
select (idcount/days) as avg
QUERY1: -
select distinct COUNT(id) as idcount
from employee
where joiningdate between CONVERT(varchar,@fromdate,103) and CONVERT(varchar,@TODATE,103)
QUERY2: -
select DATEDIFF(d,@FromDate,@todate) as days
答案 0 :(得分:0)
尝试这样的想法
;WITH CTE AS(
SELECT COUNT(DISTINCT id) AS idcount
FROM employee
WHERE joiningdate BETWEEN CONVERT(varchar,@fromdate,103) and CONVERT(varchar,@TODATE,103)
)
SELECT idcount/(DATEDIFF(d,@FromDate,@todate)) AS AV
FROM CTE