我使用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