分析不同数据库实例中相同表的数据

发布于 2024-08-02 13:03:49 字数 2075 浏览 6 评论 0原文

简短版本:

如果表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-08-09 13:03:49

似乎对于任何给定的 activityin,都适用相同的 rolein 值。因此,如果数据库 A (db1) 对于角色/活动关系至少有一个关联,则可以使用简单的一次性更新查询填充数据库 B (db2):

UPDATE db2.taskperformance
SET db2.taskperformance.rolein =
(SELECT db1.taskperformance.rolein
FROM db1.taskperformance
WHERE db1.taskperformance.activityin = db2.taskperformance.activityin);

我强烈建议在运行查询之前先备份数据库因为它会改变所有的rolein值,如果我的假设不正确,你可能会得到错误的数据。

编辑

虽然我认为它可以在一个查询中完成,但这超出了我对sql的了解。不过,我认为以下方法可行:创建一个临时表,其中包含数据库 A 中每个 activityin 值的所有 rolein 值。该表本质上成为一个活动表,除了现在您用数字替换了 activityid 值,可以查找这些数字来填充数据库 B 的 rolein 的缺失值。

CREATE TEMPORARY TABLE db2.ttable
SELECT db1.taskperformance.rolein, db1.activities.activityin
FROM db1.taskperformance, db1.activities
WHERE db1.taskperformance.activityin = db1.activities.activityin;

从原始数据来看,这会产生:

rolein     activityin 
1          1          
1          2          
2          3          
2          4          
3          6          
3          7          
3          7          

现在您应该能够针对此临时表运行更新查询:

UPDATE db2.taskperformance
SET db2.taskperformance.rolein =
(SELECT db2.ttable.rolein
FROM db2.ttable
WHERE db2.taskperformance.activityin = db2.ttable.activityin);

问题在于,如果数据库 B 中的 activityin 具有唯一值,而数据库 A 中没有出现该值。

最后:

DROP TEMPORARY TABLE db2.ttable;

为了完整性起见。

It seems like for any given activityin, the same rolein 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:

UPDATE db2.taskperformance
SET db2.taskperformance.rolein =
(SELECT db1.taskperformance.rolein
FROM db1.taskperformance
WHERE db1.taskperformance.activityin = db2.taskperformance.activityin);

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 each activityin value in database A. This table essentially becomes an activities table, except now you have numbers replacing the activityid values, which can be looked up to fill in the missing values of rolein for database B.

CREATE TEMPORARY TABLE db2.ttable
SELECT db1.taskperformance.rolein, db1.activities.activityin
FROM db1.taskperformance, db1.activities
WHERE db1.taskperformance.activityin = db1.activities.activityin;

From the original data, this yields:

rolein     activityin 
1          1          
1          2          
2          3          
2          4          
3          6          
3          7          
3          7          

Now you should be able to run the update query against this temporary table:

UPDATE db2.taskperformance
SET db2.taskperformance.rolein =
(SELECT db2.ttable.rolein
FROM db2.ttable
WHERE db2.taskperformance.activityin = db2.ttable.activityin);

The problem with this will be if you have unique values in database B for activityin that do not occur in database A.

Finally:

DROP TEMPORARY TABLE db2.ttable;

For completeness' sake.

很快妥协 2024-08-09 13:03:49

终于我明白了。

看来我只是没有开发出有助于提问的大脑部分。

这是结果

select distinct(  a.rolein ) , a.activityin, b.activityin 
from 
    taskperformance@dm_prod a,
    taskperformance b, 
    activities@dm_prod c,
    activities d
where b.rolein = 0
    and b.activityin = d.activityin 
    and d.activityid = c.activityid
    and c.activityin = a.activityin
order by b.activityin , a.activityin

谢谢 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

select distinct(  a.rolein ) , a.activityin, b.activityin 
from 
    taskperformance@dm_prod a,
    taskperformance b, 
    activities@dm_prod c,
    activities d
where b.rolein = 0
    and b.activityin = d.activityin 
    and d.activityid = c.activityid
    and c.activityin = a.activityin
order by b.activityin , a.activityin

Thank you JYelton, your questions helped me to understand my own question better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文