2013-06-19 62 views
0

我試圖獲得與一個國家的賬戶相關的最長和最短的1年時間。這些數據是從一個表中提取的。MySQL MAX與第二列相關

對於一個國家,我將擁有帳戶一的最高帳戶回報和帳戶二的最低帳戶回報。所以每個國家1個結果。

我有以下但它不能正常工作,它實際上爲我提供了來自不正確帳戶的最高和最低值,因爲它應該只與擁有1年時間範圍的帳戶一起工作。

也忘了補充說明dpromo_one的總體結果,只對這些國家例如('美國','英國','南非','印度','澳大利亞')只要。它剛剛變得非常複雜,它讓我頭腦發熱。

SELECT DISTINCT acc2.account_name AS account_one, acc5.account_name AS account_two, 
    MAX(acc2.dpromo_rate) AS dpromo_one, MIN(acc5.dpromo_rate) AS dpromo_two, 
    acc2.deposit_term, acc2.country 
FROM accounts acc2 
INNER JOIN accounts acc5 ON acc2.country = acc5.country 
WHERE acc2.type =2 
AND acc5.type =2 
AND acc2.deposit_term = '1 Year' 
GROUP BY country 

整體輸出例如可能是1號線以下 :

Country Bank Highest Bank    Lowest 
USA  BOFA 1yr  1%  Wells Fargo 1yr 0.5% 
UK  HSBC 1yr  0.5% Halifax 1yr  0.25% 
Australia CBA 1yr  0.4% NAB 1yr   0.1% 

例如賬表有例如是相關

ACCOUNT_NAME 國家 dpromo_rate deposit_term以下字段

請注意,我們一個重新擁有帳戶和價格並排。我的代碼這樣做,但不正確,雖然這就是爲什麼它也解釋了爲什麼我有別名重複的字段名稱。

+0

您想要返回哪個其他字段?例如,我假定有一個國家有多個account_names,但由於該組只有一個(未指定)將被返回。你能發表一些樣本數據和你想輸出的內容嗎? – Kickstart

+0

第1行的總體產出示例應如下所示:美國BOFA 1%富國銀行0.5%和第2行可能是英國匯豐0.5%哈利法克斯0.25%和第3行澳大利亞CBA 0.4%NAB 0.1% –

+0

並且輸入數據給出線? – Kickstart

回答

0

它正在做究竟是 MySQL說它會做什麼。 group by子句中未包含的列具有任意值。在大多數其他數據庫中,查詢只會因語法錯誤而失敗。

這裏有個竅門,以獲得帳戶的名稱:

SELECT substring_index(group_concat(acc2.account_name order by acc2.dpromo_rate desc), ',', 1) AS account_one, 
     substring_index(group_concat(acc5.account_name order by acc5.dpromo_rate asc), ',', 1) AS account_two, 
     MAX(acc2.dpromo_rate) AS dpromo_one, MIN(acc5.dpromo_rate) AS dpromo_two, 
     acc2.deposit_term, acc2.country 
FROM accounts acc2 
INNER JOIN accounts acc5 ON acc2.country = acc5.country 
WHERE acc2.type =2 AND acc5.type =2 AND acc2.deposit_term = '1 Year' 
GROUP BY country 

我認爲你可以簡化查詢到一個簡單的聚集。我不明白爲什麼你在做一個連接:

SELECT substring_index(group_concat(acc.account_name order by acc.dpromo_rate desc), ',', 1) AS account_one, 
     substring_index(group_concat(acc.account_name order by acc.dpromo_rate asc), ',', 1) AS account_two, 
     MAX(acc.dpromo_rate) AS dpromo_one, MIN(acc.dpromo_rate) AS dpromo_two, 
     acc.deposit_term, acc.country 
FROM accounts acc 
WHERE acc.type = 2 and acc.deposit_term = '1 Year' 
GROUP BY country; 

如果您打算在存款期限只適用於max,然後替換爲最大:

max(case when acc.deposit_term = '1 Year' then acc.dpromo_rate end) as dpromo_one 
+0

這是從以前的代碼疏忽,我已經修改了有關信息,我希望它幫助了 –

+0

