2017-11-25 103 views
0

我試圖顯示從我的查詢中有3個表中的一個列中的列,但我似乎無法弄清楚如何讓它正常工作。無論我嘗試什麼,它似乎都不起作用。這似乎很容易,但也許我錯過了一些非常簡單的事情。以下是我有:如何在SQL查詢中顯示多個表中的列?

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 

我需要增加一個列:從hot_digity_dog表

TEST 

不要太擔心所有事情的名稱,但我只需要弄清楚我到底能夠在查詢中顯示另一列。我試着將TEST添加到第一個SELECT語句中,但它給了我一個錯誤:「TEST:無效標識符」。

我知道查詢的工作原理(沒有添加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 
+0

請出示您添加其他列的查詢。 –

+0

嘗試添加適當的數據樣本和預期結果 – scaisEdge

+0

您從不選擇hdd查詢中的TEST列。在那裏選擇它,以及'number_of_games,price,prints',我懷疑它會起作用。 – Zorkolot

回答

0

的問題是,你是不是從hot_digity_dog的選擇外部查詢。您可以從基於該表的AGGREGATE子查詢中進行選擇 - 您以別名hdd1作爲別名的子查詢。所以,目前尚不清楚如何你想添加那個列。 test完全由number_of_games決定?如果是,則可以將test添加到聚合子查詢的selectgroup by子句中,然後可以在外部查詢中選擇它。

如果test不是由number_of_games確定,你要總結number_of_foodnumber_of_gamestest爲分組?如果是這樣,再次在子查詢中添加testselectgroup by(但結果將單獨由number_of_games分組)。

如果test不是由number_of_games確定,要通過組爲number_of_games的總和,但要顯示test無論如何,你可能需要使用分析sum()而不是總sum()

這只是一個小例子,向您證明您發佈的內容不足以讓我們來幫助您。請提供(更多)更多細節 - 通過編輯原始問題,而不是在評論中。

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 
相關問題