0

我会尽力解释我想要完成的事情。

我有一个包含以下数据子集的表:

ID    BF_ID    C_ID    T_ID
1     1        10000   182
2     1        230     null
3     2        123     null
4     3        10000   124

基本上,我想根据 查询此表BF_ID以查看结果中是否存在同时具有非空T_ID和非空 C_ID 的数据。

在上面的示例中,我想区分不同 BF_ID 之间的查询。BF_id 的行数可以是无限的。

  • BF_ID 为 1 将返回“混合”。
  • BF_ID 为 2 将返回“C_ID_ATTRIB”
  • BF_ID 为 3 将返回“T_ID_ATTRIB”
4

2 回答 2

0

Gordon 提供的示例的另一种替代方法,可能更具可读性:(只是在没有测试的情况下输入) select bf_id, case when c is null and t is null then 'neither' when c is null and t is not null then 'T_ID_ATTRIB' when c is not null and t is null then 'C_ID_ATTRIB' else 'mixed' as which_attr from ( select bf_id, max(c_id) c, max(t_id) t from table1 group by bf_id)

于 2012-11-30T20:09:32.403 回答
0

为此,您只需要一个 group by 和 case 语句:

select bf_id,
       (case when count(t_id) = 0 and count(c_id) = 0 then 'Neither'
             when count(t_id) > 0 and count(c_id) = 0 then 'T_ID_ATTRIB'
             when count(t_id) = 0 and count(c_id) > 0 then 'C_ID_ATTRIB'
             else 'Mixed'
        end) as WhichAttribute
from t
group by bf_id
于 2012-11-30T19:48:55.370 回答