下面是我最初的数据集的样子
prof_id id title
1 5 A
1 5 B
1 5 C
1 5 D
2 5 C
2 5 D
2 5 E
NA 5 F
NA 5 G下面是新表的样子:
prof_id id title
1 5 A
1 5 B
1 5 C
1 5 D
1 5 F
1 5 G
2 5 C
2 5 D
2 5 E
2 5 F
2 5 G对于prof_id具有空值的任何行都应该归因于所有的prof_id。我提供了一个示例,其中有两个‘prof_id’,但也有一个有1或0 prof_id的实例。
prof_id我对SQL并不熟悉,所以我不知道如何开始。如有任何指导,将不胜感激。
谢谢
发布于 2019-07-24 21:57:08
在这种情况下,您需要做交叉连接,实质上是将两个表相乘在一起。
首先挑出所有的空白处:
select id, title from table where prof_id is null然后选择要应用于所有表的prof_id
select distinct prof_id from table where prof_is is not null在一起进行交叉连接,然后将其余的“好”数据合并回来。
(select distinct prof_id from table where prof_is is not null)
CROSS JOIN
(select id, title from table where prof_id is null)
UNION ALL
(select prof_id, id, title from table where prof_id is not null)发布于 2019-07-24 23:17:18
可以使用cross join生成所有行。然后使用union all将其与其余数据结合起来。
以下语法应该可以工作:
select p.prof_id, i.id, t.title
from (select distinct prof_id
from t
where prof_id <> 'NA' -- or do you mean is not null
) p cross join
(select distinct id from t) i cross join
(select distinct title
from t
where prof_id = 'NA' -- or is null
) t
union all
select prof_id, id, title
from t
where prof_id <> 'NA' -- or is not nullhttps://stackoverflow.com/questions/57191650
复制相似问题