超奇怪的問題?數值類SUM後,返回的結果還是數據,但為什麼不能計算?
select T01 as 品號,T02 as 品名,T03 as 尺寸,T04 as 上月結存,isnull(X01,0) as 本月入庫,isnull(X02,0) as 本月出庫,isnull(X03,0) as 本月調整,isnull((T04+X01-X02+X03),0) as 結存數 from
(
select * from
(
/*庫別中品號資料*/
select TAICON_TA.TA001 as T01,TAICON_TA.TA002 as T02,TAICON_TA.TA006 as T03
from TAICON_TA,TAICON_TB,INPUT_A,INPUT_B
where INPUT_B.B002 =TAICON_TA.TA001 and TAICON_TA.TA006=TAICON_TB.TB002 and TAICON_TB.TB003= '5 ' and INPUT_A.A001=INPUT_B.B001 and INPUT_A.A012=INPUT_B.B006 and INPUT_A.A002= '1 ' and ( INPUT_A.A014= '[2]107待處理品倉 ')
group by TAICON_TA.TA006,TAICON_TA.TA001,TAICON_TA.TA002,TAICON_TB.TB001
/*order by TAICON_TB.TB001,TAICON_TA.TA001*/
)a left join
/*上月結存*/
(select INPUT_B.B002,isnull((SUM(INPUT_B.B005)-SUM(INPUT_B.B007)+SUM(INPUT_B.B009)),0) as T04
from INPUT_A,INPUT_B
where INPUT_A.A001=INPUT_B.B001 and INPUT_A.A012=INPUT_B.B006 and INPUT_A.A002= '1 ' and INPUT_A.A008 < '2007.03.00 '
group by INPUT_B.B002
)
b on a.T01=b.B002
)c left join
(
/*本月結存*/
select INPUT_B.B002,isnull(SUM(INPUT_B.B005),0) as X01,isnull(SUM(INPUT_B.B007),0) AS X02,isnull(SUM(INPUT_B.B009),0) as X03,isnull((SUM(INPUT_B.B005)-SUM(INPUT_B.B007)+SUM(INPUT_B.B009)),0) as X04
from INPUT_A,INPUT_B
where INPUT_A.A001=INPUT_B.B001 and INPUT_A.A012=INPUT_B.B006 and INPUT_A.A002= '1 ' and INPUT_A.A008 like '2007.03.% '
group by INPUT_B.B002
)d on c.T01= d.B002
where ((T04+X01+X02+X03+X04) <> 0)
order by T01
/*where ((T04+X01+X02+X03+X04) <> 0)這一句的作用是不顯示T04,X01,X02,X03,X04都為0或Null的結果.但T04有的值不為0的也給排除了,奇怪.
如下面沒有加Where語句後顯示的結果%/
品 號品 名尺 寸上月結存本月入庫(全)本月出庫(全)本月調整(全)結存數
TAQ2D151MA18250LL3TAQ 200V 150uF ML18x25990 0 0 0 990
TAQ2D331MK1835MLL3TAQ 200V 330uF ML18x35.50 125,378 118,567 0 6,811
TAQ2D470MA12200LL5TAQ 200V 47uF ML12.5x20336 0 0 0 336
TAQ2D471MK18400LL3TAQ 200V 470uF ML18x4011,071 81,726 88,126 0 4,671
TAQ2D471YK18400LL3TAQ 200V 470uF YL18x400 3,105 2,400 0 705
TAQ2G100MK1012MLL3TAQ 400V 10uF ML10x12.5206,297 0 0 0 206,297
TAQ2G100MK10160RC3TAQ 400V 10uF MR10x1697,963 0 0 0 97,963
TAQ2G101MK1631MLL3TAQ 400V 100uF ML16x31.52,723 11,491 0 0 14,214
/*加了where ((T04+X01+X02+X03+X04) <> 0)後顯示:*/
TAQ2D331MK1835MLL3TAQ 200V 330uF ML18x35.50 125,378 118,567 0 6,811
TAQ2D471MK18400LL3TAQ 200V 470uF ML18x4011,071 81,726 88,126 0 4,671
TAQ2D471YK18400LL3TAQ 200V 470uF YL18x400 3,105 2,400 0 705
TAQ2G100MK1012MLL3TAQ 400V 10uF ML10x12.5206,297 0 0 0 206,297
TAQ2G100MK10160RC3TAQ 400V 10uF MR10x1697,963 0 0 0 97,963
TAQ2G101MK1631MLL3TAQ 400V 100uF ML16x31.52,723 11,491 0 0 14,214
/*有兩條(1,3條)不見了*/
[解决办法]
看看將每個元素都加上isnull()
where ((isnull(T04,0)+isnull(X01,0)+isnull(X02,0)+isnull(X03,0)+isnull(X04,0)) <> 0)
另外,你select 里的結存數 最好也寫成這樣
[解决办法]
where ((T04+X01+X02+X03+X04) <> 0)
这样写会出问题的,只要有一个值为null,所有的值相加后都为null
因为where 语句不和null记录做比较,所以只要为null的记录都不出来
[解决办法]
拼接字符串,如果不加isnull的判断只要出现null那么整个字符串就变成null。
[解决办法]
“上面select的isnull會無效”,不會,那個isnull還是有效,只是你上面的是在select中做判斷,下面是在where中做判斷,相互間沒關係的。
[解决办法]
isnull((T04+X01-X02+X03),0) as 結存數
上面select的也分開寫isnull(T04)+....否則結果不對的