HAVING子句中的CASE语句

时间:2014-08-05 06:49:44

标签: sql sqlite

我有这个问题:

select distinct RL.LINK_ID as LINK_ID, RLG.[ZLEVEL] 
from RDF_LINK as RL, RDF_LINK_GEOMETRY as RLG 
where (RL.BRIDGE = 'Y' OR RL.TUNNEL = 'Y') AND RL.LINK_ID = RLG.[LINK_ID]

给我这个输出:

RecNo   LINK_ID     ZLEVEL
1       26018528    0
2       53678211    1
3       53678211    0
4       53681432    1
5       53681432    0
6       53682353    0
7       53682353    1
8       53683791    1
9       53683791    0
10      53691049    1
11      53691049    0

现在我想要单个LINK_ID(如果有两个(对于他们ZLevel是1)

同样,我尝试了GROUP BYHaving with CASE statement,但无法按照我的意愿获得输出。

1 个答案:

答案 0 :(得分:1)

在分组后,您会想要过滤掉某些群组时使用HAVING。

但是,您实际上想要为每个LINK_ID创建一个组,并为每个组获取最大ZLEVEL值:

SELECT RL.Link_ID,
       max(RLG.ZLevel)
FROM RDF_Link AS RL
JOIN RDF_Link_Geometry AS RLG USING (Link_ID)
WHERE RL.Bridge = 'Y'
   OR RL.Tunnel = 'Y'
GROUP BY RL.Link_ID