Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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/7/sql-server/27.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_Tsql - Fatal编程技术网

Sql 不排除具有特定权限的人的查询

Sql 不排除具有特定权限的人的查询,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有这样的数据: 表按用户权限列出的用户: PersonID Last First Facility Department Privilages 1 Hoff Mary S P abc 1 Hoff Mary S P cde 1 Hoff Mary S P def 2 Smith Georg S P

我有这样的数据: 表按用户权限列出的用户:

PersonID Last First Facility Department Privilages 1 Hoff Mary S P abc 1 Hoff Mary S P cde 1 Hoff Mary S P def 2 Smith Georg S P abc 2 Smith Georg S P cde 但是,当我查看该查询的第一人称(Hoff)时,不应该返回,因为他们确实拥有def的特权,我确实在结果中找到了那个人:

PersonID Last First Facility Dept Privilages
1        Hoff Mary  S        P    (list of her Privilages appended together..does include the def)

您得到的是Mary Hoff,因为有一行符合您的条件。您可以做的是聚合一个人的行,然后查看是否没有行匹配:

SELECT
  personid, last, first, facility, 
  STUFF ( ... department ...) AS departments,
  STUFF ( ... privilages ...) AS privileges
FROM person_by_privilages
WHERE facility = 'S'
AND department like ('%P%')
GROUP BY personid, last, first, facility
HAVING COUNT(CASE WHEN privilages LIKE ('%def%') THEN 1 END) = 0;

如果不存在具有def权限的行,则需要显示用户行。差不多

SELECT personid, last, first, facility, department, ...
FROM person_by_privilages pp
WHERE facility = 'S'
AND department like ('%P%')
AND NOT EXISTS
(
  SELECT *
  FROM person_by_privilages pp2
  WHERE pp2.personid = pp.personid
  AND pp2.facility = 'S'
  AND pp2.department like ('%P%')
  AND pp2.privilages LIKE ('%def%')
);

假设您的数据是一致的(关于Personid,Last,First),您可以通过这种方式将个人数据的提取从他们的特权中分离出来

SELECT DISTINCT PersonID, Last,  First, Facility, Department
  FROM person_by_privilages a
 WHERE NOT EXISTS (SELECT 1 
                     FROM person_by_privilages 
                    WHERE PersonID = a.PersonID and Last = a.Last 
                          and First = a.First and Facility = a.Facility
                          and Department = a.Department
                          and Privilages = 'def')

由于每个特权只有一行,因此必须检查是否没有包含要排除的特权的行。

我想我会注意到,您对“特权”一词的拼写似乎不一致。我不知道这是否是可以纠正的,或者它是否导致了您的任何问题。您的子查询正在为所有人添加结果。你为什么一开始就使用这个查询?您想要的结果不是已经是外部/主查询的一部分了吗?对不起,只是我在键入问题。我想我修好了there@shawnt00-主要外部查询包括每个人的所有特权。我只需要没有def特权的人。将所有特权附加在一起并不能实现这一点。好的,我认为这些特权已经作为单个值存储在一起了。看起来下面的答案可能正是您所需要的。看起来它返回了Hoff privilages,而它们不是“def”。我在结果中看到“abc”和“cde”。还有其他想法吗?我不想把它还给霍夫,只是史密斯。哦,对不起,你说得对。这不管用。我没注意到你在展示部门。因此,此查询无法工作。我会想出另一个解决方案。一个解决方案是把部门塞进一个字符串,就像你对特权所做的那样。我已经相应地更新了我的答案。或者看看我的另一个答案。非常感谢你。工作得很好。
SELECT DISTINCT PersonID, Last,  First, Facility, Department
  FROM person_by_privilages a
 WHERE NOT EXISTS (SELECT 1 
                     FROM person_by_privilages 
                    WHERE PersonID = a.PersonID and Last = a.Last 
                          and First = a.First and Facility = a.Facility
                          and Department = a.Department
                          and Privilages = 'def')