如何从表中选择行以添加分数

时间:2015-11-17 22:19:14

标签: sql

我有以下数据:

Score   activity     user
2      123BE54A342B B51450EDEE7B
2      123BE54A342B B51450EDEE7B
2      FB45EC5D0643 B51450EDEE7B
2      FB45EC5D0643 C1F859FE020A

我想要做的是编写一个查询,返回用户完成相同活动的分数

案例1:换句话说,这个数据:

Score   activity     user
2      123BE54A342B B51450EDEE7B
2      123BE54A342B B51450EDEE7B
2      FB45EC5D0643 B51450EDEE7B
2      FB45EC5D0643 C1F859FE020A

活动" FB45EC5D0643"获得2分。

活动" 123BE54A342B"没有报告只有1个用户完成它

此外,活动可以通过" m"用户数和" n"每个用户的次数,但只有在" m"用户已完成相同数量的活动。

案例2:换句话说,这个数据:

score  activity      user
2      FB45EC5D0643 B51450EDEE7B
2      FB45EC5D0643 B51450EDEE7B
2      FB45EC5D0643 B51450EDEE7B
2      FB45EC5D0643 C1F859FE020A
2      FB45EC5D0643 C1F859FE020A
2      FB45EC5D0643 FDEF34772AAA
2      FB45EC5D0643 FDEF34772AAA
2      FB45EC5D0643 FDEF34772AAA

得分总和为4(活动仅由所有用户完成两次)

如果你能提供一个SQL语句来让我朝着正确的方向前进,我们将不胜感激。

哦......顺便说一句......分数和活动来自"活动"表和用户来自"用户"表。他们是如何联系的是" User_Activity" table,包含活动的索引和用户的索引

像这样:

Activity    User_Activity    User
Indx        Indx_Activity    Indx
            Indx_User

非常感谢你!

根据评论:

SELECT SUM(Score)
    ,Activity.Indx AS 'activity'
    ,[User].Indx AS 'user'
FROM Activity
INNER JOIN User_Activity ON Activity.Indx = User_Activity.Indx_Activity
INNER JOIN [User] ON User_Activity.Indx_User = [User].Indx
INNER JOIN Team_Users ON [User].Indx = Team_Users.Indx_User
WHERE Team_Users.Indx_Team = 'C1401D43-7154-4E92-BA0B-B491137E998D'
    AND Activity.Indx_ActivityDistribution = dbo.Indx_ActivityDistribution_Team()
GROUP BY Activity.Indx
    ,[User].Indx

。 以上没有用......

试过这个:

SELECT sum(score), Activity.Indx as 'activity',  [User].Indx as 'user'
FROM         Activity INNER JOIN
                  User_Activity ON Activity.Indx = User_Activity.Indx_Activity INNER JOIN
                  [User] ON User_Activity.Indx_User = [User].Indx INNER JOIN
                  Team_Users ON [User].Indx = Team_Users.Indx_User
          where Team_Users.Indx_Team = 'C1401D43-7154-4E92-BA0B-B491137E998D'
          and Activity.Indx_ActivityDistribution = dbo.Indx_ActivityDistribution_Team()
group by Activity.Indx, [User].Indx
having count ([User].Indx) > 1

以下是它运行的数据:

Score  activity user
1   FB45EC5D0643    C1F859FE020A
1   123BE54A342B    B51450EDEE7B
1   123BE54A342B    B51450EDEE7B
1   FB45EC5D0643    B51450EDEE7B

这是它返回的内容:

2   123BE54A342B    A6F2-B51450EDEE7B
非常感谢xQbert! 这是工作查询:

SELECT avg(score), Activity.Indx as 'activity'
FROM         Activity INNER JOIN
                  User_Activity ON Activity.Indx = User_Activity.Indx_Activity INNER JOIN
                  [User] ON User_Activity.Indx_User = [User].Indx INNER JOIN
                  Team_Users ON [User].Indx = Team_Users.Indx_User
          where Team_Users.Indx_Team = 'C1401D43-7154-4E92-BA0B-B491137E998D'
          and Activity.Indx_ActivityDistribution = dbo.Indx_ActivityDistribution_Team()
group by Activity.Indx
having count (distinct [User].Indx) > 1

更新

这是最终查询:

    select @Temp = MIN(CNT) * UAScore
    from
    (

        SELECT [user].Indx as 'UserIndx', Activity.Indx as 'ActivityIndx', User_Activity.Score as 'UAscore', count(*) as CNT
        FROM  Activity INNER JOIN
                User_Activity ON Activity.Indx = User_Activity.Indx_Activity INNER JOIN
                [User] ON User_Activity.Indx_User = [User].Indx INNER JOIN
                Team_Users ON [User].Indx = Team_Users.Indx_User INNER JOIN
                Team ON Team_Users.Indx_Team = Team.Indx INNER JOIN
                Event_Activity ON Activity.Indx = Event_Activity.Indx_Activity INNER JOIN
                User_ActivityStatus ON User_Activity.Indx_User_ActivityStatus = User_ActivityStatus.Indx          
        WHERE 
                rank = dbo.User_ActivityStatus_Finished()
                and Team_Users.Indx_Team = @Indx_Team
                and Event_Activity.Indx_Event = @Indx_Event
                and Activity.Indx_ActivityDistribution = dbo.Indx_ActivityDistribution_Team()
        group by Activity.Indx, [user].indx, User_Activity.Score

    )t
    group by ActivityIndx, UAscore

2 个答案:

答案 0 :(得分:1)

使用聚合查询返回用户,活动和分数的用户,活动,计数和分数分组。将其输入另一个返回活动的聚合和计数的最小值乘以活动和分数的分数分组。

    SELECT activity,
            MIN(CNT) * score
    FROM (
            SELECT user, activity, score, COUNT(*) AS CNT
            FROM Table
            GROUP BY user, activity, score
    ) t
    GROUP BY activity, score
    HAVING COUNT(DISTINCT user) > 1

答案 1 :(得分:0)

从select和group by中删除了user.indx。如果您需要每个活动的用户,我们可以将其作为派生表/内联视图,并根据activity.indx连接回所需的集合以获取相关用户。

这样想。此选择应返回一组数据,这些数据是具有满足您其他要求的多个用户的活动。然后你将这个集合加入到你的基础集中,以获得你所追求的名字。有意义吗?

我还在计数中添加了DISTINCT。好像记录中存在同一个用户可以加入相同活动的记录,我的理解是你不想数他们两次!

SELECT sum(score), Activity.Indx as 'activity'
FROM Activity 
INNER JOIN User_Activity 
  ON Activity.Indx = User_Activity.Indx_Activity 
INNER JOIN [User] 
  ON User_Activity.Indx_User = [User].Indx 
INNER JOIN Team_Users 
  ON [User].Indx = Team_Users.Indx_User
where Team_Users.Indx_Team = 'C1401D43-7154-4E92-BA0B-B491137E998D'
  and Activity.Indx_ActivityDistribution = dbo.Indx_ActivityDistribution_Team()
group by Activity.Indx
having count (Distinct [User].Indx) > 1