取两次平均长度并添加列查询

时间:2015-12-15 16:49:05

标签: sql-server-2008

请参阅以下查询

Select avg(datediff(,date1, date2)) as Avg_TOTALLengthofServicePerMember.

现在我想创建另一个查询。

select 
    id, date1, date2, datediff(date1,date2) as Memberdiff, 
    Avg_TOTALLengthofServicePerMember

结果应该是......

ID    Date1      Date2       Memberdiff     Avg_TOTAlLengthofServicePerMember
1      1/1/2015   1/2/2015       1               10
2      1/1/2015   1/3/2015       2               10

1 个答案:

答案 0 :(得分:0)

这是我最好的结果......

select M1.id, M1.date1, M1.date2, avg(datediff(day, date1, date2)) as Memberdiff , (select avg(datediff(day, date1, date2)) from m) as Avg_TOTAlLengthofServicePerMember
FROM M1
GROUP BY M1.id, M1.date1, M1.date2