我知道你不能在where子句中使用别名,但我似乎无法弄清楚如何在这里获得正确的查询。我需要以下查询来显示我们的“单位数”,其中有超过10种类型。我尝试了一些不同的东西,但没有运气,下面是我到目前为止。任何帮助表示赞赏!
SELECT location
,datekey
,SUM(CASE
WHEN SmartSize = 'Small'
THEN Unavailable + Vacant + Occupied
ELSE NULL
END) AS SmallUnitCount
,SUM(CASE
WHEN SmartSize = 'Medium'
THEN Unavailable + Vacant + Occupied
ELSE NULL
END) AS MediumUnitCount
,SUM(CASE
WHEN SmartSize = 'Large'
THEN Unavailable + Vacant + Occupied
ELSE NULL
END) AS LargeUnitCount
FROM statstable
GROUP BY location
,datekey
HAVING COUNT(unavailable + vacant + occupied) > 10
答案 0 :(得分:2)
如果我理解正确,您需要sum()
,而不是count()
:
HAVING SUM(unavailable + vacant + occupied) > 10
如果您想要特定的单位类型,您可以这样做:
HAVING SmallUnitCount > 10