SQL Server:如果引用表中的值相等,请从表中选择值
我有两张桌子:单位SQL Server:如果引用表中的值相等,请从表中选择值,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有两张桌子:单位 name | departament | count | unit_manager unit_1 | dep_1 | 100 | nick unit_2 | dep_1 | 200 | john 及雇员: Employees表有一个外键单位,它引用units表中的name列 我需要编写一个SQL查询来选择所有单位,其中除单位经理之外的所有员工都有相同的职位 我该怎么做 我所能找
name | departament | count | unit_manager
unit_1 | dep_1 | 100 | nick
unit_2 | dep_1 | 200 | john
及雇员:
Employees表有一个外键单位,它引用units表中的name列
我需要编写一个SQL查询来选择所有单位,其中除单位经理之外的所有员工都有相同的职位
我该怎么做
我所能找到的就是如何从employees表中选择具有相似值的单位
SELECT u.*
FROM employee s1, employee s2
INNER JOIN units u ON s2.unit = u.name
WHERE s1.full_name <> s2.full_name
AND s1.position = s2.position
试试这个:
SELECT u.*
FROM employee s1
INNER JOIN employee s2 ON s1.urID = s2.urID
INNER JOIN units u ON s2.unit = u.name
WHERE s1.full_name <> s2.full_name
AND s1.position = s2.position
如果不返回所需的数据,请尝试:
SELECT u.*
FROM employee s1
INNER JOIN employee s2 ON s1.urID = s2.urID
LEFT OUTER JOIN units u ON s2.unit = u.name
WHERE s1.full_name <> s2.full_name
AND s1.position = s2.position
为了更好地掌握如何正确进行连接,请参见连接类型,我不得不猜测一下您想要什么,但请尝试一下。。。 您的测试数据不足以测试结果,因此我添加了一些行。。。 这将返回unit_1,因为unit_1中的所有员工都是职员,而不是unit_2,因为unit_2中的1名员工“不是职员”,不包括经理
create table #unit (name varchar(20), departament varchar(20), counta varchar(20), unit_manager varchar(20))
insert into #unit values('unit_1','dep_1','100','nick');
insert into #unit values('unit_2','dep_1','200','john');
create table #employees(full_name varchar(20), unit varchar(20), position varchar(20))
insert into #employees values('nick','unit_1','manager');
insert into #employees values('mary1.1','unit_1','clerk');
insert into #employees values('mary1.2','unit_1','clerk');
insert into #employees values('mary1.3','unit_1','clerk');
insert into #employees values('mary1.4','unit_1','clerk');
insert into #employees values('john','unit_2','manager');
insert into #employees values('mary2.1','unit_2','clerk');
insert into #employees values('mary2.2','unit_2','not a clerk');
insert into #employees values('mary2.3','unit_2','clerk');
insert into #employees values('mary2.4','unit_2','clerk');
SELECT u.name AS Unit, COUNT(*) as Employees, COUNT(DISTINCT e1.Position) as Positions
FROM #unit u
JOIN #employees e1 on u.name = e1.unit AND u.unit_manager <> e1.full_name
GROUP by u.name
HAVING COUNT(DISTINCT e1.Position) = 1
如果您想查看所有单元的列表,以及它们有多少不同的位置,请执行此操作
SELECT u.name AS Unit, COUNT(*) as Employees, COUNT(DISTINCT e1.Position) as Positions
FROM #unit u
JOIN #employees e1 on u.name = e1.unit AND u.unit_manager <> e1.full_name
GROUP by u.name
你最初的努力有什么问题?你有错误吗?你能把它贴出来吗?你想用员工全名或员工职位来比较什么?您的数据集中似乎有相互矛盾的信息。nick是否仅仅是1号机组的经理,还是同时也是2号机组的有效经理。-20多年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,因此不鼓励使用它。强烈反对将这两种风格混合使用@此外,我知道您只是试图获取查询,但避免使用select*,始终定义select列,即使您实际上需要表中的所有列,尽管这种情况很少发生。
SELECT u.name AS Unit, COUNT(*) as Employees, COUNT(DISTINCT e1.Position) as Positions
FROM #unit u
JOIN #employees e1 on u.name = e1.unit AND u.unit_manager <> e1.full_name
GROUP by u.name