2016-03-15 42 views
1

我在刪除NULLS時遇到困難。 ISNULL聲明似乎適用於動態結果,但不適用於第二種。從條件聚合結果中刪除NULLS

我最終提取查詢是:顯示

select itemid,title,description,cat,fibre,washing,colours,promo, 
    max(case when seqnum = 1 then isnull(chm_sizegrouping,'') end) as sizes_1, 
    max(case when seqnum = 2 then isnull(chm_sizegrouping,'') end) as sizes_2, 
    max(case when seqnum = 3 then chm_sizegrouping end) as sizes_3, 
    max(case when seqnum = 4 then chm_sizegrouping end) as sizes_4) 

結果是:

itemid sizes_1 sizes_2 
LM008 one  NULL 
LM009   NULL 
LM010   NULL 
lm011   NULL 

任何幫助是非常讚賞,

感謝。

回答

1

試試這個:

select itemid,title,description,cat,fibre,washing,colours,promo, 
     isnull(max(case when seqnum = 1 then chm_sizegrouping end),'') as sizes_1, 
     isnull(max(case when seqnum = 2 then chm_sizegrouping end),'') as sizes_2, 
........ 
+0

好吧,我感到很蠢.................. :( – DanDan

+0

It works。Thank you! – DanDan

0

不幸的是,您必須使用ISNULL函數才能檢索值爲NULL的每個字段。

+0

那奇怪的事情,我有。第一列「sizes_1」不包含NULL,但是「sizes_2」包含NULL。儘管我已經將ISNULL語句包裹在「sizes_2」摘錄中。 – DanDan

+0

您需要將max()括入isnull() - 'isnull(max(...),'')',反之亦然。 – Arvo

+0

在isnull函數內封裝max() –

0

只要把ISNULL支票上的MAX()功能外,像這樣:

select itemid,title,description,cat,fibre,washing,colours,promo, 
    isnull(max(case when seqnum = 1 then chm_sizegrouping end), '') as sizes_1, 
    isnull(max(case when seqnum = 2 then chm_sizegrouping end),'') as sizes_2, 
    max(case when seqnum = 3 then chm_sizegrouping end) as sizes_3, 
    max(case when seqnum = 4 then chm_sizegrouping end) as sizes_4)