Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将查询转换为SQL Server语法_Sql_Sql Server_Database_Syntax Error - Fatal编程技术网

将查询转换为SQL Server语法

将查询转换为SQL Server语法,sql,sql-server,database,syntax-error,Sql,Sql Server,Database,Syntax Error,我一直在从Chegg那里得到帮助,给我的大多数答案对我来说都不起作用。 我的第一个任务是编写一个查询,其中列出分配给大多数项目的员工 一项答复是: 我得到的错误是: 味精1033,第15级,状态1,第3行 ORDER BY子句在视图、内联函数、派生表、子查询和公共表表达式中无效,除非还指定了TOP、OFFSET或FOR XML 第二个反应是 我得到的错误是: 味精130,15级,状态1,第8行 无法对包含聚合或子查询的表达式执行聚合函数 第三个反应是 Select emp_ID,emp_Name

我一直在从Chegg那里得到帮助,给我的大多数答案对我来说都不起作用。 我的第一个任务是编写一个查询,其中列出分配给大多数项目的员工

一项答复是:

我得到的错误是:

味精1033,第15级,状态1,第3行 ORDER BY子句在视图、内联函数、派生表、子查询和公共表表达式中无效,除非还指定了TOP、OFFSET或FOR XML

第二个反应是

我得到的错误是:

味精130,15级,状态1,第8行 无法对包含聚合或子查询的表达式执行聚合函数

第三个反应是

Select emp_ID,emp_Name 
from Employee inner join Project on Employee.Dept_ID = Project.Dept_ID 
group by emp_ID,emp_Name 
having count(Proj_ID) = max(count(Proj_ID));
错误消息:

味精130,15级,状态1,第13行 无法对包含聚合或子查询的表达式执行聚合函数

非常感谢您对如何解决其中任何一个问题的任何帮助

下面是创建我的数据库的脚本(如果有帮助的话)

create table department
(
  dept_ID int not null ,
  dept_name char(50) NOT NULL,
  manager_ID int not null,
  manager_start_date date not null,

  constraint Dept_PK primary key (dept_ID),
  constraint D_Name_AK unique (dept_name)
);
insert into department values(1,'abc',1,'2019-01-08');
insert into department values(2,'abc2',2,'2019-01-08');
insert into department values(3,'abc3',2,'2019-01-08');
insert into department values(4,'abc4',2,'2019-01-08');

/*project table done*/
create table project
(
  proj_ID int not null,
  proj_name varchar(20) not null,
  dept_ID int not null,
  proj_location varchar(20) not null,

  constraint Proj_ID_PK primary key (proj_ID),
  constraint Proj_Dep_FK foreign key (dept_ID) references department(dept_ID)
);
insert into project values ( 1,'project1',1,'india');
insert into project values ( 2,'project2',2,'US');

/*employee table done*/
create table employee
(
  emp_ID int NOT NULL ,
  emp_name char(50) not null,
  emp_ssn char(11) not null,
  emp_address char(50) not null,
  salary decimal(10,2) not null,
  sex char(1) not null,
  date_of_birth date not null,
  dept_ID int not null,
  supervisor_ID int null,

  constraint emp_PK primary key(emp_ID),
  constraint emp_Name_AK unique (emp_name),
  constraint emp_SSN_AK unique (emp_ssn),
  constraint sup_FK foreign key(supervisor_ID) references employee(emp_ID),
  constraint empDep_FK foreign key(dept_ID) references department(dept_ID)
);

insert into employee values( 1,'jagmeet', 'ssn','patel nagar',300,'M','1997-07-01',1,1);

insert into employee values( 2,'harpreet', 'ssn1','patel nagar2',300,'F','1997-07-01',1,2);
/*Department location table done*/
create table dept_location
(
  dept_ID int not null,
  location char(50) not null,

  constraint dept_location_PK primary key(dept_ID, location),
  constraint dept_FK foreign key (dept_ID) references department(dept_ID)
);
insert into dept_location values(1,'loc1');

insert into dept_location values(2,'loc2');

/*dependent table done*/
create table dependent
(
  dependent_ID int not null ,
  emp_ID int NOT NULL,
  dependent_name varchar(20) NOT NULL,
  dependent_sex char(1) NOT NULL,
  dependent_DOB date not null,
  dep_relation varchar(10) not null,

  constraint dep_ID_PK primary key (dependent_ID),
  constraint deb_emp_ID_FK foreign key (emp_ID) references employee(emp_ID)
);
insert into dependent values (1,2,'deptname','M','2018-01-09','rel1');

insert into dependent values (2,1,'deptname2','F','2018-01-09','rel2');
/*work period table done*/

