这是我下面的代码。我想显示“通过”或“失败”作为该特定建筑物/物业/公寓大楼的“无家可归”字段的值。下面的代码返回这些行:
Property Homeless
1523cdc Fail
1523cdc Pass
2700 Fail
2700 Fail
在名为 2700 的建筑物中,有8个公寓,其中7个被标记为“无家可归”字段,其中1个公寓为空白。在名为 1523cdc 的建筑物中,有6个apts,其中1个被标记为“无家可归”字段为“是”,其余5个为空白。我正在尝试跟踪他标记为“无家可归”的公寓的百分比-2700应该是100%,1523cdc应该是16.6666%。 我只想为每栋建筑物设置一行,在此示例中,它应该是2700 Fail和1523cdc Pass
我该怎么做。谢谢:|
SELECT p.sCode "Property"
, case when (sum(case when p.scode = '2700' and unit.sfield5 = 'yes' then 1 else 0 end)) = 8 then 'Pass'
when (sum(case when p.scode = '1523cdc' and unit.sfield5 = 'yes' then 1 else 0 end)) = 1
then 'Pass' else 'Fail' end as 'Homeless'
FROM Property p left JOIN Unit ON (p.hMy = unit.hProperty)
WHERE 1 = 1
and unit.scode not like ('b%')
and unit.scode not like ('h%')
and unit.scode not like ('s%')
and unit.scode != 'HOMESPUN'
and p.scode in ('2700','1523cdc')
group by p.scode, unit.sfield5
ORDER BY p.sCode ASC
答案 0 :(得分:1)
2件事:
您不需要1=1
您不应该group by unit.sfield5
-由于您有2个值(是和空白),因此会产生2行
SELECT p.sCode "Property"
, case when (sum(case when p.scode = '2700' and unit.sfield5 = 'yes' then 1 else 0 end)) = 8 then 'Pass'
when (sum(case when p.scode = '1523cdc' and unit.sfield5 = 'yes' then 1 else 0 end)) = 1
then 'Pass' else 'Fail' end as 'Homeless'
FROM Property p left JOIN Unit ON (p.hMy = unit.hProperty)
WHERE unit.scode not like ('b%')
and unit.scode not like ('h%')
and unit.scode not like ('s%')
and unit.scode != 'HOMESPUN'
and p.scode in ('2700','1523cdc')
group by p.scode
ORDER BY p.sCode ASC