显示列名称,其中包含多个列之间的最大值

时间:2014-11-21 17:28:21

标签: sql postgresql max multiple-columns postgis

我有从表单收集的数据。并且已经“转动”了数据,所以它看起来像这样:

COUNTY     | denver  | seattle   | new_york | dallas   | san fran
-----------+---------+-----------+----------+----------+---------
ada        | 3       | 14        | 0        | 0        | 0    
slc        | 10      | 0         | 0        | 0        | 9    
canyon     | 0       | 5         | 0        | 0        | 0    
washington | 0       | 0         | 11       | 0        | 0    
bonner     | 0       | 0         | 0        | 2        | 0

(这是使用case语句完成的,我正在使用的环境中不允许使用交叉表:cartodb)

我现在需要一个列出最大值CITY的列。例如:

COUNTY     | CITY     | denver  | seattle   | new_york | dallas   | san fran
-----------+----------+---------+-----------+----------+----------+---------
ada        | seattle  | 3       | 14        | 0        | 0        | 0    
slc        | denver   | 10      | 0         | 0        | 0        | 9    
canyon     | seattle  | 0       | 5         | 0        | 0        | 0    
washington | new_york | 0       | 0         | 11       | 0        | 0    
bonner     | dallas   | 0       | 0         | 0        | 2        | 0

感谢您的回复。我想知道我是否可以在一个查询中实现这一点。例如,以下是我用于将数据导入上面第一个表示例的查询,该数据调整数据:

SELECT counties.name, counties.state, counties.the_geom,
count(case when fandom_survey_one.favorite_team = 'Arizona Cardinals' then 'ari' end)                                       ari,
count(case when fandom_survey_one.favorite_team = 'Atlanta Falcons' then 'atl' end) atl,
count(case when fandom_survey_one.favorite_team = 'Baltimore Ravens' then 'bal' end) bal,
count(case when fandom_survey_one.favorite_team = 'Buffalo Bills' then 'buf' end) buf,
count(case when fandom_survey_one.favorite_team = 'Carolina 
FROM fandom_survey_one, counties
WHERE ST_Intersects(fandom_survey_one.the_geom, counties.the_geom)
group by counties.name, counties.state, counties.the_geom
order by counties.name, counties.state

我想知道是否有办法将Gordon或Erwin提供的答案合并到第一个查询中,以便能够在一个查询中完成所有操作。感谢。

2 个答案:

答案 0 :(得分:4)

这是"simple" or "switched" CASE语句的教科书示例,以避免重复代码。

SELECT CASE greatest(denver, seattle, new_york, dallas, "san fran")
          WHEN denver      THEN 'denver'
          WHEN seattle     THEN 'seattle'
          WHEN new_york    THEN 'new_york'
          WHEN dallas      THEN 'dallas'
          WHEN "san fran"  THEN 'san fran'
       END AS city, *
FROM   tbl;

列表中的第一个(从左到右)在平局的情况下获胜。

答案 1 :(得分:0)

您可以使用大case语句执行此操作:

select t.*,
       (case when denver = greatest(denver, seattle, new_york, dallas, sanfran) then 'denver'
             when seattle = greatest(denver, seattle, new_york, dallas, sanfran) then 'seattle'
             when new_york = greatest(denver, seattle, new_york, dallas, sanfran) then 'new_york'
             when dallas = greatest(denver, seattle, new_york, dallas, sanfran) then 'dallas'
             when sanfran = greatest(denver, seattle, new_york, dallas, sanfran) then 'sanfran'
        end) as City                 
from table t;

编辑:

我会在最后调整结果。像这样:

SELECT name, state, the_geom,
       MAX(CASE WHEN seqnum = 1 THEN favorite_team END) as favorite_team,
       MAX(CASE WHEN favorite_team = 'Arizona Cardinals' THEN cnt ELSE 0 END) as ari,
       MAX(CASE WHEN favorite_team = 'Atlanta Falcons' THEN cnt ELSE 0 END) as atl,
       MAX(CASE WHEN favorite_team = 'Baltimore Ravens' THEN cnt ELSE 0 END) as bal,
       MAX(CASE WHEN favorite_team = 'Buffalo Bills' THEN cnt ELSE 0 END) as buf
FROM (SELECT c.name, c.state, c.the_geom, s.favorite_team, count(*) as cnt,
             ROW_NUMBER() OVER (PARTITION BY c.name, c.state, c.the_geom ORDER BY COUNT(*) desc) as seqnum
      FROM fandom_survey_one s JOIN
           counties c
           ON ST_Intersects(s.the_geom, c.the_geom)
      GROUP BY c.name, c.state, c.the_geom, s.favorite_team
     ) c
GROUP BY name, state, the_geom
ORDER BY name, state