create table work_period
(
  emp_ID int NOT NULL,
  proj_ID int not null,
  pay_period date not null,
  weekly_work_hrs int not null,

  constraint pay_period_PK primary key (pay_period),
  constraint WP_empID_FK foreign key (emp_ID) references employee(emp_ID),
  constraint WP_projID_FK foreign key (proj_ID) references project(proj_ID)
);
insert into work_period values(1,1,'2012-09-01',7);

insert into work_period values(2,2,'2014-09-01',8);
您可以使用子查询获取项目数量最多的员工,以及max和count聚合函数

select * from employee where dept_id in (
    select max(ct) from 
        (select count(1) ct, t1.dept_id from department t1
        inner join project t2 on t2.dept_id = t1.dept_id
        group by t1.dept_id) t3 )

你的预期结果是什么?因为在我的数据库中,每个人都有相同数量的项目,我只希望列出项目最多的部门和该部门的所有员工。干得好!!!看起来很复杂,但它能工作。我能做同样的事情来找到时间最多的项目吗?这将是我的第二个最后一个查询,这是“选择t.*,t3.*从员工t内部加入选择maxweekly\u work\u hrs maxhours,t1.dept_id来自部门t1内部连接项目t2在t2.dept_id=t1.dept_id内部连接工作时间w在w.proj_id=t2.proj_id组由t1.dept_id在t3.dept_id=t.dept_id`不是我想要的结果,但我仍然很欣赏effort@IvanGonzalez,我想你需要把这个问题单独提出来,因此,它不会在主要问题上发生冲突。谢谢
create table department
(
  dept_ID int not null ,
  dept_name char(50) NOT NULL,
  manager_ID int not null,
  manager_start_date date not null,

  constraint Dept_PK primary key (dept_ID),
  constraint D_Name_AK unique (dept_name)
);
insert into department values(1,'abc',1,'2019-01-08');
insert into department values(2,'abc2',2,'2019-01-08');
insert into department values(3,'abc3',2,'2019-01-08');
insert into department values(4,'abc4',2,'2019-01-08');

/*project table done*/
create table project
(
  proj_ID int not null,
  proj_name varchar(20) not null,
  dept_ID int not null,
  proj_location varchar(20) not null,

  constraint Proj_ID_PK primary key (proj_ID),
  constraint Proj_Dep_FK foreign key (dept_ID) references department(dept_ID)
);
insert into project values ( 1,'project1',1,'india');
insert into project values ( 2,'project2',2,'US');

/*employee table done*/
create table employee
(
  emp_ID int NOT NULL ,
  emp_name char(50) not null,
  emp_ssn char(11) not null,
  emp_address char(50) not null,
  salary decimal(10,2) not null,
  sex char(1) not null,
  date_of_birth date not null,
  dept_ID int not null,
  supervisor_ID int null,

  constraint emp_PK primary key(emp_ID),
  constraint emp_Name_AK unique (emp_name),
  constraint emp_SSN_AK unique (emp_ssn),
  constraint sup_FK foreign key(supervisor_ID) references employee(emp_ID),
  constraint empDep_FK foreign key(dept_ID) references department(dept_ID)
);

insert into employee values( 1,'jagmeet', 'ssn','patel nagar',300,'M','1997-07-01',1,1);

insert into employee values( 2,'harpreet', 'ssn1','patel nagar2',300,'F','1997-07-01',1,2);
/*Department location table done*/
create table dept_location
(
  dept_ID int not null,
  location char(50) not null,

  constraint dept_location_PK primary key(dept_ID, location),
  constraint dept_FK foreign key (dept_ID) references department(dept_ID)
);
insert into dept_location values(1,'loc1');

insert into dept_location values(2,'loc2');

/*dependent table done*/
create table dependent
(
  dependent_ID int not null ,
  emp_ID int NOT NULL,
  dependent_name varchar(20) NOT NULL,
  dependent_sex char(1) NOT NULL,
  dependent_DOB date not null,
  dep_relation varchar(10) not null,

  constraint dep_ID_PK primary key (dependent_ID),
  constraint deb_emp_ID_FK foreign key (emp_ID) references employee(emp_ID)
);
insert into dependent values (1,2,'deptname','M','2018-01-09','rel1');

insert into dependent values (2,1,'deptname2','F','2018-01-09','rel2');
/*work period table done*/

create table work_period
(
  emp_ID int NOT NULL,
  proj_ID int not null,
  pay_period date not null,
  weekly_work_hrs int not null,

  constraint pay_period_PK primary key (pay_period),
  constraint WP_empID_FK foreign key (emp_ID) references employee(emp_ID),
  constraint WP_projID_FK foreign key (proj_ID) references project(proj_ID)
);
insert into work_period values(1,1,'2012-09-01',7);

insert into work_period values(2,2,'2014-09-01',8);
select * from employee where dept_id in (
    select max(ct) from 
        (select count(1) ct, t1.dept_id from department t1
        inner join project t2 on t2.dept_id = t1.dept_id
        group by t1.dept_id) t3 )