English 中文(简体)
带计数、分组依据、位置和联接的SQL Server SELECT
原标题:SQL Server SELECT with Count, Group By, Where and Join

I previously created this SELECT command in SQL Server, that needs to count how many Employees each Department contains. This is what I used:

SELECT Departments.Description AS [Department], COUNT(Employees.ID) AS [Employees]
FROM Employees 
RIGHT JOIN Departments ON
Employees.DepartmentID = Departments.ID 
GROUP BY Departments.Description

而且它运行良好(即使没有员工,也会向部门显示)。

现在,我想通过以下过滤器只计算当前工作的员工:

WHERE Employees.JobEndDate = null

我试着这样做:

SELECT   Departments.Description AS [Department], COUNT(Employees.ID) AS [Employees] 
FROM Employees 
RIGHT JOIN Departments ON
Employees.DepartmentID = Departments.ID 
WHERE Employees.JobEndDate = NULL 
GROUP BY Departments.Description

But now I get no results. Any idea what I m doing wrong?

Thanks :) Mitsy.

最佳回答

测试字段返回值是否为NULL不是通过相等的=is运算符执行的

= null

不起作用,而是写:

Employees.JobEndDate IS NULL
问题回答
SELECT Departments.Description AS [Department], COUNT(Employees.ID) AS [Employees] FROM Employees RIGHT JOIN Departments ON Employees.DepartmentID = Departments.ID WHERE Employees.JobEndDate = IS NULL GROUP BY Departments.Description




相关问题
SELECT command to calculate percentage

I m trying to get the percentage of each video I have in my database based on its view count against all other videos. I m then trying to display all the videos from highest view count to lowest, ...

Consolidating a COUNT query

I have a page where I am running an initial SQL query to get a list of subjects, then I loop over this query and run two additional queries for each record returned from the original subjects query (I ...

R: Count number of objects in list [closed]

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) ...

Mysql get count of rows for each day

My Current query is: SELECT DISTINCT DATE(vote_timestamp) AS Date, COUNT(*) AS TotalVotes FROM `votes` WHERE vote_target_id= 83031 GROUP BY DATE(vote_timestamp) ORDER BY DATE(vote_timestamp) DESC ...

Group by named column

I always forget how to do things like this. I have a database table with birthdates and I want to find out how many people have the same age. I m trying: SELECT TIMESTAMPDIFF( YEAR, birthdate, ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

热门标签