2014-11-21 91 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