select 语句中的多个条件基于值
Multiple conditions in select statement based on values
我有多个ctes。在我的 select 语句中,我必须根据条件过滤值。这是我的查询。
SELECT roadName
,sum(roadLength) AS sumRoadLength
,avg(elevationDifference) AS eglAvgDepth
,avg(elevationDifference) AS pglAvgDepth
,
FROM cte3
GROUP BY roadName
ORDER BY roadName
在 "elevationDifference" 下有很多值,范围从 -10 到 +20,分布在 "roadName" 中。我想要完成的是,如果所有 "elevationDifference" 值都 <0 并取平均值,"eglAvgDepth" 将 return。与 pglAvgDepth 相同的情况,其中值 >0.
我尝试添加 where 语句,但仅适用于 eglAvgDepth
WHERE elevationDifference < 0
GROUP BY roadName
ORDER BY roadName
只需添加一个条件表达式:
avg(case when elevationDifference < 0 then elevationDifference end) as eglAvgDepth,
avg(case when elevationDifference > 0 then elevationDifference end) as pglAvgDepth,
编辑:
你已经表达了你想要基于所有值是正数还是负数的值。如果是:
(case when max(elevationDifference) < 0 then avg(elevationDifference) end) as eglAvgDepth,
(case when max(elevationDifference) > 0 then avg(elevationDifference) end) as pglAvgDepth,
我有多个ctes。在我的 select 语句中,我必须根据条件过滤值。这是我的查询。
SELECT roadName
,sum(roadLength) AS sumRoadLength
,avg(elevationDifference) AS eglAvgDepth
,avg(elevationDifference) AS pglAvgDepth
,
FROM cte3
GROUP BY roadName
ORDER BY roadName
在 "elevationDifference" 下有很多值,范围从 -10 到 +20,分布在 "roadName" 中。我想要完成的是,如果所有 "elevationDifference" 值都 <0 并取平均值,"eglAvgDepth" 将 return。与 pglAvgDepth 相同的情况,其中值 >0.
我尝试添加 where 语句,但仅适用于 eglAvgDepth
WHERE elevationDifference < 0
GROUP BY roadName
ORDER BY roadName
只需添加一个条件表达式:
avg(case when elevationDifference < 0 then elevationDifference end) as eglAvgDepth,
avg(case when elevationDifference > 0 then elevationDifference end) as pglAvgDepth,
编辑:
你已经表达了你想要基于所有值是正数还是负数的值。如果是:
(case when max(elevationDifference) < 0 then avg(elevationDifference) end) as eglAvgDepth,
(case when max(elevationDifference) > 0 then avg(elevationDifference) end) as pglAvgDepth,