2014-11-21 110 views
2

我有从表单中收集的数据。并具有“摆动”的数据,所以它看起来是这样的:在几列之间显示最大值的列名称

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 

感谢您的回复。我想知道是否可以在一个查询中做到这一点。例如,下面是我用得到我上面的数据写入第一表例如查询,whick摆动数据:

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 

我不知道是否有以纳入戈登或者欧文提供这个答案的方式第一个查询可以在一个查询中完成。谢谢。

+0

您可以发布您用来创建第一个结果的查询吗? – paqogomez 2014-11-21 17:30:34

+0

(i)您是否仍然拥有上表中的数据库中可访问的原始数据(不支持)?什么是表结构? (ii)如果不止一个城市具有某个县的最大价值,会发生什么? – Abecee 2014-11-21 18:58:28

+0

我发布了上面的原始查询(这是为我创建的枢轴)。我的数据是从谷歌形式进来的,这就是为什么我要摆脱它。如果有更多的城市具有相同的最大值,我希望包括这两个,但愿意只选择第一个。 – user30832 2014-11-23 21:45:41

回答

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 
+0

感谢您的回复。我想知道是否可以在一个查询中做到这一点。例如,以下是我用于将数据放入上述第一个表格示例的查询: – user30832 2014-11-23 20:16:12

+0

在上面添加了我的第一个代码,想知道是否可以添加您的建议以创建所有查询? – user30832 2014-11-23 20:29:56

3

这是一个"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; 

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

+0

在上面添加了我的第一个代码,想知道是否可以添加您的建议来完成这一项查询? – user30832 2014-11-23 20:29:19