我正在尝试获取与某个国家/地区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 deposit_term
请注意,我们同时拥有帐户和费率。我的代码虽然这样做但是不正确,这就是为什么它也解释了为什么我有重复字段名称的别名。
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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个帐户具有相同的费率,这是该国家/地区的最高费率,那么只会返回一个帐户。返回哪一个不确定。