我有一个看起来像这样的表:
我正在寻找一种方法来合并#include <iostream>
#include <cassert>
using namespace std;
struct lnode
{
int key;
lnode* next;
lnode(int k, lnode* n = nullptr) :key(k), next(n) {}
};
void insert(lnode* &L, int x)
{
lnode **pos = &L;
while (*pos && (*pos)->key <= x) {
pos = &((*pos)->next);
}
*pos = new lnode(x, *pos);
}
int main()
{
lnode * t = nullptr;
insert(t, 3);
insert(t, 4);
insert(t, 1);
insert(t, 7);
insert(t, -4);
insert(t, 9);
insert(t, 2);
while (t) {
std::cout << t->key << " ";
t = t->next;
}
}
上的列,以便查询返回以下内容:
organizations_core_id
如何合并这些列并替换最新值?
答案 0 :(得分:1)
首先group by organization_core_id
来获取具有slug
和name
的最后一个非空值的行的ID,然后加入表:
select
t.organization_core_id,
t1.slug,
t2.name
from (
select
organization_core_id,
max(case when slug is not null then id end) slugid,
max(case when name is not null then id end) nameid
from tablename
group by organization_core_id
) t
left join tablename t1 on t1.id = t.slugid
left join tablename t2 on t2.id = t.nameid
请参见demo。
结果:
> organization_core_id | slug | name
> -------------------: | :------------- | :---------
> 1 | dolphin | Dolphin v2
> 2 | sea-horse-club | Sea Horse