SQL计数字符

时间:2018-06-19 19:15:35

标签: sql count

我想使用一条SQL语句,该语句返回部门号,部门名称和每个部门的雇员数,每个部门少于4名的员工按部门号和部门名称分组。我输入了样本数据

CREATE TABLE dbo.Departments
(
First_Name NVARCHAR(20) NOT NULL,
Last_Name NVARCHAR(20) NOT NULL,
Job_Description VARCHAR(30) NULL,
Department_Name VARCHAR(30) NOT NULL,
Department_Number INT NOT NULL,
Date_Hired DATE NOT NULL,
);

INSERT INTO dbo.Departments(First_Name,Last_Name,Job_Description,Department_Name,Department_Number,Date_Hired)
VALUES
('Niel','Matthews','Sales Representative','Marketing',80,'1990-06-12'),
('Jerome','Daniels','Sales Representative','Accounting',60,'1973-03-30'),
('Caleb','Sewcharran','Employee','Accounting',35,'1986-08-10'),
('Orion','Wolf','Employee','Marketing',80,'1987-10-04'),
('Gaige','Johnson','Employee','Accounting',60,'1992-09-22'),
('Maya','Heller','Employee','Marketing',25,'1989-06-17'),
('James','Maroon','Employee','Accounting',30,'2011-08-22'),
('Alex','Mercer','Employee','Accounting',60,'2010-03-17'),
('Adam','Beecher','Sales Representative','Marketing',80,'2012-10-04'),
('Bruce','Wayne','Employee','Marketing',25,'2012-08-12'),
('Al','Kepler','Sales Representative','Accounting',80,'1973-06-30'); 

我也尝试了一条SQL语句,但是它不起作用

SELECT Department_Number,Department_Name,Job_Description 
FROM dbo.Departments
WHERE Job_Description='Employee'
(SELECT DISTINCT Department_Name FROM dbo.Departments,
    (SELECT CAST('Marketing'AS INT)AS Least_Employees,
                CAST('Accounting'AS INT)AS Least_Least_Employees)AS Least_Employees
                WHERE Department_Name>4 OR Department_Name>4)
                GROUP BY Department_Name AND Department_Number;

如果您能告诉我我本可以用来走上正确道路的话,请感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用getElementById子句:

GROUP BY