如何排除所有与我的平均声明相反的日期值?
查询:
select Avg_DayDiff case when Avg_DayDiff > 0
from(
SELECT DATEDIFF(DAY, xx, yy) AS Avg_DayDiff
FROM Database1.dbo.tbl_1
where
month(datecompleted) = month(dateadd(month,-1,current_timestamp))
and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
and ApprovalRequiredFrom = 'GRM'
) temptable
我的查询从第一行返回语法错误(select语句)我还在学习,所以任何提示都非常感谢!
我正在寻找的结果是只返回约会,如果#> 0,所以我可以平均一下。
感谢
答案 0 :(得分:2)
请尝试在where
声明中使用case when
子句作为主查询,而不是select
:
select Avg_DayDiff
from(
SELECT DATEDIFF(DAY, xx, yy) AS Avg_DayDiff
FROM Database1.dbo.tbl_1
where
month(datecompleted) = month(dateadd(month,-1,current_timestamp))
and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
and ApprovalRequiredFrom = 'GRM'
) temptable
where Avg_DayDiff > 0
答案 1 :(得分:1)
您可以简单地排除例如
的值 SELECT DATEDIFF(DAY, xx, yy) AS Avg_DayDiff
FROM Database1.dbo.tbl_1
where month(datecompleted) = month(dateadd(month,-1,current_timestamp))
and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
and ApprovalRequiredFrom = 'GRM'
and DATEDIFF(DAY, xx, yy) > 0
答案 2 :(得分:1)
使用abs()函数,它只返回正值。
SELECT abs(DATEDIFF(DAY, xx, yy)) AS Avg_DayDiff
以上将足以获得正值并避免负值。