[英]Inner joins with have clause does not give output
I have tables like this. 我有这样的桌子。
PollingDivision PollingDivision
pdID pdName
1 Homagama
2 Maharagama
3 Kesbewa
PollingBooth PollingBooth
PBID PBName pdivID
1 HP 1
2 HD 2
PollingBoothElection PollingBoothElection
pbID elecID numofEO
1 1 3
PollingBoothElectionOfficial PollingBoothElectionOfficial
pbID elecid eOfficialID
1 1 1
I'm trying to get pooling booth names from polling booth in which the allocated election official count(count of eOfficialID column for a particular elecID and pbID in PollingBoothElectionOfficial table) is less than the number of election official number in PollingBoothElection Table by Polling Division. 我正在尝试从以下分配的选举官员人数(在PollingBoothElectionOfficial表中用于特定elecID和pbID的eOfficialID列的数量)小于在PollingBoothElectionOflection表中的选举官员人数中所获得的投票站的集合摊位名称。
Here's what I tried. 这是我尝试过的。
SELECT
PB.PBName, COUNT(PBEO.eOfficialID) AS EOCount
FROM
PollingBoothElection PBE
INNER JOIN
PollingBooth PB ON PBE.pbID = PB.PBID
INNER JOIN
PollingDivision PD ON PB.pdivID = PD.pdID
INNER JOIN
PollingBoothElectionOfficial PBEO ON PBE.elecID = PBEO.elecID
AND PBE.pbID = PBEO.pboothID
WHERE
PBE.elecID = 1
AND PD.pdName = 'Homagama'
GROUP BY
PB.PBName, PBE.numOfEO
HAVING
COUNT(PBEO.eOfficialID) < (PBE.numOfEO);
Though I have data in my table it does not give me a result. 尽管我的表中有数据,但它没有给我结果。 What am I doing wrong here?
我在这里做错了什么?
Expected result 预期结果
pbName Count
HP 1
Because For Polling Booth HP number of eo is 3 but in PollingBoothElectionOfficial table there's only 1 record for pbid1 elecid 1 so the count is less than the number of eos(numOfEO). 因为对于轮询展台,HP的eo数量为3,但是在PollingBoothElectionOfficial表中,pbid1 elecid 1仅有1条记录,因此该计数小于eos(numOfEO)的数量。
I'll put it in here since I'll go beyond the comment limit. 我将其放在此处,因为我将超出评论限制。
Your query results what you're expecting: 您的查询会得出您期望的结果:
SAMPLE DATA 样本数据
CREATE TABLE PollingDivision(
pdID INT,
pdName VARCHAR(20)
)
CREATE TABLE PollingBooth(
PBID INT,
PBName VARCHAR(20),
pdivID INT
)
CREATE TABLE PollingBoothElection(
pbID INT,
elecID INT,
numofEO INT
)
CREATE TABLE PollingBoothElectionOfficial(
pbID INT,
electID INT,
eOfficialID INT
)
INSERT INTO PollingDivision VALUES (1, 'Homagama'), (2, 'Maharagama'), (3, 'Kesbewa');
INSERT INTO PollingBooth VALUES (1, 'HP', 1), (2, 'HD', 2);
INSERT INTO PollingBoothElection VALUES (1, 1, 3);
INSERT INTO PollingBoothElectionOfficial VALUES (1, 1, 1);
Your (formatted) query: 您的(格式化的)查询:
SELECT
PB.PBName,
COUNT(PBEO.eOfficialID) AS EOCount
FROM PollingBoothElection PBE
INNER JOIN PollingBooth PB
ON PBE.pbID = PB.PBID
INNER JOIN PollingDivision PD
ON PB.pdivID = PD.pdID
INNER JOIN PollingBoothElectionOfficial PBEO
ON PBE.elecID = PBEO.electID --replaced PBEO.elecID with PBEO.electID
AND PBE.pbID = PBEO.pbID -- replaced PBEO.pboothID with PBEO.pbID
WHERE
PBE.elecID = 1
AND PD.pdName = 'Homagama'
GROUP BY PB.PBName, PBE.numOfEO
HAVING COUNT(PBEO.eOfficialID) < (PBE.numOfEO);
RESULT 结果
PBName EOCount
-------------------- -----------
HP 1
This should help resulting to what you needed: 这应该可以帮助您实现所需的结果:
Select PBName, SUM(eOfficialIDCount)[Count]
from (Select pbID, elecID, Count(Distinct eOfficialID)eOfficialIDCount
from PollingBoothElectionOfficial Group by pbID, electID)A
Left Join (Select pbID, elecID, numofEO from PollingBoothElection)B On B.pbID+b.elecID = A.pbID+a.elecID
Left Join (Select pbID, PBName from PollingBooth)C On C.pbID = A.pbID
Where eOfficialIDCount < numofEO Group by PBName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.