如何将默认值分配给聚合函数中的日期?

select isnull(min(date_tested),'01-01-2010') from test_date where date_tested>=@startdate and enddate<=@enddate 
我有时会得到这个输出 -
datetested
__________
我想要 -
datetested
01-01-2010
所以这不起作用...我的意思是在日期不存在的情况下它返回空白..我想在任何时候返回一个默认值我猜空白....因为我猜date_tested不是null否则它会对它有一个赋值。如果它为null,我可以给它分配默认值吗?     
已邀请:
试试这个: 通过date_tested从test_date组中选择min(isnull(date_tested,'01 -01-2010')) 在聚合中使用isnull和case语句可以获得很多功能     
试试这个
select coalesce(date_tested,'01-01-2010') from test_date group by date_tested
    
为什么要按聚合函数中使用的列进行分组?如果X值满足
WHERE
中的条件,则会得到X行。但如果它真的是你想要的,你可以使用
SELECT 
ISNULL((SELECT MIN(date_tested) 
FROM test_date 
WHERE date_tested>=@startdate and enddate<=@enddate  
group by date_tested),'01-01-2010') as [datetested];
    
好吧,我不明白你是否想要MIN date_tested,或者你想要date_tested的所有可能值,因为你在date_tested上使用MIN并且你是GROUPING BY date_tested。如果您想要MIN值,那么您可以执行以下操作:
SELECT MIN(ISNULL(date_tested,'01-01-2010'))
FROM test_date
WHERE date_tested>=@startdate and enddate<=@enddate 
你必须要小心,因为如果它为null,你将为date_tested赋值。如果你想要date_tested的所有可能值,那么你可以这样做:
SELECT ISNULL(date_tested,'01-01-2010')
FROM test_date
WHERE date_tested>=@startdate and enddate<=@enddate 
GROUP BY ISNULL(date_tested,'01-01-2010')
    
尝试使用您的开始日期参数作为默认值,如下所示...
select isnull(min(date_tested),@startdate)
from test_date 
where date_tested>=@startdate 
and enddate<=@enddate
    

要回复问题请先登录注册