@JohnSmith。 。 。我從我的查詢中刪除它。這對你有用嗎? –

+0

不幸的是我得到了#1052 - 字段列表中的'dpromo_rate'列是不明確的,我已經提供了實際表結構(只有相關的)的進一步信息才能提供幫助。請看原始帖子 –

0

我的事情這將給結果

select max(dpromo_rate), min(dpromo_rate), country, account_name, provider from accounts 
where deposit_term = '1 Year' group by country, provider, account_name 
+0

感謝以及雖然得到錯誤,當我嘗試 –

0

爲了讓你的基礎在輸出例如: -

SELECT DISTINCT z.county, b.account_name, b.dpromo_rate, d.account_name, d.dpromo_rate 
FROM accounts z 
INNER JOIN (SELECT country, type, MAX(dpromo_rate) AS MaxRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) a 
ON z.country = a.country AND z.type = a.type 
INNER JOIN accounts b 
ON a.country = b.country and a.MaxRate = b.dpromo_rate AND a.type = b.type 
INNER JOIN (SELECT country, type, MIN(dpromo_rate) AS MinRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) c 
ON z.country = c.country AND z.type = c.type 
INNER JOIN accounts d 
ON c.country = d.country and c.MinRate = d.dpromo_rate AND c.type = d.type 

這只是獲取國家,帳戶名稱與最大利率,實際最高利率,帳戶名稱與最低利率和實際最低利率。

不確定在輸出中需要存款期限和提供者,但它們很容易從b或d別名表中獲得。

請注意,如果您在一個國家擁有多個帳戶,並且所有帳戶都分享最高或最低費率,這會造成混亂。

要限制它的最大速度的幾個國家和秩序: -

SELECT DISTINCT z.county, b.account_name, b.dpromo_rate, d.account_name, d.dpromo_rate 
FROM accounts z 
INNER JOIN (SELECT country, type, MAX(dpromo_rate) AS MaxRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) a 
ON z.country = a.country AND z.type = a.type 
INNER JOIN accounts b 
ON a.country = b.country and a.MaxRate = b.dpromo_rate AND a.type = b.type 
INNER JOIN (SELECT country, type, MIN(dpromo_rate) AS MinRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) c 
ON z.country = c.country AND z.type = c.type 
INNER JOIN accounts d 
ON c.country = d.country and c.MinRate = d.dpromo_rate AND c.type = d.type 
WHERE z.country IN ('united states', 'united kingdom', 'south africa', 'india', 'australia') 
ORDER BY b.dpromo_rate 

將其限制在每個國家之一,那麼你可以這樣做: -

SELECT z.county, b.account_name, b.dpromo_rate, d.account_name, d.dpromo_rate 
FROM accounts z 
INNER JOIN (SELECT country, type, MAX(dpromo_rate) AS MaxRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) a 
ON z.country = a.country AND z.type = a.type 
INNER JOIN accounts b 
ON a.country = b.country and a.MaxRate = b.dpromo_rate AND a.type = b.type 
INNER JOIN (SELECT country, type, MIN(dpromo_rate) AS MinRate FROM accounts WHERE type = 2 AND deposit_term = '1 Year' GROUP BY country, type) c 
ON z.country = c.country AND z.type = c.type 
INNER JOIN accounts d 
ON c.country = d.country and c.MinRate = d.dpromo_rate AND c.type = d.type 
WHERE z.country IN ('united states', 'united kingdom', 'south africa', 'india', 'australia') 
GROUP BY z.county 
ORDER BY b.dpromo_rate 

注意,如果一個國家的2個賬戶具有相同的匯率,這是該國家的最高匯率,那麼只有一個賬戶可以退貨。哪一個返回是不確定的。

+0

請注意你錯過了我的代碼上面.....和acc2.deposit_term ='1年',這是什麼使這些獨特的...你能檢查出原來的職位,因爲我有 –

+0

編輯添加額外的檢查 – Kickstart

+0

實際上它的工作原理,謝謝你,我不知道我們是否可以通過最高dpromo_one的整體結果對數據進行排序,並只做這些國家,如國家('美國' ,「英國」,「南非」,「印度」,「澳大利亞」)。 –