0

我正在尝试获取与一个国家/地区 1 年时间范围内的帐户相关联的最高和最低值。此数据是从一个表中提取的。

对于一个国家,我将获得账户一的最高账户回报和账户二的最低账户回报。所以每个国家有 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% 

例如,accounts 表具有以下相关字段,例如

account_name 国家 dpromo_rate deposit_term

请注意,我们同时拥有帐户和费率。我的代码这样做但不正确,这就是为什么它也解释了为什么我有重复字段名称的别名。

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

要将其限制为每个国家/地区一个,您可以这样做:-

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 个帐户具有相同的费率,这是该国家/地区的最高费率,则只会返回一个。返回哪个是不确定的。

于 2013-06-19T13:49:56.810 回答
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 回答
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 回答