2017-04-06 91 views
1

如何寫兩個select語句在一個查詢的一個結果,我希望兩個SELECT語句的一個結果相結合的平均如何寫兩個select語句在一個查詢的一個結果

結果查詢,如: -

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 
+0

嘗試somethink這是可能的動態SQL查詢? –

+0

是的,這是可能的,但你需要關心連接適當的值。 –

+0

我需要插入到現有的動態SQL查詢,我應該從哪裏開始 –

相關問題