Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将关系数据行粘合在一起_Sql_Oracle_Rdbms - Fatal编程技术网

Sql 将关系数据行粘合在一起

Sql 将关系数据行粘合在一起,sql,oracle,rdbms,Sql,Oracle,Rdbms,我有一个稀疏的表,结构如下: id | name | phone | account 没有主键或索引 也有空值。我想要的是将不同行的数据“粘合”在一起,例如: 给定 我想和你在一起 id | name | phone | account | 1 'John' '339-33-27' 4 但是,我不知道表中遗漏了哪些值。 处理这类问题的一般方法是什么?我需要只使用连接还是可能是递归函数

我有一个稀疏的表,结构如下:

id | name      | phone          | account   
没有主键或索引

也有空值。我想要的是将不同行的数据“粘合”在一起,例如: 给定

我想和你在一起

id | name       | phone           | account |
  1     'John'    '339-33-27'        4      
但是,我不知道表中遗漏了哪些值。 处理这类问题的一般方法是什么?我需要只使用连接还是可能是递归函数

更新:提供了更清晰的示例

id to account is many-to-many 
account to name is many-to-many 
phone to name is one-to-one
数据库基本上是原始事务数据
我想要的是获取我已经拥有/可以找到帐户的所有行

如果我理解正确,那么这可能会起作用。您需要的是一个自连接

select t2.id, t1.name, t1.phone, t1.account
from table1 t1
join table1 t2 on t1.account = t2.account and t1.phone = t2.phone
where t1.name is not null
然而,这个特定的查询依赖于来自示例数据的假设。我的假设是,如果name不为null,Id将为null,可以通过查看电话号码和帐户找到Id。如果这个假设不成立,那么我们可能需要更多的样本数据来解决您的问题


根据数据的不同,您可能需要左连接或交换,以便T1获取id而不是名称,其中的条件是id不为null。数据样本量如此之小,很难判断。

请不要将您能找到的每一个数据库都记录下来。仅标记您正在使用的行。如果两行在同一列中具有不同的非空值,该怎么办?这会发生吗?如果是这样,你应该告诉我们你希望看到什么。。。如果没有,我想知道这张桌子可能是什么;)我删除了不兼容的数据库标记。请只标记您正在使用的数据库。我认为如果不知道您为什么要这样做,就无法回答此问题。有很多操作可以将行分组在一起(例如,阅读数据库的
group BY
子句),但如何使用它很大程度上取决于您想要完成的任务。更新的标记,@Frazz更清晰的解释
select t2.id, t1.name, t1.phone, t1.account
from table1 t1
join table1 t2 on t1.account = t2.account and t1.phone = t2.phone
where t1.name is not null