不知道顯示的公司名稱,如果我創建了正確測試數據並瞭解要求:
WITH trade AS (
SELECT 31 TRADE_ID, 1 STOCK_ID, 1 STOCK_EX_ID, 4000 SHARES FROM DUAL UNION ALL
SELECT 52 TRADE_ID, 1 STOCK_ID, 1 STOCK_EX_ID, 1000 SHARES FROM DUAL UNION ALL
SELECT 13 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 2300 SHARES FROM DUAL UNION ALL
SELECT 18 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 72000 SHARES FROM DUAL UNION ALL
SELECT 23 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 24000 SHARES FROM DUAL UNION ALL
SELECT 25 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 400 SHARES FROM DUAL UNION ALL
SELECT 39 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 2300 SHARES FROM DUAL UNION ALL
SELECT 44 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 25000 SHARES FROM DUAL UNION ALL
SELECT 46 TRADE_ID, 2 STOCK_ID, 2 STOCK_EX_ID, 400 SHARES FROM DUAL UNION ALL
SELECT 16 TRADE_ID, 3 STOCK_ID, 1 STOCK_EX_ID, 1500 SHARES FROM DUAL UNION ALL
SELECT 17 TRADE_ID, 3 STOCK_ID, 1 STOCK_EX_ID, 24000 SHARES FROM DUAL UNION ALL
SELECT 21 TRADE_ID, 3 STOCK_ID, 1 STOCK_EX_ID, 1500 SHARES FROM DUAL
),
company AS (
SELECT 1 STOCK_ID, 'BNP Paribas' NAME FROM DUAL UNION ALL
SELECT 2 STOCK_ID, 'British Airways' NAME FROM DUAL UNION ALL
SELECT 3 STOCK_ID, 'EDF' NAME FROM DUAL
),
stock_exchange AS (
SELECT 1 STOCK_EX_ID, 'Euronext Paris' NAME FROM DUAL UNION ALL
SELECT 2 STOCK_EX_ID, 'London Stock Exchange' NAME FROM DUAL
)
SELECT
MAX(trade_id) KEEP (DENSE_RANK LAST ORDER BY shares) trade_id,
co.name,
MAX(shares) maxshares,
MAX(se.name) KEEP (DENSE_RANK LAST ORDER BY shares) stock_exchange_name
FROM
trade t
JOIN company co ON co.stock_id = t.stock_id
JOIN stock_exchange se ON se.stock_ex_id = t.stock_ex_id
WHERE
se.stock_ex_id IS NOT NULL
GROUP BY
co.name
ORDER BY
co.name
你想要顯示公司的最大股份價值不同的公司名稱? – Buddi
是的。我想要擁有公司最大股票價值的獨特公司名稱。 –
下面我發佈了一個查詢請檢查,並讓我知道在任何疑問的情況下。 – Buddi