我有三张桌子。第一个是C-x SPC
,有5列(PrivteOwner
),第二个是ownerno, fname, lname, address, telno
,有10列(PropertyForRent
),第三个是propertyno, street, city, postcode, type, rooms, rent, ownerno, staffno, branchno
有4列(Viewing
)。
我想找到拥有最多属性而没有查看的所有者。我的代码如下:
clientno, propertyno, viewdate, comment
此代码是否正常运行?如果是,我们如何有效地写它?
答案 0 :(得分:0)
如果您不提供数据定义,数据或预期结果,则很难回答问题。
无论如何,根据你的描述,我认为这可能会得到你想要的结果。
SELECT
TOP 1
PrivteOwner.ownerno,
PrivteOwner.fname,
PrivteOwner.lname,
COUNT(ViewNumber) AS PropertyNumber
FROM
(
SELECT
PropertyForRent.propertyno AS propertyno
, COUNT(Viewing.propertyno) AS ViewNumber
FROM PropertyForRent
LEFT JOIN Viewing ON Viewing.propertyno = PropertyForRent.propertyno
GROUP BY PropertyForRent.propertyno
) AS NoView
JOIN PropertyForRent ON PropertyForRent.propertyno = NoView.propertyno
JOIN PrivteOwner ON PrivteOwner.ownerno = PropertyForRent.ownerno
WHERE ViewNumber = 0
GROUP BY PrivteOwner.ownerno,
PrivteOwner.fname,
PrivteOwner.lname
ORDER BY PropertyNumber DESC