选择语句连接两个具有两个键的表

时间:2014-12-03 00:53:37

标签: sql tsql

我正在尝试获取一个显示此活动表中值的总和的正确记录,此外它还要求我与两个键值保持绝对匹配。

这是我正在尝试做的粗略草图......

enter image description here

考虑到这一点,我可能会重复使用UserID的值,因为我有3个品牌。

因此,F为1,M为1,每个品牌值为J为1。

我目前提出的存储过程是这样的......

Select datename(month, A.ActivityTime), U.UserID, U.FirstName, U.LastName, Sum(A.Steps) as [Total Steps], Sum(A.Miles) as [Total Miles], Sum(A.MinutesActive) as [Total Minutes]
from Tracker.Activities as A join Tracker.Users as U on (U.BrandID = A.BrandID) and (U.UserID = A.UserID)
where Month(ActivityTime) = @month
group by datename(month, A.ActivityTime), U.FirstName, U.LastName, U.UserID
order by [Total Miles] desc

并且应显示一个表格,显示由UserID和BrandID分隔的每个列的总计。

以下是我从中提取信息的两张表的图片供参考。

enter image description here

0 个答案:

没有答案