2015-08-26 293 views
0
SELECT 
t.trade_id,  
co.name,  
t.shares, 
se.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 AND t.shares =(SELECT MAX(t.shares) FROM trade ) 
GROUP BY t.trade_id, co.name, t.shares, se.name 
ORDER BY co.name 
; 

此查詢顯示重複的公司名稱以及不同的共享值。我的問題是,您如何刪除重複的公司名稱,並根據其重複項目顯示具有最大份額值的公司名稱?Oracle SQL - 刪除重複並保留具有最大值的行

我試着用這條線t.shares =(SELECT MAX(t.shares) FROM trade )來獲得最大份額值,但它似乎並不奏效。

下面是它的顯示錶的圖片:

enter image description here

+0

你想要顯示公司的最大股份價值不同的公司名稱? – Buddi

+0

是的。我想要擁有公司最大股票價值的獨特公司名稱。 –

+0

下面我發佈了一個查詢請檢查,並讓我知道在任何疑問的情況下。 – Buddi

回答

0

使用下面的查詢,將與最大的股票價值

select trade_id,name,shares,name_1 from 
    (
    SELECT 
    t.trade_id,  
    co.name as name, 
    t.shares, 
    se.name as name_1, 
    row_number() over (partition by co.name order by t.shares desc) as R_no 
    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 t.trade_id, co.name, t.shares, se.name 
    ORDER BY co.name 
    ) 
    where R_no=1 
+0

它不起作用。你在第一行列出了兩次'name'。嘗試使用我的查詢。我的老師實際上添加了類似't.shares =(SELECT MAX(t.shares)FROM trade)'的查詢來獲得結果(之後他將其刪除),我忘記了當我嘗試實現時添加的代碼它自己。大聲笑 –

0

不知道顯示的公司名稱,如果我創建了正確測試數據並瞭解要求:

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