用户和公司拥有积分,公司,团队的SQL按公司和团队的积分总和

时间:2018-08-26 21:14:16

标签: sql

我有表格User,Points,Team和Company。

用户属于团队和公司。 团队与公司关联。 点与用户相关联。

列出团队和公司总分的最佳进攻计划是什么?

User Table
UserId|CompanyId|TeamId
1     |1        |1
2     |1        |1
3     |1        |2
4     |2        |3
5     |2        |3
6     |2        |3

Team Table
TeamId|TeamName|CompanyId
1     |a       |1
2     |b       |1
3     |c       |2

Company Table
CompanyId|CompanyName
1        |abc
2        |def

Points Table
PointsId|UserId|Points
1       |1     |3
2       |1     |1
3       |2     |2
4       |3     |3
5       |3     |5
6       |4     |2
7       |5     |3
8       |6     |1
9       |6     |1

示例: User1已登录,因此他需要查看每个公司以及该公司中每个团队的总积分

在这种情况下,可以说userId 1已登录

查询一个-每个公司的总积分:

Company 1 Has 3 users and they have 5 different points associated to the users
Company 1 Sum Points = (3 + 1 + 2 + 3 + 5) = 14
Company 2 Has 3 users and they have 4 different points associated to their users
Company 2 Sum Points = (2 + 3 + 1 + 1) = 7

查询两个-登录用户所在公司的每个团队的积分总和: UserId 1在CompanyId 1中,该公司具有2个团队

Team 1 has 2 Users (userId 1,2)
Team 1 Sum Points: (3 + 1 + 2) = 6
Team 2 has 1 User ( userId 3 )
Team 2 Sum Points: (3 + 5) = 8

我当时正在考虑将companyId和TeamId包含在积分表中,然后根据该表进行查询,但这似乎是解决这一问题的简便方法。

2 个答案:

答案 0 :(得分:0)

这些查询将执行您想要的操作。我将Points的总和放入了子查询中,因为它简化了JOIN

 SELECT c.CompanyName, COUNT(u.UserId) AS users, SUM(p.scores) AS scores, SUM(p.Points) AS points
  FROM User u
  JOIN Company c ON c.CompanyID = u.CompanyID
  JOIN (SELECT UserId, COUNT(PointsId) AS scores, SUM(Points) AS Points
        FROM Points p
        GROUP BY UserId) p
        ON p.UserId = u.UserID
  GROUP BY c.CompanyId;

输出:

CompanyName     users   scores  points
abc             3       5       14
def             3       4       7

查询2:

 SELECT t.TeamName, COUNT(u.userId) AS users, SUM(p.Points) AS points
  FROM Team t
  JOIN User u ON u.TeamId = t.TeamId
  JOIN (SELECT UserId, COUNT(PointsId) AS scores, SUM(Points) AS Points
        FROM Points p
        GROUP BY UserId) p
        ON p.UserId = u.UserID
  GROUP BY t.TeamId

输出:

TeamName    users   points
a           2       6
b           1       8
c           3       7

如果要将团队列表限制为特定公司,请添加例如AND u.CompanyId = 1上的JOINUser,例如

SELECT t.TeamName, COUNT(u.userId) AS users, SUM(p.Points) AS points
  FROM Team t
  JOIN User u ON u.TeamId = t.TeamId AND u.CompanyId = 1
  JOIN (SELECT UserId, COUNT(PointsId) AS scores, SUM(Points) AS Points
        FROM Points p
        GROUP BY UserId) p
        ON p.UserId = u.UserID
  GROUP BY t.TeamId;

输出:

TeamName    users   points
a           2       6
b           1       8

SQLFiddle

答案 1 :(得分:0)

userpoints左连接到companyGROUP BY公司使用sum()获得每个公司的积分之和。这样会使每个公司都变得尖锐。

SELECT c1.companyid,
       c1.companyname,
       coalesce(sum(p1.points), 0)
       FROM company c1
            LEFT JOIN user u1
                      ON u1.companyid = c1.companyid
            LEFT JOIN points p1
                      ON p1.userid = u1.userid
       GROUP BY c1.companyid,
                c1.companyname;

内部加入usercompanyteam,以获取用户所属公司的团队。左加入团队的用户及其观点。 GROUP BY团队使用sum()获得每个公司的积分总和。使用WHERE子句将其限制为用户1。这样可以为用户用户1的公司每个团队获取积分。

SELECT t1.teamid,
       t1.teamname,
       coalesce(sum(p1.points), 0)
       FROM user u1
            INNER JOIN company c1
                       ON c1.companyid = u1.companyid
            INNER JOIN team t1
                       ON t1.companyid = c1.companyid
            LEFT JOIN user u2
                      ON u2.teamid = t1.teamid
            LEFT JOIN points p1
                      ON p1.userid = u2.userid
       WHERE u1.userid = 1
       GROUP BY t1.teamid,
                t1.teamname;