我应该如何联接以下查询,以便可以将它们放在1个表中?

时间:2018-12-13 20:46:59

标签: sql sql-server

请在下面的SQL查询中提出建议。

Select Name AVG(datediff(dd, cast(App as Date), cast(Fund as Date)))    
from Main    
where Folder in ('Employee Crew', 'Crew')     
  and Month(Fund) = Month(getdate()) AND Year(Fund) = Year(getdate())    
Group By Crew


select Name AVG(datediff(dd, cast(App as Date), cast(Fund as Date)))        
from Main
where Folder in ('Employee Crew', 'Crew')
  and datediff(dd,cast(Fund as date),cast(getdate()as date)) between 0 and 90
Group By Crew


select Name AVG(datediff(dd, cast(App as Date), cast(Fund as Date)))
from Main     
where Folder in ('Employee Crew', 'Crew')     
  and datediff(dd,cast(Fund as date),cast(getdate()as date)) between 0 and 360        
Group By Crew

2 个答案:

答案 0 :(得分:1)

您可以在两个查询之间使用UNION

SELECT * FROM table1
UNION
SELECT * FROM table2

答案 1 :(得分:0)

使用条件聚合:

select Name, Crew,
       avg(case when Month(Fund) = Month(getdate()) AND Year(Fund) = Year(getdate()) 
                then datediff(day, cast(App as Date), cast(Fund as Date))
           end),
       avg(case when datediff(day, cast(Fund as date), cast(getdate() as date)) between 0 and 90 
                then datediff(day, cast(App as Date), cast(Fund as Date))
           end),
       avg(case when datediff(day, cast(Fund as date), cast(getdate() as date)) between 0 and 360  
                then datediff(day, cast(App as Date), cast(Fund as Date))
           end)
from Main     
where Folder in ('Employee Crew', 'Crew')       
Group By Name, Crew
相关问题