我還是新的SQL和讓我周圍的整個子查詢的聚集,顯示了一定的成效頭,並尋找一些建議:SQL聚合函數和排序
的表可能看起來像:
Customer: (custID, name, address)
Account: (accountID, reward_balance)
Shop: (shopID, name, address)
關係表:
Holds (custID*, accountID*)
With (accountID*, shopID*)
我怎樣才能找到具有至少reward_balance商店? (客戶信息沒有在這一點上需要)
我想:
SELECT accountID AS ACCOUNT_ID, shopID AS SHOP_ID, MIN(reward_balance) AS LOWEST_BALANCE
FROM Account, Shop, With
WHERE With.accountID = Account.accountID
AND With.shopID=Shop.shopID
GROUP BY
Account.accountID,
Shop.shopID
ORDER BY MIN(reward_balance);
這工作,因爲無意的方式:
ACCOUNT_ID | SHOP_ID | LOWEST_BALANCE
1 | 1 | 10
2 | 2 | 40
3 | 3 | 100
4 | 4 | 1000
5 | 4 | 5000
正如你可以看到Shop_ID 4實際上有6000(1000 + 5000)的餘額,因爲有兩個客戶註冊了它。我想我需要根據他們的平衡總結最低的商店餘額,並從低到高展示它。
我一直在試圖聚集在顯示之前的數據,但是這是我來脫膠:
SELECT shopID AS SHOP_ID, MIN(reward_balance) AS LOWEST_BALANCE
FROM (SELECT accountID, shopID, SUM(reward_balance)
FROM Account, Shop, With
WHERE
With.accountID = Account.accountID
AND With.shopID=Shop.shopID
GROUP BY
Account.accountID,
Shop.shopID;
當我運行這樣的說法,我得到一個無效的標識符錯誤。
Error at Command Line : 1 Column : 24
Error report -
SQL Error: ORA-00904: "REWARD_BALANCE": invalid identifier
00904. 00000 - "%s: invalid identifier"
所以我想我可能有我的加盟條件不正確,總分類不正確的,會很感激的任何一般建議。
感謝您的長時間閱讀!
嘗試在內部選擇內添加一個別名到SUM(reward_balance)作爲reward_balance'。 – Iztoksson
嘿Uporabnik003,我根據你的建議編輯它,但現在我得到「列明確定義」的錯誤。 – Ben
我不熟悉Oracle,但嘗試將它命名爲'sum_reward_balance',然後更正主要語句。也許它需要一個唯一的名稱,而不是原來的列名稱。 – Iztoksson