Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 如何连接共享同一列的表?_Sql_Sql Server 2008 - Fatal编程技术网

Sql 如何连接共享同一列的表?

Sql 如何连接共享同一列的表?,sql,sql-server-2008,Sql,Sql Server 2008,我做了一个查询,我想做的是使用departmentid将这两个查询连接起来 我想展示的是部门名称和未经批准的经理 在这种情况下,最好使用联合还是加入 表格 create table cserepux ( status int, comment varchar(25), departmentid int, approveddate datetime ); insert into cserepux (status, comment, departmentid, appr

我做了一个查询,我想做的是使用
departmentid
将这两个查询连接起来

我想展示的是
部门名称
未经批准的经理

在这种情况下,最好使用联合还是加入

表格

create table cserepux
(
   status int, 
   comment varchar(25),
   departmentid int,
   approveddate datetime 
);

insert into cserepux (status, comment, departmentid, approveddate)
values (1, 'testing1', 1, NULL), (1, 'testing2', 1, NULL),
       (1, 'testing2', 2, NULL), (0, 'testing2', 1, NULL),
       (0, 'tesitng2', 1, NULL), (0, 'testing2', 1, NULL),
       (0, 'tesitng2', 1, NULL), (0, 'testing3', 2, NULL),
       (0, 'testing3', 3, NULL);

create table cseDept 
(
    departmentid int, 
    department_name varchar(25)
);

insert into cseDept (departmentid,department_name)
values (1, 'department one'), (2, 'department two'),
       (3, 'department three'), (4, 'department four');
查询

select 
    departmentid, 
    COUNT(*) AS 'not_approved_manager'
from 
    cserepux
where 
    approveddate is null
group by 
    departmentid

SELECT * FROM cseDept

你需要加入。工会不会满足你的要求

select d.department_name, COUNT(*) AS 'not_approved_manager'
from cserepux c
inner join cseDept d on c.departmentid = d.departmentid
where approveddate is null
group by d.department_name

你需要加入。工会不会满足你的要求

select d.department_name, COUNT(*) AS 'not_approved_manager'
from cserepux c
inner join cseDept d on c.departmentid = d.departmentid
where approveddate is null
group by d.department_name

您是否只需要一个连接和一个正确的群组

  select dep.department_name, COUNT(*) AS 'not_approved_manager'
  from cseDept dep
  join cserepux cs on cs.departmentid = dep.departmentid
  where approveddate is null
  group by dep.department_name
小提琴:


由于联接和分组依据在SQL中是非常基本的东西,所以我建议您一定要看一些教程,以便更熟练地使用它。你可以试试楼梯文章系列

你只需要一个连接和一个正确的分组吗

  select dep.department_name, COUNT(*) AS 'not_approved_manager'
  from cseDept dep
  join cserepux cs on cs.departmentid = dep.departmentid
  where approveddate is null
  group by dep.department_name
小提琴:


由于联接和分组依据在SQL中是非常基本的东西,所以我建议您一定要看一些教程,以便更熟练地使用它。你可以试试阶梯文章系列

我在问题中添加了你的小提琴的内容,以便为将来的读者提供必要的信息。如果您觉得我输入的内容不正确,请随时修复或恢复编辑。我将您的小提琴的内容添加到问题中,以便为将来的读者提供必要的信息。如果您觉得我输入了错误的内容,请随时修复或恢复编辑。