分析不同数据库实例中相同表的数据
简短版本:
如果表 A 和 B 中的两列都有一个公共标识符,而该标识符又可能在 C 列中具有两个值,那么如何映射表 A 和 B 中的两列
可以说:
A
---
1 , 2
B
---
? , 3
C
-----
45, 2
45, 3
使用表 CI 知道 id 2 和 3 属于同一项目 ( 45 ),因此“?”表 B 中的值应该是 1。
什么查询可以执行类似的操作?
编辑
省略长版本。这真的很无聊/令人困惑
编辑
我在这里发布了一些输出。
从这个查询:
select distinct( rolein) , activityin from taskperformance@dm_prod where activityin in (
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
)
我有以下部分:
select distinct( activityin ) from taskperformance where rolein = 0
输出:
http://question1337216.pastebin.com/f5039557
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
输出:
http://question1337216.pastebin.com/f6cef9393
最后:
select distinct( rolein) , activityin from taskperformance@dm_prod where activityin in (
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
)
输出:
http://question1337216.pastebin.com/f346057bd
以第一个查询中的 Activityin 335 为例(来自任务性能 B)。
它存在于 A 的活动中。
但不存在于 A 的任务绩效中(但相关活动:92、208、335、595)
存在于结果中。对应的角色为:1
Short version:
How can I map two columns from table A and B if they both have a common identifier which in turn may have two values in column C
Lets say:
A
---
1 , 2
B
---
? , 3
C
-----
45, 2
45, 3
Using table C I know that id 2 and 3 belong to the same item ( 45 ) and thus "?" in table B should be 1.
What query could do something like that?
EDIT
Long version omitted. It was really boring/confusing
EDIT
I'm posting some output here.
From this query:
select distinct( rolein) , activityin from taskperformance@dm_prod where activityin in (
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
)
I have the following parts:
select distinct( activityin ) from taskperformance where rolein = 0
Output:
http://question1337216.pastebin.com/f5039557
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
Output:
http://question1337216.pastebin.com/f6cef9393
And finally:
select distinct( rolein) , activityin from taskperformance@dm_prod where activityin in (
select activityin from activities@dm_prod where activityid in (
select activityid from activities@dm_prod where activityin in (
select distinct( activityin ) from taskperformance where rolein = 0
)
)
)
Output:
http://question1337216.pastebin.com/f346057bd
Take for instance activityin 335 from first query ( from taskperformance B) .
It is present in activities from A.
But is not in taskperformance in A ( but a the related activities: 92, 208, 335, 595 )
Are present in the result. The corresponding role in is: 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎对于任何给定的
activityin
,都适用相同的rolein
值。因此,如果数据库 A (db1) 对于角色/活动关系至少有一个关联,则可以使用简单的一次性更新查询填充数据库 B (db2):我强烈建议在运行查询之前先备份数据库因为它会改变所有的
rolein
值,如果我的假设不正确,你可能会得到错误的数据。编辑
虽然我认为它可以在一个查询中完成,但这超出了我对sql的了解。不过,我认为以下方法可行:创建一个临时表,其中包含数据库 A 中每个
activityin
值的所有rolein
值。该表本质上成为一个活动表,除了现在您用数字替换了activityid
值,可以查找这些数字来填充数据库 B 的rolein
的缺失值。从原始数据来看,这会产生:
现在您应该能够针对此临时表运行更新查询:
问题在于,如果数据库 B 中的
activityin
具有唯一值,而数据库 A 中没有出现该值。最后:
为了完整性起见。
It seems like for any given
activityin
, the samerolein
value applies. Thus if database A (db1) has at least one association for each for the rolein/activityin relationships, you can populate database B (db2) with a simple one-time update query:I strongly advise backing up the database first before running the query as it will change all the
rolein
values, and if my assumption is incorrect, you may have bad data.EDIT
Although I think it could be done in one query, it's beyond my knowledge of sql. However I think the following will work: Create a temporary table that contains all the
rolein
values for eachactivityin
value in database A. This table essentially becomes an activities table, except now you have numbers replacing theactivityid
values, which can be looked up to fill in the missing values ofrolein
for database B.From the original data, this yields:
Now you should be able to run the update query against this temporary table:
The problem with this will be if you have unique values in database B for
activityin
that do not occur in database A.Finally:
For completeness' sake.
终于我明白了。
看来我只是没有开发出有助于提问的大脑部分。
这是结果
谢谢 JYelton,你的问题帮助我更好地理解了我自己的问题。
Finally I've got it.
It seems that I just didn't develop the section of the brain that helps to make queries.
Here's the result
Thank you JYelton, your questions helped me to understand my own question better.