我有两张桌子,一张是"活动"表
UserId ActionID companyId
2971 1 334
2971 4 334
3343 2 124
4416 3 231
4416 3 231
另一个是" ActionType" table,包含以下列:
ActionID ActionTypeName
1 Download
2 Buy
3 View
4 Click
我应该如何构建我的SQL以使每个用户获得每个不同的操作,名称及其计数?
我想要的结果是:
UserId ActionId ActionTypeName ActionCount
2971 1 Download 1
2971 4 Click 1
3343 2 Buy 1
4416 3 View 2
真的很难用SQL。
感谢您的帮助
答案 0 :(得分:3)
您可以使用以下内容:
SELECT a.UserId, a.ActionId, b.ActionTypeName, COUNT(*) AS ActionCount
FROM Activity a
INNER JOIN ActionType b
ON a.ActionId=b.ActionId
GROUP BY a.UserId, a.ActionId, b.ActionTypeName
答案 1 :(得分:0)
使用group by子句计算如下:
Select u.userid, a.actionid,a.actiontypename , count(actionid)
from activity u
inner join actiontype a
on a.actionid = b.actionid
group by u.userid, a.actionid,a.actiontypename