奇怪的NULL值,请大侠指点!
比如T表,我要得出前三个字段相同记录的Counts总和
StartDate EndDate ID Counts
2001-5-1 <NULL> 1 2
2001-5-1 <NULL> 1 1
2005-1-1 2005-10-1 1 5
2005-1-1 2005-10-1 1 3
我的做法是:
select StartDate,EndDate,ID,
(select sum(Counts) from T T1 where StartDate=T.StartDate and EndDate=T.EndDate and ID=T.ID) as Total order by StartDate,EndDate,ID
结果2005-1-1至2005-10-1的记录可以累加,结果为8
但上面两条(2001-5-1)确无法累加,结果为 <NUUL> ,请问这是怎么回事啊?
我看是EndDate=T.EndDate这个条件不成立造成的,我这样做对吗?怎么解决这个问题?
------解决方案--------------------select StartDate , isnull(EndDate, ' ') EndDate , id , sum(counts)
from tb
group by StartDate , isnull(EndDate, ' ') EndDate , id