以ä¸ã®ããã«「,」åºåãã®æåãé£çµããã¦ããç¶æ ã§ã°ã«ã¼ãåãè¡ãããã®æä½ãè¨è¿°
CREATE TABLE nodes(tags TEXT); INSERT INTO nodes VALUES('tag1,tag2,tag3'); INSERT INTO nodes VALUES('tag1,tag3'); INSERT INTO nodes VALUES('tag1,tag3'); INSERT INTO nodes VALUES('tag1,tag4,tag5');ãã®ãã¼ã¿ã以ä¸ã®ããã«ããã
tag | count -------+------- tag1 | 4 tag3 | 3 tag2 | 1 tag4 | 1 tag5 | 1
ãã®ãããªå¦çã¯ã©ã®ããã«ã¨å¼ã¶ã®ãåãããªãããã¥ã¢ã³ã¹çã«ã¯
・åºåãæåé åå
・ãã¼ã¹é åå
・æåååå²çµå
ãªã©ã?
æ£å¼å称ãç¥ããã・・・・
ã¨ããããSQLã ãè¨è¼
SELECT tag, COUNT(tag) FROM (SELECT UNNEST(STRING_TO_ARRAY(tags, ',')) FROM nodes) AS T1 GROUP BY tag ORDER BY COUNT(tag) DESC, tag;UNNESTã¨STRING_TO_ARRAYãå©ç¨ãããã¨ã§å¯¾å¿ãå¯è½ã§ãã。
UNNESTã§æ¤ç´¢ãè¡ãã¨ããããå¿ç¨ãåºã¦ããã®ã§åèã«ãã¦ã¿ãã。
ã¨ããããèªåã®ç®çã¯ä¸è¨ã§è§£æ±ºã§ã。