SQL,在连接表上查找总和

时间:2016-09-14 15:27:11

标签: sql sql-server

我有3张表,我试图加入,并获得一列的总和。我有很多困难想出一个有效的查询。这是表格

Expected content-type:
application/atom+xml; charset=UTF-8
Actual content-type:
text/html; charset=UTF-8

我需要做的是报告一年内给员工的总硬币值。所以我要找的最终结果是:

CoinAwards
+----+------------+------------+-----------+
| ID | EmployeeID | AwardDate  | CoinLevel |
+----+------------+------------+-----------+
|  1 | 116733     | 6/11/2015  | 1         |
|  2 | 116359     | 5/20/2016  | 3         |
|  3 | 116733     | 12/8/2015  | 5         |
|  4 | 10637      | 10/25/2015 | 2         |
|  5 | 105197     | 9/15/2016  | 3         |
+----+------------+------------+-----------+

CoinLevels
+----+-------+
| ID | Value | 
+----+-------+
|  1 | 5     | 
|  2 | 10    | 
|  3 | 15    | 
|  4 | 50    | 
|  5 | 100   |
+----+-------+

Employees
+------------+------------+----------+
| EmployeeID | FirstName  | LastName |
+------------+------------+----------+
|  116733    | John       | Doe      |
|  116359    | Jason      | Watson   |
|  10637     | Brandon    | Wilson   |
|  105197    | Mike       | Jones    |
+------------+------------+----------+

以下是我一直在处理的问题,但它无效:

RESULT: Total Sum of Coin Values given in 2015
+------------+------------+----------+-------------------------+
| EmployeeID | FirstName  | LastName | Sum_Of_2015_Coin_Values |
+------------+------------+----------+-------------------------+
|  116733    | John       | Doe      | 105                     |
|  116359    | Jason      | Watson   | 0                       |
|  10637     | Brandon    | Wilson   | 10                      |
|  105197    | Mike       | Jones    | 0                       |
+------------+------------+----------+-------------------------+

任何人都知道如何更改此查询以获取我正在寻找的结果?谢谢你的帮助!

6 个答案:

答案 0 :(得分:6)

Select E.EmployeeID
      ,E.FirstName
      ,E.LastName 
      ,Sum_Of_2015_Coin_Values = isnull(sum(L.Value),0)
 From  Employees E
 Left Join  CoinAwards C on (E.EmployeeID = C.EmployeeID  and Year(C.AwardDate)=2015   )
 Left Join  CoinLevels L on (C.CoinLevel = L.ID )
 Group By 
       E.EmployeeID
      ,E.FirstName
      ,E.LastName 

答案 1 :(得分:0)

从SELECT列表中删除CoinAwards.AwardDate, CoinAwards.CoinLevel

Employees.FirstName, Employees.Lastname添加到您的GROUP BY。

答案 2 :(得分:0)

您还需要在其他列上使用aggregate function

SELECT CoinAwards.EmployeeID, MAX(CoinAwards.AwardDate), MAX(CoinAwards.CoinLevel),
  SUM(CoinLevels.Value) AS Sum_Of_2015_Coin_Values, 
  MAX(Employees.FirstName), MAX(Employees.Lastname)
FROM CoinAwards 
LEFT OUTER JOIN
  CoinLevels ON CoinAwards.CoinLevel = CoinLevels.ID 
LEFT OUTER JOIN
  Employees ON Employees.EmployeeID = CoinAwards.EmployeeID
WHERE  (CoinAwards.AwardDate BETWEEN '01/01/2015' AND '12/31/2015')
GROUP BY CoinAwards.EmployeeID

或者将它们排除在外:

SELECT CoinAwards.EmployeeID, 
  SUM(CoinLevels.Value) AS Sum_Of_2015_Coin_Values
FROM CoinAwards 
LEFT OUTER JOIN
  CoinLevels ON CoinAwards.CoinLevel = CoinLevels.ID 
LEFT OUTER JOIN
  Employees ON Employees.EmployeeID = CoinAwards.EmployeeID
WHERE  (CoinAwards.AwardDate BETWEEN '01/01/2015' AND '12/31/2015')
GROUP BY CoinAwards.EmployeeID

或者他们也分组:

SELECT CoinAwards.EmployeeID, CoinAwards.AwardDate, CoinAwards.CoinLevel,
  SUM(CoinLevels.Value) AS Sum_Of_2015_Coin_Values, 
  Employees.FirstName, Employees.Lastname
FROM CoinAwards 
LEFT OUTER JOIN
  CoinLevels ON CoinAwards.CoinLevel = CoinLevels.ID 
LEFT OUTER JOIN
  Employees ON Employees.EmployeeID = CoinAwards.EmployeeID
WHERE  (CoinAwards.AwardDate BETWEEN '01/01/2015' AND '12/31/2015')
GROUP BY CoinAwards.EmployeeID, CoinAwards.AwardDate, CoinAwards.CoinLevel, Employees.FirstName, Employees.Lastname

错误可能如下:

  

专栏' dbo.YourTable.col2'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我希望这会对你有所帮助。

答案 3 :(得分:0)

尝试类似

的内容
SELECT Employees.EmployeeID, Employees.FirstName, Employees.Lastname
SUM(COALESCE(CoinLevels.Value,0)) AS Sum_Of_2015_Coin_Values

FROM CoinAwards 
LEFT OUTER JOIN
  CoinLevels ON CoinAwards.CoinLevel = CoinLevels.ID 
LEFT OUTER JOIN
 Employees ON Employees.EmployeeID = CoinAwards.EmployeeID
WHERE  (CoinAwards.AwardDate BETWEEN '01/01/2015' AND '12/31/2015')
GROUP BY Employees.EmployeeID, Employees.FirstName, Employees.Lastname

答案 4 :(得分:0)

你也可以使用它:

SELECT Employees.EmployeeID, 
    Employees.FirstName, 
    Employees.Lastname,
    ISNULL(SUM(CoinLevels.Value), 0) AS Sum_Of_2015_Coin_Values
FROM CoinAwards 
LEFT JOIN CoinLevels ON CoinAwards.CoinLevel = CoinLevels.ID 
LEFT JOIN Employees ON Employees.EmployeeID = CoinAwards.EmployeeID
WHERE CoinAwards.AwardDate BETWEEN '01/01/2015' AND '12/31/2015'
GROUP BY Employees.EmployeeID, Employees.FirstName, Employees.Lastname

答案 5 :(得分:0)

;用cte作为 ( 选择employeeid,sum(value)coin_value 从 CoinAwards ca. 在cl.id = ca.coinlevel上加入CoinLevels cl 年份(转换(日期,奖励日期,107))= 2015年 按员工分组 ) 从cte c中选择employeeid,firstname,lastname,coin_value 在c.employeeid = e.employeeid

上加入员工e