我使用DB2和有两个表:SQL DB2计算模式
用户:
- ID - 关键
- 教育(整数值表示级)
- 年龄
付款方式:
- 店
- 总和
- ID - 外键
我要计算教育的统计模式,每家商店,现在我想类似的东西: 1)计算店X模式:
select u.education
from users u, payments p
where u.id = p.id AND
p.shop = 'X'
group by u.education order by count(*) desc
fetch first 1 rows only;
此查询工作正常
2)计算教育的模式,每店:
select p.Shop as Shop,
avg(u.age) as AvgAge,
(select u1.education
from users u1
where u1.id = p.id
group by u1.education
order by count(*) desc
fetch first 1 rows only) as ModeEdu
from users u, payments p
where u.id = p.id
group by p.Shop;
这个查询给出了一个错误:
SQLCODE = -119,ERROR:确定了具有 子句中的列或表达式无效
为什么平均年龄包含在查询中?你能显示一些样本数据和预期的输出吗? –
@vkp我包括年龄,因为我也试图获得一些关于用户的其他信息,但在选择教育模式方面存在问题。 – MRMKR
@vkp你想要看什么样品? – MRMKR