0

ある国の 1 年間のアカウントに関連付けられた最高値と最低値を取得しようとしています。このデータは 1 つのテーブルから取得されます。

ある国の口座 1 の口座収益率が最も高く、口座 2 の口座収益率が最も低くなります。したがって、国ごとに 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 預金_期間

アカウントとレートの両方が並んでいることに注意してください。私のコードはこれを行いますが、間違っています。そのため、重複したフィールド名のエイリアスがある理由も説明しています。

4

3 に答える 3

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

国ごとに 1 つに制限するには、次のようにします。

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 つのアカウントのレートが同じで、そのレートがその国の最高レートである場合は、1 つのみが返されることに注意してください。どちらが返されるかは定かではありません。

于 2013-06-19T13:49:56.810 に答える
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
于 2013-06-19T13:39:29.913 に答える
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 を次のように置き換えます。

max(case when acc.deposit_term =  '1 Year' then acc.dpromo_rate end) as dpromo_one
于 2013-06-19T13:34:49.013 に答える