给出下表ManagerRepRelationship:
repId managerId
35 33
36 33
37 33
23 56
26 60
35 34
37 34
如您所见,经理33和经理34都共享代表35和37.如果我仅经理33的已知值:
SELECT repId FROM ManagerRepRelationship WHERE managerId=33
我会回来35,36,37。
我的问题是,基于只知道managerId,是否有任何方法可以查看返回的repId在整个表中的repId列中是否没有其他重复值。
所以在上面的陈述中我想找到只有一个关系的代表。所以在这种情况下,repId 36将是我想要的回报,因为33是该代表的唯一经理。
这可以在一个SQL语句中完成吗?
答案 0 :(得分:1)
您需要在子查询中再次引用同一个表以检查其他条件:
SELECT repId FROM ManagerRepRelationship a WHERE managerId=33
AND not exists (SELECT 1 FROM ManagerRepRelationship b WHERE b.repId = a.repId and b.managerId <> a.managerId)
答案 1 :(得分:1)
DECLARE @DataSource TABLE
(
[repId] TINYINT
,[managerId] TINYINT
);
INSERT INTO @DataSource ([repId], [managerId])
VALUES (35, 33)
,(36, 33)
,(37, 33)
,(23, 56)
,(26, 60)
,(35, 34)
,(37, 34);
SELECT DS1.*
FROM @DataSource DS1
LEFT JOIN @DataSource DS2
ON DS1.[repID] = DS2.[repId]
AND DS2.[managerId] <> 33
WHERE DS1.[managerId] = 33
AND DS2.[repId] IS NULL;
答案 2 :(得分:0)
您必须对rep_id进行分组,然后进行计数。
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('myCtrl', function($scope, $http) {
$http.get("resource-database.json").then(function (response) {
$scope.myData = response.data.records;
});
$scope.filteredmyData = []
,$scope.currentPage = 1
,$scope.numPerPage = 3
,$scope.maxSize = 5;
$scope.$watch('currentPage + numPerPage', function() {
var begin = (($scope.currentPage - 1) * $scope.numPerPage)
, end = begin + $scope.numPerPage;
$scope.filteredmyData = $scope.myData.slice(begin, end);
});
});
(未经测试)
答案 3 :(得分:0)
有几种方法可以做到这一点。这是一个:
select m33.repId
from ManagerRepRelationship m33
left join ManagerRepRelationship mOther
on m33.repId = mOther.repId
and mOther.managerId != m33.managerId
where m33.managerId = 33
and mOther.repId is NULL