SQL查询:迭代表中的值并在子查询中使用它们

我有一个包含一些值的简单SQL表,例如:

id | value    (table 'values')
----------
0  | 4
1  | 7
2  | 9

我想迭代这些值,并在这样的查询中使用它们:

SELECT value[0], x1
FROM (some subquery where value[0] is used)

UNION

SELECT value[1], x2
FROM (some subquery where value[1] is used) 

...
etc

为了获得这样的结果集:

4 | x1
7 | x2
9 | x3

它必须在SQL中,因为它实际上代表数据库视图.当然真正的查询要复杂得多,但我试图在尽可能保持本质的同时简化问题.

我想我必须从值中选择并加入子查询,但是因为值应该在子查询中使用,所以我对如何实现这一点感到迷茫.

编辑:我过分简化了我的问题;实际上我想从子查询中获得2行而不仅仅是一行.

编辑2:正如建议我发布真实的查询.我简化了一下以使其更清晰,但它是一个有效的查询,问题就在那里.请注意,我已在此查询中硬编码值’2’两次.我想用来自不同表的值替换它,在上面的示例表中,我希望此查询的组合结果的结果集以4,7和9作为值而不是当前硬编码的2.

SELECT x.fantasycoach_id, SUM(round_points)
FROM (
    SELECT DISTINCT fc.id AS fantasycoach_id,
        ffv.formation_id AS formation_id,
        fpc.round_sequence AS round_sequence,
        round_points,
        fpc.fantasyplayer_id
    FROM fantasyworld_FantasyCoach AS fc

    LEFT JOIN fantasyworld_fantasyformation AS ff ON ff.id = (
            SELECT MAX(fantasyworld_fantasyformationvalidity.formation_id)
            FROM fantasyworld_fantasyformationvalidity
            LEFT JOIN realworld_round AS _rr ON _rr.id = round_id
            LEFT JOIN fantasyworld_fantasyformation AS _ff ON _ff.id = formation_id
            WHERE is_valid = TRUE
                AND _ff.coach_id = fc.id
                AND _rr.sequence <= 2 /* HARDCODED USE OF VALUE */
            )
    LEFT JOIN fantasyworld_FantasyFormationPlayer AS ffp 
    ON ffp.formation_id = ff.id

    LEFT JOIN dbcache_fantasyplayercache AS fpc 
    ON ffp.player_id = fpc.fantasyplayer_id
    AND fpc.round_sequence = 2 /* HARDCODED USE OF VALUE */

    LEFT JOIN fantasyworld_fantasyformationvalidity AS ffv 
    ON ffv.formation_id = ff.id
) x
GROUP BY fantasycoach_id

编辑3:我正在使用PostgreSQL.

最佳答案
SQL与表一起使用,基本上涉及集合操作.没有明确的迭代,通常不需要任何迭代.特别是,您所描述的最简单的实现方式是:

SELECT value, (some subquery where value is used) AS x
FROM values

但请注意,诸如此类的相关子查询对查询性能非常困难.根据您尝试执行的操作的详细信息,可以围绕简单连接,不相关的子查询或类似的,性能更好的替代方案来构建它.

更新:

鉴于对问题的更新表明子查询预计会为表值中的每个值产生多行,与示例结果相反,似乎更好的方法是将子查询重写为主查询.如果它还没有这样做(也许即使它已经这样做)那么它会将表值作为另一个基表连接起来.

更新2:

鉴于现在提供的真实查询,这是表值的值可以如何合并到其中:

SELECT x.fantasycoach_id, SUM(round_points) FROM
(
    SELECT DISTINCT
        fc.id AS fantasycoach_id, 
        ffv.formation_id AS formation_id, 
        fpc.round_sequence AS round_sequence,
        round_points,
        fpc.fantasyplayer_id

    FROM fantasyworld_FantasyCoach AS fc
    -- one row for each combination of coach and value:
    CROSS JOIN values

    LEFT JOIN fantasyworld_fantasyformation AS ff
    ON ff.id = (
        SELECT MAX(fantasyworld_fantasyformationvalidity.formation_id)
        FROM fantasyworld_fantasyformationvalidity

        LEFT JOIN realworld_round AS _rr
        ON _rr.id = round_id

        LEFT JOIN fantasyworld_fantasyformation AS _ff
        ON _ff.id = formation_id

        WHERE is_valid = TRUE
        AND _ff.coach_id = fc.id

        -- use the value obtained from values:
        AND _rr.sequence <= values.value
    )

    LEFT JOIN fantasyworld_FantasyFormationPlayer AS ffp
    ON ffp.formation_id = ff.id

    LEFT JOIN dbcache_fantasyplayercache AS fpc
    ON ffp.player_id = fpc.fantasyplayer_id

    -- use the value obtained from values again:
    AND fpc.round_sequence = values.value

    LEFT JOIN fantasyworld_fantasyformationvalidity AS ffv
    ON ffv.formation_id = ff.id
) x
GROUP BY fantasycoach_id

特别注意CROSS JOIN,它构成了两个表的交叉积;这与没有任何连接谓词的INNER JOIN是一样的,如果需要,它可以用这种方式编写.

总体查询可能至少有点简化,但我不这样做,因为它是一个工作示例而不是实际的生产查询,因此不清楚其他更改将转化为实际应用程序.

转载注明原文:SQL查询:迭代表中的值并在子查询中使用它们 - 代码日志