2012-03-18 55 views
0

我使用PostgreSQL 9.1.13PostgreSQL - 訂單通過使用數組。我可以動態創建數組嗎?

以下是我迄今所做的:

/* First Array ==> SELECT raggruppamento FROM ok_filiali GROUP BY raggruppamento ORDER by count(raggruppamento) DESC */ 
/* Second Array ==> SELECT banker FROM ok_filiali GROUP BY banker ORDER by count(banker) DESC */ 

SELECT mandato, raggruppamento, banker 
FROM ok_filiali 
ORDER BY 
idx(array['MI','FI','BS','RM','BO','TO','GE','*','PD','PR'], ok_filiali.raggruppamento), 
idx(array['120','MF','28','921','30','29','56','38','76','33', '27', '8037', '5038', '173', '5984', '45', '104', '46', 
    '84', '284', '176', '5311', '209', '248', '5138', '5064', '325', 
    '171', '348', '255', '274', '373', '5915', '318', '5076'], ok_filiali.banker) 

現在我已經使用上述查詢和手動創建2個陣列。

我需要做的是動態地創建用於Order By 2門陣列(直接使用2個查詢)

是否有可能做到這一點?

感謝

回答

3

如果我理解正確你的目標是基於某些值出現的頻率你的結果進行排序。這很容易完成與window functions

SELECT mandato, raggruppamento, banker, 
    COUNT(*) OVER (PARTITION BY raggruppamento) AS order1. 
    COUNT(*) OVER (PARTITION BY banker) AS order2 
FROM ok_filiali 
ORDER BY order1 DESC, order2 DESC; 
+0

謝謝埃爾克,你的查詢確實是我需要的。 – 2012-03-18 15:25:59