您在這裡

Views aggregation 問題

fuchen's 的頭像
fuchen 在 2012-12-18 (二) 12:03 發表

我想做到aggregation Taxonomy name 的功能 如圖這篇主題Taxonomy aggregation of name fields
不過我看不出來下面是要解決這個問題,好像只是為了支持D8。

可是我測試現在的Views 3.5的aggregation
CCK欄位可以使用Group column設定
無設定有設定
其他欄位不行
如node nid title...
Taxonomy 下的項目或詞彙都不行用Group column設定
我像做到下面這種SUM的功能,不過我使用的欄位不支援Group column
http://drupal.org/node/1463500
http://drupal.org/node/1451894#comment-5704194
請問有什麼方法可以解決....

我只是想在SQL後面加Group by Taxonomy name 求欄位的SUM...請問各位有何解法

在網路上查到這篇Using hook_views_alter to Add a "GROUP BY" Statement
分別試了hook_views_query_alter和hook_views_pre_view

function views_override_sql_views_query_alter(&$view, &$query) {
if ($view->name == 'views_name') {
//loop through the fields and add the aggregate indicator
foreach($query->fields as $key => $field) {
$query->fields[$key]['aggregate'] = TRUE;
}
$query->add_groupBy('taxonomy_term_data_name');
}
}

使用taxonomy_term_data_name為group後raw SQL沒有加上 Group,倒是把taxonomy_term_data_name欄位給用消失了...

Views Issues裡面有很多人在問這個問題
目前我已經解決這個問題
雖然做法非常粗糙....

我的做法是一樣是使用hook_views_query_alter
但是原本views模組下的plugins/views_plugin_query_default.inc
是不單單支援Groupby...要經過一堆條件比如說使用having才能用
但是aggregation又不一定是使用having所以..我的做法是在後面加了一對code
假若無having但有groupby參數就加使用groupby(field)...

views_plugin_query_default.inc在Views Issues裡面有很多種改法,也有人試著改成可以支援UI,不過我還沒去試就是了,未來版本應該會支援吧。

Re: jamesliu78
Views原本使用的COUNT、SUM有大的限制...是對整個資料做COUNT和SUM跟Views calc模組一樣是總計算
Views Raw SQL模組試著達到我的目的...但是沒支援到Groupby的控制。
Views Field Views模組功能類似Views Raw SQL模組的子查詢,就是查詢完的資料參數再扔到Views裡做查詢。
Views PHP模組 就只是PHP運行的部分...

如果有人有更好的解法,可以提出來討論...

基本上他目前GROUP BY的功能views都有支援到了,
我目前使用也是正常的(count distinct),
如果你得把select的結果拿回來再做第二次select的話,
我會建議你使用views in views或是他的views_get_view的function的方式會比較好。
(把一個views當成一條sql)
(請參考views頁首、頁尾的功能)

我是神豬~神豬的神,神豬的豬.......

基本上他目前GROUP BY的功能views都有支援到了 => 可是我測試node節點CCK的欄位都有支援到,但taxonomy term沒有,分類項目和詞彙都沒有

views in views或是他的views_get_view的function的功能是指類似 Views Field View模組所展現的功能,
我一開始是使用這個方法,但是taxonomy term好像沒法做我想要的aggregation功能所已放棄了...

不好意思~請問您所提到的views頁首、頁尾是指甚麼意思?
是說我把View插入另一個View的頁首頁尾嗎
那有什麼用? 老實說我覺得只是很像Panel排版把其他View加在前後而已,但排版又沒有Panel好用,實在看不出這功能的用處...

如果解得這麼複雜那是不是直接寫一個新的module去處理更直接?像我現在產生各taxonomy的最新文章列表時就是用一個自己寫的小module,hook_node_insert/hook_node_update/hook_node_delete底下用drupal_write_record寫入一個專用的table。

"老實說我對Drupal API還不太懂...本來是不想懂得...懂的話早就自己下去搞了...
原本只是想用現有模組達成目的...(這不是所見所得的CMS嗎!?)"
以上是抱怨別理我

hook_node_insert/hook_node_update/hook_node_delete和drupal_write_record 我都還沒用過也不知道,
不過感覺利用節點內容新增刪除更新做處理....這樣解決到是很快 (傳說中的暴力破解─我不懂你自己做還不行嗎...)
感謝提供這個方法...

我也是用來產生各taxonomy的最新文章列表計算未讀數
產生的SQL查詢如下:

SELECT taxonomy_term_data_taxonomy_term_hierarchy.name AS taxonomy_term_data_taxonomy_term_hierarchy_name, taxonomy_term_data_taxonomy_term_hierarchy.vid AS taxonomy_term_data_taxonomy_term_hierarchy_vid, taxonomy_term_data_taxonomy_term_hierarchy.tid AS taxonomy_term_data_taxonomy_term_hierarchy_tid, taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_vocabulary.machine_name AS taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_vocabul, taxonomy_term_data.name AS taxonomy_term_data_name, taxonomy_term_data.vid AS taxonomy_term_data_vid, taxonomy_term_data.tid AS tid, taxonomy_vocabulary.machine_name AS taxonomy_vocabulary_machine_name, GROUP_CONCAT( taxonomy_term_data.tid SEPARATOR '+') AS raw_sql_field, SUM( (SELECT count(0) FROM taxonomy_index
LEFT JOIN node ON (taxonomy_index.nid = node.nid)
LEFT JOIN history ON node.nid = history.nid AND history.uid = '1'
INNER JOIN node_comment_statistics ON node.nid = node_comment_statistics.nid
WHERE (taxonomy_index.tid = taxonomy_term_data.tid AND (( (node.status = '1') AND ((history.timestamp IS NULL AND (node.changed > (1355719755 - 2592000) OR node_comment_statistics.last_comment_timestamp > (1355719755 - 2592000))) OR history.timestamp < node.changed OR history.timestamp < node_comment_statistics.last_comment_timestamp) )))
) ) AS raw_sql_field_1
FROM
{taxonomy_term_data} taxonomy_term_data
LEFT JOIN {taxonomy_term_hierarchy} taxonomy_term_hierarchy ON taxonomy_term_data.tid = taxonomy_term_hierarchy.tid
INNER JOIN {taxonomy_term_data} taxonomy_term_data_taxonomy_term_hierarchy ON taxonomy_term_hierarchy.parent = taxonomy_term_data_taxonomy_term_hierarchy.tid
LEFT JOIN {taxonomy_term_hierarchy} taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_term_hierarchy ON taxonomy_term_data_taxonomy_term_hierarchy.tid = taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_term_hierarchy.tid
INNER JOIN {taxonomy_term_data} taxonomy_term_data_taxonomy_term_hierarchy_1 ON taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_term_hierarchy.parent = taxonomy_term_data_taxonomy_term_hierarchy_1.tid
LEFT JOIN {taxonomy_term_hierarchy} taxonomy_term_data_taxonomy_term_hierarchy_1__taxonomy_term_hierarchy ON taxonomy_term_data_taxonomy_term_hierarchy_1.tid = taxonomy_term_data_taxonomy_term_hierarchy_1__taxonomy_term_hierarchy.tid
INNER JOIN {taxonomy_term_data} taxonomy_term_data_taxonomy_term_hierarchy_2 ON taxonomy_term_data_taxonomy_term_hierarchy_1__taxonomy_term_hierarchy.parent = taxonomy_term_data_taxonomy_term_hierarchy_2.tid
LEFT JOIN {taxonomy_vocabulary} taxonomy_vocabulary ON taxonomy_term_data.vid = taxonomy_vocabulary.vid
LEFT JOIN {taxonomy_vocabulary} taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_vocabulary ON taxonomy_term_data_taxonomy_term_hierarchy.vid = taxonomy_term_data_taxonomy_term_hierarchy__taxonomy_vocabulary.vid
WHERE (( (taxonomy_vocabulary.machine_name IN ('forums')) ))
GROUP BY taxonomy_term_data_name
ORDER BY taxonomy_term_data_taxonomy_term_hierarchy_name ASC, taxonomy_term_data_name ASC

Views匯出code沒意義因為中間用了hook修改了

另外因為我的功能要求包含filter和contextual filters,
所以才選用Views的expose filter和contextual filters使用...

  我個人的Drupal經歷比較奇怪一點所以會一開始就選擇自製module...因為剛入門就埋頭寫自己專用的template但沒有理解好Drupal的設計,所有東西都在Content type的版型上被寫死了而不包含任何的region(使用上是沒什麼影響反正這是從頭到尾定製的個人網站),導致我現在幾乎只會和API打交道,一般人常用的Views之類高階模組反而不知道怎麼操作。所以我沒辦法判斷你這個問題能不能從現有模組的角度去解決,但可以指出API路線要怎麼走這樣。

我能夠理解,畢竟我後來覺得如果不是要整合其他模組,我也不會從Views下手,應該我常想如果直接給我一個文字方塊去編輯Sql就好....
現在也再看Database API
因為很多功能整合需要自己下手...