如何从查询结果中将单元格设为空白?

时间:2016-11-03 23:07:22

标签: sql informix

我的查询如下:

SELECT 
     cc.chain_desc as chain_desc 
    ,cc.chain_id as chain_id 
    ,COUNT(distinct t.trans_id) as TranCount
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
GROUP BY cc.chain_desc, cc.chain_id 


UNION 

SELECT 
    'TOTAL'
    ,0
    ,COUNT(distinct t.trans_id) 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 

执行上述查询会重新生成以下结果:

enter image description here

我需要将结果显示如下:

enter image description here

专栏&#34; Chain_Id&#34;是&#34;整数&#34;类型,我该怎么做那个空白?

3 个答案:

答案 0 :(得分:3)

你可以简单地选择null

 .....

 UNION 

SELECT 
    'TOTAL'
    , NULL::INTEGER
    ,COUNT(distinct t.trans_id) 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 

因为null不是您可以尝试在第一个查询上方添加的类型

DEFINE test INT;
LET test = NULL;


......

SELECT 
    'TOTAL'
    , test
    ,COUNT(distinct t.trans_id) 
    .....

或者像@Jonathan Leffler所说的那样使用NULL::INTEGERCAST(NULL AS INTEGER)

答案 1 :(得分:2)

一种方法是转换为NULL

    (case when cc.chain_id <> 0 then cc.chain_id end) as chain_id 

另一种方法是将所有内容转换为字符串:

    (case when cc.chain_id <> 0 then cast(cc.chain_id as varchar(255)) else '' end) as chain_id 

答案 2 :(得分:2)

在Informix中,您可以在投影列表中使用NULL,但该列必须具有类型。由于在Informix NULL中没有类型,因此您需要使用CAST

SELECT NULL::INTEGER AS id FROM systables WHERE tabid = 1;

SELECT CAST(NULL AS INTEGER) AS id FROM systables WHERE tabid = 1;

您可以查看其他问题的答案(Informix: Select null problem)。

您可以查看IBM知识中心(NULL Keyword)。