简体   繁体   English

T-SQL GROUP BY nvarchar 错误

[英]T-SQL GROUP BY a nvarchar error

I was wondering if anyone could help me with a request kindly:我想知道是否有人可以帮助我提出请求:

Here is the data:这是数据:

Table 1: EMPLOYEE表 1: EMPLOYEE

    FK: DID
    PK: Name 
    UserName

Table 2: DEPARTMENT表 2: DEPARTMENT

    PK: DID
    TerminationDate

I'm looking to find the number of terminated employees in the quarter.我正在寻找本季度被解雇的员工数量。 Here is the T-SQL so far:这是到目前为止的 T-SQL:

SELECT 
    DEPARTMENT.name AS Name,
    COUNT(e.userName)
FROM
    EMPLOYEE AS e
JOIN 
    DEPARTMENT ON e.department = DEPARTMENT.DID

UNION 

SELECT
    u.eu, u.name 
FROM 
    (SELECT 
         dd.name, COUNT(ee.userName) AS eu
     FROM 
         DEPARTMENT AS dd
     JOIN 
         EMPLOYEE AS ee ON dd.DID = ee.department
                        AND ee.terminationDate IS NOT NULL
     WHERE
         ee.terminationDate IS NOT NULL
         AND ee.terminationDate BETWEEN '2015-04-01' AND '2015-06-30'
     GROUP BY 
         dd.name, ee.userName) AS u
GROUP BY 
    u.eu, u.name, Name      
ORDER BY 
    Name

The error is:错误是:

Msg 8120, Level 16, State 1, Line 1消息 8120,级别 16,状态 1,第 1 行
Column 'DEPARTMENT.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.列“DEPARTMENT.name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

In the first part of your query, you list the department name in your select clause but don't include it in a group by clause (because it is missing)在查询的第一部分中,您在 select 子句中列出了部门名称,但不要将其包含在 group by 子句中(因为它丢失了)

select DEPARTMENT.name as Name, COUNT(e.userName)
from EMPLOYEE as e
join DEPARTMENT on e.department = DEPARTMENT.DID
group by DEPARTMENT.Name --add this line

The number of problems with this query are bewildering.这个查询的问题数量令人眼花缭乱。 To start with... The table definitions you have listed do not match your query.首先... 您列出的表定义与您的查询不匹配。 You have the TerminationDate as an attribute of the DEPARTMENT table, where as in the query it looks like you are accessing it though the EMPLOYEE table, which makes sense to be there.您将TerminationDate作为DEPARTMENT表的一个属性,在查询中看起来您正在通过EMPLOYEE表访问它,这在那里是有意义的。 So I'm going to assume that your table structure really is:所以我假设你的表结构真的是:

Table 1: EMPLOYEE表 1:员工

FK: DID
PK: Name 
UserName
TerminationDate

Table 2: DEPARTMENT表 2:部门

PK: DID
Name  

The way you have used a UNION in this query leads me to believe that you have a misunderstanding of what a UNION statement does.您在此查询中使用UNION的方式使我相信您对UNION语句的作用有误解。 UNION is used to join two sets into one. UNION用于将两个集合连接为一个。 So in set theory {1,2,3} union {3, 4} is {1,2,3,3,4}.所以在集合论中 {1,2,3} union {3, 4} 是 {1,2,3,3,4}。 In SQL Server, you have to make sure that the types and number of columns in the first select statement match all the selects that are unioned with it.在 SQL Server 中,您必须确保第一个 select 语句中的类型和列数与与其联合的所有选择匹配。 So for example:例如:

 Select 1 A, 2 B
 Union
 Select 3 B

Will give this error because the first select has two columns and the second has only one:会出现这个错误,因为第一个选择有两列,第二个只有一个:

Msg 205, Level 16, State 1, Line 3 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.消息 205,级别 16,状态 1,第 3 行所有使用 UNION、INTERSECT 或 EXCEPT 运算符组合的查询在其目标列表中必须具有相同数量的表达式。

And this query:这个查询:

 Select 1 A
 Union
 Select 'Bob' A

Will give this error because the column types do not match:由于列类型不匹配,会出现此错误:

Msg 245, Level 16, State 1, Line 2 Conversion failed when converting the varchar value 'Bob' to data type int.消息 245,级别 16,状态 1,第 2 行将 varchar 值“Bob”转换为数据类型 int 时转换失败。

If all you are looking for is the number of employees terminated between two dates, then there really isn't really anything you need to union.如果您所寻找的只是两个日期之间被解雇的员工人数,那么您真的没有什么需要联合的。 All the employees are already together in one set (the Employee table)... and you just need to filter the dates you want and count the record total.所有员工都已经在一组( Employee表)中......你只需要过滤你想要的日期并计算记录总数。 You don't even need the DEPARTMENT table to calculate this.您甚至不需要DEPARTMENT表来计算它。

I could write the query for you... but I'll leave the rest to you.我可以为您编写查询……但剩下的就交给您了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM