我正在尝试使用sql计算不同的复合列。以下是我的查询
SELECT distinct_val.LeaseTenantSSN, count(distinct_val.LeaseTenantSSN) = 3
FROM (select distinct houseid, LeaseTenantSSN from tenants) AS distinct_val
group by distinct_val.LeaseTenantSSN;
结果
我想要的是
SELECT distinct_val.LeaseTenantSSN
FROM (select distinct houseid, LeaseTenantSSN from tenants) AS distinct_val
where count(distinct_val.LeaseTenantSSN) = 3
group by distinct_val.LeaseTenantSSN;
但它在where语句中给出了错误。我正在使用Postgresql
答案 0 :(得分:1)
使用App.xaml
:
HAVING
这可能会修复您的错误,但我怀疑您确实需要以下内容:
SELECT distinct_val.LeaseTenantSSN
FROM (select distinct houseid, LeaseTenantSSN from tenants) AS distinct_val
group by distinct_val.LeaseTenantSSN
having count(distinct_val.LeaseTenantSSN) = 3
这将返回每个租户SSN,其中有三个与他相关的不同房屋。