如何在一个查询中为一个结果编写两个select语句

时间:2017-04-06 09:09:54

标签: sql-server-2008-r2 dynamic-sql

如何在一个查询中为一个结果编写两个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

1 个答案:

答案 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