如何在SQL查询中显示多个表中的列?

时间:2017-11-25 20:27:21

标签: sql oracle multiple-columns toad

我试图在查询中显示3个表中的一个列,但我似乎无法弄清楚如何让它正常工作。无论我尝试什么,它似乎都不起作用。这似乎很容易,但也许我错过了一些非常简单的事情。这就是我所拥有的:

SELECT hpg.id_number,
     hpg.id_description,
     COUNT (hdd.number_of_games) games,
     SUM (NVL (price, 0)) total,
     SUM (number_of_food) food
--I tried adding column here, but nothing seemed to work
FROM (SELECT number_of_games, price, prints
        FROM (hopeful_dog_hops)
       WHERE status = 'Done') hdd,
     (SELECT SUM (number_of_food) number_of_food, number_of_games
          FROM hot_digity_dog
      GROUP BY number_of_games) hdd1,
     (SELECT id_description, sort_types, id_number
        FROM (hot_pick_games)
       WHERE disabled = 'TRUE' AND viewable_type = 'OK') hpg
WHERE hdd.prints(+) = hpg.id_number
     AND hdd.number_of_games = hdd1.number_of_games
GROUP BY hpg.id_description, hpg.id_number, sort_types
ORDER BY sort_types

这个巨大的查询显示以下列:

ID_NUMBER    ID_DESCRIPTION    GAMES    TOTAL    FOOD

我需要再添加一列:

TEST
来自hot_digity_dog表的

不要过于担心所有内容的名称,但我只需要弄清楚如何在查询中显示另一列。我尝试将TEST添加到第一个SELECT语句,但它给了我一个错误:“TEST:invalid identifier。”

我知道查询有效(没有添加TEST部分,因此它不能成为查询中的错误)。它必须是新添加的部分。

非常感谢任何帮助。提前谢谢。

以下是使用新列的查询:

SELECT hpg.id_number,
     hpg.id_description,
     COUNT (hdd.number_of_games) games,
     SUM (NVL (price, 0)) total,
     SUM (number_of_food) food,
     hdd.TEST
FROM (SELECT number_of_games, price, prints
        FROM (hopeful_dog_hops)
       WHERE status = 'Done') hdd,
     (SELECT SUM (number_of_food) number_of_food, number_of_games
          FROM hot_digity_dog
      GROUP BY number_of_games) hdd1,
     (SELECT id_description, sort_types, id_number
        FROM (hot_pick_games)
       WHERE disabled = 'TRUE' AND viewable_type = 'OK') hpg
WHERE hdd.prints(+) = hpg.id_number
     AND hdd.number_of_games = hdd1.number_of_games
GROUP BY hpg.id_description, hpg.id_number, sort_types
ORDER BY sort_types

以下是列标题的样子:

ID_NUMBER    ID_DESCRIPTION    GAMES    TOTAL    FOOD    TEST

2 个答案:

答案 0 :(得分:1)

我认为你只是错过了hdd查询中的测试列:

SELECT hpg.id_number,
     hpg.id_description,
     COUNT (hdd.number_of_games) games,
     SUM (NVL (price, 0)) total,
     SUM (number_of_food) food,
     hdd.TEST
FROM (SELECT number_of_games, price, prints, TEST
        FROM (hopeful_dog_hops)
       WHERE status = 'Done') hdd,
     (SELECT SUM (number_of_food) number_of_food, number_of_games
          FROM hot_digity_dog
      GROUP BY number_of_games) hdd1,
     (SELECT id_description, sort_types, id_number
        FROM (hot_pick_games)
       WHERE disabled = 'TRUE' AND viewable_type = 'OK') hpg
WHERE hdd.prints(+) = hpg.id_number
     AND hdd.number_of_games = hdd1.number_of_games
GROUP BY hpg.id_description, hpg.id_number, sort_types
ORDER BY sort_types

答案 1 :(得分:0)

问题是您没有从外部查询中的hot_digity_dog中进行选择。您可以从基于该表的AGGREGATE子查询中进行选择 - 您将别名为hdd1的子查询。因此,您不希望添加该列 test是否number_of_games完全确定了test?如果是,则可以将select添加到聚合子查询的group bytest子句中,然后可以在外部查询中选择它。

如果number_of_games未确定number_of_food,您是否要将number_of_games归为test test ?如果是这样,请再次在子查询中将select添加到group bynumber_of_games(但结果与仅test分组的结果不同。)

如果number_of_games未确定number_of_games,您希望按test分组,但是您想要显示sum(),您可能想要使用分析 sum()而不是汇总@Override @Transactional public void saveTemporaryRecords(List<TemporaryRecord> temporaryRecords) { for (TemporaryRecord temporaryRecord : temporaryRecords) { try { temporaryRecordDao.persist(temporaryRecord); } catch (Exception e) { e.printStackTrace(); } } }

这只是一个小例子,向您证明您发布的内容不足以让我们帮助您。请提供(更多)更多细节 - 通过编辑原始问题,而不是评论。