简体   繁体   English

如何为db2 Sql中的每一列汇总没有空值的记录?

[英]How to rollup records having not null values for each column in db2 Sql?

I have a table as below-我有一张桌子如下-

id ID name姓名 col1第 1 列 col2列2 col3第 3 列
1 1 aaa啊啊啊 1 1 null空值 null空值
1 1 aaa啊啊啊 null空值 1 1 null空值
1 1 aaa啊啊啊 null空值 null空值 1 1
1 1 aaa啊啊啊 null空值 null空值 2 2
1 1 aaa啊啊啊 null空值 1 1 null空值
2 2 bbb bbb null空值 null空值 null空值
2 2 bbb bbb 1 1 null空值 null空值

i want the output as below-我想要输出如下-

id ID name姓名 col1第 1 列 col2列2 col3第 3 列
1 1 aaa啊啊啊 1 1 1 1 1 1
1 1 aaa啊啊啊 null空值 1 1 2 2
2 2 bbb bbb 1 1 null空值 null空值

i tried doing this:我试过这样做:

select id ,name, max(col1),max(col2),max(col3) 
from table group by id, name;

but the output is this, which is not as expected但输出是这样的,这不是预期的

id ID name姓名 col1第 1 列 col2列2 col3第 3 列
1 1 aaa啊啊啊 1 1 1 1 2 2

can u pls help me achieve the desired output.你能帮我实现想要的输出吗? many thanks in advance.提前谢谢了。

You may try this as is.你可以试试这个。

Firstly, the rows in each group specified by (id, name) columns are enumerated.首先,枚举由(id,name)列指定的每个组中的行。 Note, that if you don't use the order by clause in the row_number function (as in the example), such an enumeration may be done in some non-repeatable and non-predictable way.请注意,如果您不在 row_number 函数中使用 order by 子句(如示例中所示),则可能会以某种不可重复和不可预测的方式进行此类枚举。 So, if you want a repeatable result, you should have a way to order the rows explicitly.所以,如果你想要一个可重复的结果,你应该有一种方法来明确地对行进行排序。 I've added an additional row, and you may use it uncommenting the order by clause to get the exact result as you specified in the question.我添加了一个额外的行,您可以使用它取消对 order by 子句的注释,以获得您在问题中指定的确切结果。

Secondly, the rows in each group are processed with a Recursive Common Table Expression statement.其次,用递归公用表表达式语句处理每组中的行。 A new surrogate column grp (additional group number inside each group) is introduced.引入了一个新的代理列 grp(每个组内的附加组号)。 The first row of each group (having rn_=1) gets all the original values and grp=1.每组的第一行(具有 rn_=1)获取所有原始值且 grp=1。 The grp value of each next row is increased by 1, if the the value of c1, c2 or c3 in this next row is not null and a value in a previous row of the corresponding column is non null as well.如果下一行的 c1、c2 或 c3 的值不为空,并且对应列的前一行中的值也非空,则每下一行的 grp 值增加 1。
If the group number is not increased, a non-null value from a previous row is copied instead the current value for each cx.如果组号没有增加,则复制前一行的非空值而不是每个 cx 的当前值。
If it is increased, the current row values are retained.如果增加,则保留当前行值。

Finally, we use this surrogate group number in the group by clause.最后,我们在 group by 子句中使用这个代理组号。
dbfiddle link . dbfiddle 链接

with mytab (id, name, c1, c2, c3, ord) as
(
values
  (1, 'aaa', 1, null::int, null::int, 10)
, (1, 'aaa', null, 1, null, 20)
, (1, 'aaa', null, null, 1, 30)
, (1, 'aaa', null, null, 2, 40)
, (1, 'aaa', null, 1, null, 50)
, (2, 'bbb', null, null, null, 1)
, (2, 'bbb', 1, null, null, 2)
)
-- Rows enumeration inside all groups
, mytab2 as 
(
select row_number () over (partition by id, name
--order by ord
) rn_, t.*
from mytab t
)
--
, a (id, name, rn_, grp, c1, c2, c3) as
(
select id, name, rn_, 1 as grp, c1, c2, c3 
from mytab2
where rn_ = 1
  union all
select t.id, t.name, t.rn_
, a.grp 
+ case 
    when a.c1 is not null and t.c1 is not null
      or a.c2 is not null and t.c2 is not null
      or a.c3 is not null and t.c3 is not null
    then 1
    else 0
  end as grp
, case 
    when a.c1 is not null and t.c1 is not null
      or a.c2 is not null and t.c2 is not null
      or a.c3 is not null and t.c3 is not null
    then t.c1
    else coalesce (a.c1, t.c1)
  end as c1
, case 
    when a.c1 is not null and t.c1 is not null
      or a.c2 is not null and t.c2 is not null
      or a.c3 is not null and t.c3 is not null
    then t.c2
    else coalesce (a.c2, t.c2)
  end as c2
, case 
    when a.c1 is not null and t.c1 is not null
      or a.c2 is not null and t.c2 is not null
      or a.c3 is not null and t.c3 is not null
    then t.c3
    else coalesce (a.c3, t.c3)
  end as c3
from a, mytab2 t
where t.id = a.id and t.name = a.name and t.rn_ = a.rn_ + 1
)
select id, name
, max (c1) as c1
, max (c2) as c2
, max (c3) as c3
from a
group by id, name, grp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM