SQL - 从连接表中提取随机样本

发布于 2024-12-10 11:30:25 字数 1332 浏览 0 评论 0原文

我必须用 TAB_B 中的随机记录 (5%) 填充一个空表 (TAB_A),并与其他 3 个表(C1、C2、C3)连接。

现在,如果我运行以下查询,一切正常:

INSERT INTO TAB_A (field1,field2)
    SELECT TAB_B.ID_TASS, TAB_B.ID_SEZ
    FROM (SELECT TAB_B.*, row_number() 
            OVER (ORDER BY dbms_random.VALUE) r 
            FROM TAB_B) 
    WHERE r < (SELECT COUNT(*) FROM TAB_B)*0.05

问题是我还需要填写 TAB_A.field3,其值位于 TAB_C3 中。 所以我重写了之前的查询:

INSERT INTO TAB_A (field1,field2,field3)
    SELECT B.ID_TASS, B.ID_SEZ, C3.Z_ID
    FROM (SELECT B.*, row_number() 
            OVER (ORDER BY dbms_random.VALUE) r 
            FROM TAB_B B
                JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
                JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
                JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
            WHERE C3.Z_ID = 9) 
    WHERE r < (SELECT COUNT(*) FROM B)*0.05

但是出现错误:ORA-00904:“C3.Z_ID”无效标识符。

我认为问题可能出在我在“SELECT TAB_B.ID_TASS, TAB_B.ID_SEZ, TAB_C3”中更改的语句“SELECT B.ID_TASS, B.ID_SEZ, C3.Z_ID”中。 Z_ID”....但没有成功:同样的错误再次发生。

顺便说一句,以下查询完美运行:

select count(*)
    from TAB_B B
        JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
        JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
        JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
    WHERE C3.Z_ID = 9

有人可以帮助我吗?

I've to fill an empty table (TAB_A) with random records (5%) from TAB_B, joined with other 3 tables (C1, C2, C3).

Now, if I run the following query everything works well:

INSERT INTO TAB_A (field1,field2)
    SELECT TAB_B.ID_TASS, TAB_B.ID_SEZ
    FROM (SELECT TAB_B.*, row_number() 
            OVER (ORDER BY dbms_random.VALUE) r 
            FROM TAB_B) 
    WHERE r < (SELECT COUNT(*) FROM TAB_B)*0.05

The problem is that I need also to fill in the TAB_A.field3 whose values are in TAB_C3.
So I rewrite my previous query:

INSERT INTO TAB_A (field1,field2,field3)
    SELECT B.ID_TASS, B.ID_SEZ, C3.Z_ID
    FROM (SELECT B.*, row_number() 
            OVER (ORDER BY dbms_random.VALUE) r 
            FROM TAB_B B
                JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
                JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
                JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
            WHERE C3.Z_ID = 9) 
    WHERE r < (SELECT COUNT(*) FROM B)*0.05

but an error occurs: ORA-00904: "C3.Z_ID" invalid identifier.

I think the problem could be in the statement "SELECT B.ID_TASS, B.ID_SEZ, C3.Z_ID", that I changed in "SELECT TAB_B.ID_TASS, TAB_B.ID_SEZ, TAB_C3.Z_ID".... but without success: the same error occurs again.

By the way, the following query works perfectely:

select count(*)
    from TAB_B B
        JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
        JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
        JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
    WHERE C3.Z_ID = 9

Can anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风和你 2024-12-17 11:30:25

尝试

...

SELECT B.ID_TASS, B.ID_SEZ, C3_Z_ID

FROM (SELECT B.*, C3.Z_ID AS C3_Z_ID, row_number()
...

try

...

SELECT B.ID_TASS, B.ID_SEZ, C3_Z_ID

FROM (SELECT B.*, C3.Z_ID AS C3_Z_ID, row_number()
...

不甘平庸 2024-12-17 11:30:25

尽管我还没有测试过,以下应该可以工作

INSERT INTO TAB_A (field1,field2,field3)
>     SELECT intTab.ID_TASS, intTab.ID_SEZ, intTab.Z_ID
>     FROM (SELECT B.*, c3.z_id,row_number() 
>             OVER (ORDER BY dbms_random.VALUE) r 
>             FROM TAB_B B
>                 JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
>                 JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
>                 JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
>             WHERE C3.Z_ID = 9) intTab
>     WHERE intTab.r < (SELECT COUNT(*) FROM B)*0.05

The following should work although I havent tested

INSERT INTO TAB_A (field1,field2,field3)
>     SELECT intTab.ID_TASS, intTab.ID_SEZ, intTab.Z_ID
>     FROM (SELECT B.*, c3.z_id,row_number() 
>             OVER (ORDER BY dbms_random.VALUE) r 
>             FROM TAB_B B
>                 JOIN TAB_C1 C1 on C1.X_ID = B.X_ID
>                 JOIN TAB_C2 C2 on C2.Y_ID = C1.Y_ID
>                 JOIN TAB_C3 C3 on C3.Z_ID = C2.Z_ID
>             WHERE C3.Z_ID = 9) intTab
>     WHERE intTab.r < (SELECT COUNT(*) FROM B)*0.05
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文