如何使用聚合函数从列中选择第一个非null项



具有真实数据的示例:

CustomerId      Scope       Key                             Value
1               Customer    SelfPasswordReset_MaxAttempts   5
2               Customer    SelfPasswordReset_MaxAttempts   10
3               Customer    SelfPasswordReset_MaxAttempts   20
NULL            Platform    PlatformGUID                    c20414f6-3bd2-4d37-ac74-152b0efdb582
...

假设我们还有一个CustomerParant表

CustomerId  ParentId    Level
5           5           0
5           1           1
4           4           0
4           2           1
4           1           2
3           3           0
3           2           1
3           1           2
2           2           0
2           1           1
1           1           0

列出每个客户的父级,以及需要遍历树才能到达那里的迭代次数。意味着3的父母是2,2的父母是1等等…

现在我想构建一个查询,返回如下结果:

CustomerId  SelfPasswordReset_MaxAttemps    PlatformGUID ... 
5           5                   c20414f6-3bd2-4d37-ac74-152b0efdb582
4           10                  c20414f6-3bd2-4d37-ac74-152b0efdb582
3           20                  c20414f6-3bd2-4d37-ac74-152b0efdb582
2           10                  c20414f6-3bd2-4d37-ac74-152b0efdb582
1           5                   c20414f6-3bd2-4d37-ac74-152b0efdb582

特别注意:并不是每个客户都定义了自己的级别。正如您所看到的,我添加了客户4和5,他们没有自己定义的参数。他们将继承他们的父母,而不是

一般来说,SQL合并函数返回第一个定义的非空值,该值来自等参数列表

Coalesce (NULL,1,2) // this will return 1

Coalesce (NULL,'abc','xyz') // this will return abc

最新更新