ã¯ããã«
ãã¯ã³ã´æè²äºæ¥ã§ãã¼ã¿ã¢ããªã¹ãã¨ãã¦åãã¦ããå°æã§ãã
ä¸è¬çã«ãã¼ã¿ã¢ããªã¹ãã¯ãã¼ã¿ã®åéã»åæãéãã¦çµç¹ã®ææ決å®ãæ¯æ´ããå½¹å²ãæå¾
ããããã¨ãå¤ãããã¯ã³ã´æè²äºæ¥ã«ãããç§ã®ããã·ã§ã³ãKPIååã®å¯è¦åãããã·ã¥ãã¼ã / ã¬ãã¼ãã®ä½æã»æä¾ãéãã¦ãã¼ã¿ããªãã³ãªçµç¹ã«è²¢ç®ããã¨ããã«ããã¾ãã
ç§ãã¡æè²äºæ¥ã«ã¯æ½çãå®è¡ããä¼ç»è
ããã¸ãã¹ä¸ã®ææ決å®è
ã ãã§ãªãããµã¼ãã¹ãæ´»ç¨ãã¦æè²ã®ç¾å ´ã«ç«ã£ã¦ããæ¹ã
ããµã¼ãã¹ã«å±éãã¦ããææãå¶ä½ãã¦ãããã¼ã ãªã©å¤æ§ãªæ¹é¢ãããã¼ã¿åéã»åæã®éè¦ãããã¾ããããã ãã«ããããã大ãã楽ããæ¥ã
ãéããã¦ãã¾ãã
課é¡ã«ã¤ãã¦ï¼å°å ¥ã«ä»£ãã¦ï¼
ã¯ã¨ãªãæ¸ãã¦ãçµæãåæãã¦ãè³æã«ã¾ã¨ãã¦ãå±éãã¦ãå
±æãã¦ã»ã»ã»ã¿ãããªä»äºããã¦ããã¨SQLã§æ½åºãã縦æã¡ã®ãã¼ã¿ã横æã¡ã«ä½ãå¤ããããã¨ãããããã¾ãã
ãã¯ã³ã´æè²äºæ¥ã§ã¯GoogleCloudConsoleããBigQueryï¼ä»¥ä¸BQï¼ã使ã£ã¦ãã¼ã¿éè¨ãããã¨ãå¤ãã®ã§ãããBQæ¥åã§ãé »ç¹ã«ãããªå ´é¢ãçã¾ãã¾ãã
å
¸åçãªç¸¦æã¡ãã¼ã¿ã¯ä»¥ä¸ã®ãããªåºåãã¼ãã«ãæ³å®ã§ãã¾ãã
縦æã¡ãã¼ãã«ã®ä¾
Title : æéãã¥ã¼æ°ï¼2023å¹´4æï½2024å¹´3æï¼
ãã¼ã¸å | å¹´æ | ãã¥ã¼æ° |
---|---|---|
TOP | 2023-04 | 1,000 |
ç»é²ãã¼ã¸ | 2023-04 | 100 |
ååãã¼ã¸A | 2023-04 | 600 |
ååãã¼ã¸B | 2023-04 | 200 |
TOP | 2023-05 | 1,500 |
ç»é²ãã¼ã¸ | 2023-05 | 200 |
ååãã¼ã¸A | 2023-05 | 500 |
ååãã¼ã¸B | 2023-05 | 400 |
ã»ã»ã» | ã»ã»ã» | ã»ã»ã» |
TOP | 2024-03 | 5,000 |
ååãã¼ã¸D | 2024-03 | 1,000 |
ä»ã©ãã¯BIãã¼ã«ï¼TableauãLooker Studioãªã©ï¼ã«ãã®ã¾ã¾æãã¦è¯ãæãã«å¯è¦åãã¦ããããã¨ãå¢ãã¾ããããã¢ãããã¯ãªåæãä¸ç¹å®å¤æ°ã«åããè³æãä½æããå ´åãæå
ã§ææ©ãæ´å½¢ãããã¹ãå ´é¢ãåºã¦ãã¾ãã
横æã¡ãã¼ã¿ã¨ã¯ãå
·ä½çã«ã¯ä»¥ä¸ã®ãããªåºåãã¼ãã«ã§ãã
横æã¡ãã¼ãã«ã®ä¾
Title : æéãã¥ã¼æ°ï¼2023å¹´4æï½2024å¹´3æï¼
ãã¼ã¸å\å¹´æ | 2023-04 | 2023-05 | ã»ã»ã» | 2024-03 |
---|---|---|---|---|
TOP | 1,000 | 1,500 | ã»ã»ã» | 5,000 |
ååãã¼ã¸A | 600 | 500 | ã»ã»ã» | 300 |
ååãã¼ã¸B | 200 | 400 | ã»ã»ã» | 600 |
ç»é²ãã¼ã¸ | 100 | 200 | ã»ã»ã» | 300 |
ååãã¼ã¸D | ã»ã»ã» | ã»ã»ã» | ã»ã»ã» | 1,000 |
æ¬è¨äºã§ã¯ããã®ãããªç¸¦æã¡ãã¼ãã«â横æã¡ãã¼ãã«ãå®ç¾ããæ段ã®ä¸ããå人çã«å¥½ããªæ段ã3ã¤æ示ãã¾ãã
æ段ç®æ¬¡
- 表è¨ç®ã¢ããªã±ã¼ã·ã§ã³ã®é¢æ°ã§è§£æ±º
- SQLå®çµã§ãå¯ç¨æ§ã«çå!? PIVOTæ§æã使ã£ã¦ã¿ã
- å®å ¨åçï¼ LOOPæ§æã§åçã«æ¨ªã«ä¼¸ã°ã
- ï¼å¾æ¥è«ï¼ãããæ£è§£
æ段â ï¼è¡¨è¨ç®ã¢ããªã±ã¼ã·ã§ã³ã§è§£æ±º
ãªããããããï¼ï¼ã¨ããæãã§ãããèªåã®æ¥åã®ä¸ã§ç¹ã«ã¢ãããã¯ãªåææ¥åã ã¨ã90%ã¯ãã®æ段ãã¨ãã¾ãããåç¥ã®éãããããæ©è½ãã³ãããªã©ã®æ§ã
ãªæ段ãèããããã¨ãããããã§ã¯é¢æ°ï¼ï¼é¢æ°ã®ã³ããï¼ã使ã£ã解決æ³ãä¾ç¤ºãã¾ãã
以ä¸ãæ½åºçµæãã³ããããã·ã¼ãã¨ãã¦æ¨ªæã¡å¤æã®ããã«é©åãªé¢æ°ãèãã¦ã¿ã¾ãã
ä½æ¥ããã·ã¼ãä¾
ã¾ããå´ãç¨æããããEåã«ãã¼ã¸åã並ã¹ãã¨ãã¾ããå
·ä½çã«ã¯ = UNIQUE(A:A) ã¿ãããªé¢æ°ãå©ç¨ãã¦ããããæä½ãã£ã½ãè³æã«å¿
è¦ãªãã¼ã¸åã ãã並ã¹ã¦ããããåæ§ã«Få以å³ãé©å½ã«ããã¾ãã
ãã®æãF2ã»ã«ã«è¯ãæãã®é¢æ°ãæ¸ãã¦è¡¨å
ã«ã³ãããããã¨ã§æ¨ªæã¡ã®è¡¨ãä½æãã¾ãããã¦ãã©ããªé¢æ°ãæ¸ãã°ããã§ããããï¼
æ¬é¡ã§ã¯ãªãã®ã§ããã£ã¨ããã¾ãã
é¢æ°ä¾
= SUMPRODUCT(($A:$A = $E2) * ($B:$B = F$1) , $C:$C)
çµæãæ£ããåºã¦ããããªã®ã確èªããããé¢æ°ãã³ãããã¦è§£æ±ºã§ãã
ã¡ãªãã
- ææ©ã
ãã¡ãªãã
- ã¢ãããã¯ãªæ¥åã«éå®ãããï¼åçãªæ¨ªæã¡ã¨ã¯ç¨é ãï¼
æ段â¡ï¼SQLå®çµï¼ PIVOTæ§æã使ã£ã¦ã¿ã
表è¨ç®ï¼ãããããªããã ãï¼ ã¨ããæ¹ã«ã¯ãã¡ãããæ¤è¨ãã ããã
æ³å®ã±ã¼ã¹ã¨ãã¦ãã¼ãã«ãmonthly_pvãã«ã¯ä»¥ä¸ã®ãããªã«ã©ã æ§æã§ã¬ã³ã¼ããå
¥ã£ã¦ããã¨ãã¾ãã
TableName : monthly_pv
ã«ã©ã å | å | ä¾ |
---|---|---|
page_title | STRING | TOP |
access_month | STRING | 2023-04 |
monthly_view | INT64 | 123 |
ã¤ã¾ãã SELECT * FROM monthly_pv ORDER BY access_month; ã¨ããã¯ã¨ãªãæããçµæãåé ã®ç¸¦æã¡ãã¼ã¿ã®ä¾ã§ãã
GoogleSQLã«ã¯PIVOTæ§æãç¨æããã¦ãã¦ãéè¨é¢æ°ã¨çµã¿åããããã¨ã§ã¹ãã¼ãï¼ãã¯ãã«ã«ç¸¦â横ãå®ç¾ãããã¨ãåºæ¥ã¾ãã
å
·ä½çã«ã¯ä»¥ä¸ã®ãããªã¯ã¨ãªã§ãã
SELECT * FROM `monthly_pv` PIVOT ( MAX(monthly_view) FOR access_month IN ('2023-04','2023-05','2023-06','2023-07','2023-08','2023-09','2023-10','2023-11','2023-12','2024-01','2024-02','2024-03') );
ãã®æ§æã¯éè¨é¢æ°ã¨çµã¿åããããã¨ã§å®åãçºæ®ãã¾ããä¸ã®ä¾ã ã¨ä»®ã«ååæãã¨ã®ç·viewæ°ã足ãè¾¼ã¿ããï¼ããã¦ä»®ã«quarterã¿ãããªã«ã©ã ãããï¼ã¨ããã¨ãMAXé¢æ°âSUMé¢æ°ã«å¤æ´ãFOR ~ INã®é¨åãä¿®æ£ããã ãã§ãã
å®éã®æ¥åãã¼ã¿ã¯ããã¡ãã£ã¨è¤éã§ä¸çç¸ã§ã¯ãããªãããããã¾ããããç¥ããªãã¨SELECTæã¨ãã¦å»¶ã
ã¨CASE WHENãæ¸ãé£ãããã¨ã«ãªãã®ã§ããã¹ã¿ã¼ãããã¨ããã§ãã
ã¨ããã§ãããã¾ã§ãã£ããFOR access_month IN ï½ã®ã¨ãããåçã«æ¸ããããã¨æãã®ã¯èªç¶ãªãã¨ã§ããFOR access_month IN (SELECT DISTINCT access_month FROM monthly_pv ORDER BY 1) ã¿ãããªæ§æã許ããããªãæé«ãªã®ã§ããã»ã»ã»ããã¯éãã¾ããï¼ããè¦ãã¨ãã¡ããã¡ããªæ§æã§ãããæç´ã«æååãçæãã¦å¤æ°ã¨ãã¦INã®ä¸ã«çªã£è¾¼ãã§ããã¾ãããã¾ããï¼ããã®ãããæ¨ªå¹ ã¨ãªãã«ã©ã ãåçã«çæãããã¨ãé£ããã¹ã±ã¸ã¥ã¼ã«ã¿ã¹ã¯ã«ä¹ãã¦èªååãããããã®ã¯é©å½ã§ã¯ããã¾ããããã®æ段ã®æ大ã®ä¸æºãã¤ã³ãã§ããã
ã¡ãªãã
- BigQueryå®çµ
ãã¡ãªãã
- INã®ä¸èº«ãææ¸ãããªããã°ãªããªãï¼åçã«ä½ããªãï¼
æ段â¢ï¼LOOPã¨ä¸æãã¼ãã«ã使ã£ã¦åçã«æ¨ªæã¡ãã¼ãã«ãçæããï¼ï¼æ¬é¡ï¼
ä¸ã®ä¾ã ã¨12ãæã»ã©ã®æ¨ªæã¡ã«ã©ã ãææ¸ãããã ãã§ããã§ãããä¾ãã°å¤§éã®ææã®åãã¹ããã¼ã¿ã«ã¤ãã¦åè¬ãã¦ãããçå¾ã1ã¬ã³ã¼ãã¨ãã¦åãã¹ãã®ç¹æ°ã横ã§æã¤ãã¼ãã«ãä½ããããã¨ãããããªã±ã¼ã¹ã§ãããã¨ããã¨ãPIVOTæ§æãæ¸ãã ãã§1æéã®éä¸ãè¦ãããã¿ãããªãã¨ã«ãªãããã¾ããããããã¯æ¥æ¬¡ã§æ´æ°ããããã¼ã¿ãã¼ãã«å¯¾ãã¦éè¨çµæãèªåã§å¯è¦åããéã®æ½åºã¯ã¨ãªã¨ãã¦ãé©å½ã¨ã¯ããã¾ããï¼æ°ããªæãè¿ãããã³ã«ã¯ã¨ãªã®ä¿å®ãå¿
è¦ã«ãªã£ã¦ãã¾ãã¾ãï¼ã
ããã§LOOPæ§æã使ã£ã¦è§£æ±ºãã¾ãããï¼ã¨ããã®ããæ¬è³æã®æ¬é¡ã§ãã
åææ¥åã¨ãã¦Pythonãä»ã®ããã°ã©ãã³ã°è¨èªãå©ç¨ããã¦ããæ¹ã«ã¯FORæã¨ãã¦ãªãã¿æ·±ããã®ã§ãããSQLã§LOOPãå©ç¨ã§ãããã¨ãæè¿ç¥ãã¾ããï¼ãã®è¨äºãæ¸ãã¦ã¿ããã¨æã£ãåæ©ã«ãªã£ã¦ãã¾ãï¼ããã£ããå
·ä½çãªã¯ã¨ãªãæ¸ãã¦ã¿ã¾ããä»®æ³ãã¼ãã«ã¨ãã¦åè¿°ã®monthly_pvãå©ç¨ãã¾ãã
--ã«ã¼ãå¤æ° DECLARE idx INT64 DEFAULT 0; --横ã«ä¼¸ã°ãããã®æã©ãã«ã®é å DECLARE month_labels ARRAY<STRING> ; --ã¾ãã¯æ¨ªã«ä¼¸ã°ãæã©ãã«ã管çããããã«é ååãã SET month_labels = ( SELECT ARRAY_AGG(i.access_month) FROM (SELECT DISTINCT access_month FROM `monthly_pv` ORDER BY 1) AS i ); --次ã«åºåç¨ã®ãã¼ãã«ãä½æããããã®æç¹ã§ã¯ãã¼ã¸åã®ã¿ã«ã©ã ã§æ㤠CREATE OR REPLACE TABLE monthly_pv_output AS ( SELECT DISTINCT page_title FROM `monthly_pv` ); --LOOPå¦çéå§ãææ°ã«å¿ãã¦åçã«ã«ã©ã ãçæãã¦JOINãã¦ãã LOOP --ã«ã©ã æä½ãããåã«åºåç¨ãã¼ãã«ãä¸æãã¼ãã«ï¼TEMP TABLEï¼ã«éé¿ CREATE TEMP TABLE output_table AS ( SELECT * FROM `monthly_pv_output` ); --åºåç¨ãã¼ãã«ã«ã«ã©ã ã追å ãBQã®ã«ã©ã å½åè¦åã«å¾ã_ãå é ã«è¿½å ã-ã_ã«ç½®ãæãã EXECUTE IMMEDIATE FORMAT(""" ALTER TABLE `monthly_pv_output` ADD COLUMN %s INT64 """ , REPLACE(CONCAT('_',month_labels[idx]),'-','_')) ; --åºåç¨ãã¼ãã«ãä¸æ¸ããã¾ãã¯å ¨ãã¼ã¿ã¯ãªã¢ DELETE FROM `monthly_pv_output` WHERE page_title IS NOT NULL ; --éé¿ããä¸æãã¼ãã«ã¨JOINãããã«ã©ã ã使ã£ã¦åºåç¨ãã¼ãã«ãæ´æ°ãOUTER JOINãæã¾ãã INSERT INTO `monthly_pv_output` SELECT i.*,j.monthly_view FROM output_table AS i LEFT OUTER JOIN ( SELECT page_title,monthly_view FROM `monthly_pv` WHERE access_month = month_labels[idx] ) AS j ON i.page_title = j.page_title ; --ä¸æéé¿ãã¼ãã«ãDROP DROP TABLE output_table; --LOOPã®ç¶ç¶å¯å¦ãindex = é åé·ã¨ãªã£ããçµäº SET idx = idx + 1; IF idx = ARRAY_LENGTH(month_labels) THEN LEAVE; END IF; END LOOP;
ãã¤ã³ãã¨ãã¦ã¯LOOP 㨠CREATE TEMP TABLEãçµã¿åããã¦åºåç¨ãã¼ãã«ãéé¿âã«ã©ã 追å âéé¿ãã¼ã¿ï¼æ°ã«ã©ã åJOINã§å
¨æ´æ°ã»ã»ã»ãç¹°ãè¿ããã¨ã§ãBQã®é åºä»ããªArrayãã¤ã³ããã¯ã¹ã¨é£åãã¦ç¨ãã¦ãã¾ãã
ãããLOOPã®æ¨å¥¨ãããå©ç¨æ³ãªã®ããã¨ããã®ã¯åããã¾ããããæ§æã使ã£ãèªç±åº¦ã®é«ãã¯ã¨ãªããªã¥ã¨ã¼ã·ã§ã³ããä¼ãã§ããã°å
æ ã§ãã
ã¡ãªãã
- BigQueryå®çµï¼åçã«æ¨ªæã¡ãå®ç¾
ãã¡ãªãã
- å°ãè¤é
å¾æ¥è«
以ä¸ã¾ã§ã®å
容ããã¼ã ã®ååã«è¦ã¦ããã£ãã¨ãããPIVOTã¯åçã«ä½¿ãã¾ãããEXECUTE IMMEDIATE FORMATãå©ç¨ãã¦ï¼ãã¨æãã¦ããããèããå´©ãè½ã¡ã¾ããã
PIVOTæ§æã®ä¸èº«ãå¼·å¼ã«æååçæããã ãã§ã¯ãã¾ããããªãã§ãããEXECUTE IMMEDIATE FORMATã¯åçã«çæããæååãå«ãã¯ã¨ãªæååã解éãã¦ãããã®ã§ãããã§è§£æ±ºã§ãã¾ãããªãã§æãã¤ããªãã£ãâ¦ã
å
·ä½çã«ã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
--横ã«ä¼¸ã°ãããã®æã©ãã«ã®é å DECLARE month_labels ARRAY<STRING> ; --ã¾ãã¯æ¨ªã«ä¼¸ã°ãæã©ãã«ã管çããããã«é ååãã SET month_labels = ( SELECT ARRAY_AGG(i.access_month) FROM (SELECT DISTINCT access_month FROM `monthly_pv` ORDER BY 1) AS i ); --é åãUNNESTå±éããªããæååçæãã EXECUTE IMMEDIATE FORMAT(""" SELECT * FROM `monthly_pv` PIVOT (MAX(monthly_view) FOR access_month IN %s); """, (SELECT CONCAT("(",STRING_AGG(CONCAT("'",months,"'"),","),")") FROM UNNEST(month_labels) AS months))
è¯ãæãã«ã·ã³ãã«ã«æ¸ãã¾ããããããããããå¦ã³ï¼ããããã¼ã ã§åãã¦ããæå³ï¼
ãããLOOPã§ã¯ã¨ãªãæ¸ãã¦åããã¦ã¿ãã¨ãå°ãé·ã横æã¡ãã¼ã¿ãä½ããã¨æãã¨rateLimitExceededã¨ã©ã¼ï¼çæéã§åä¸ãã¼ãã«ã®æ¸ãæããé£ç¶ãã¦è¡ãã¨åºç¾ãã¾ãï¼ãåºã¦ãã¾ããã¨ãããã¨ããä¸å®å ¨ãâ¦ãã¨ãããã¨ã§BQã«ç»é²ããã¦ããèªåã®PROCEDUREã¯ç¡äºã«ãªãã¡ã¯ã¿ãªã³ã°ããã¾ããã
ã¡ãªãã
- BigQueryå®çµï¼åçã«æ¨ªæã¡ãå®ç¾ããããç°¡æ½ã
ãã¡ãªãã
- ãªãï¼ãããæ£è§£ã¨ããã¦ãã ããï¼
ãããã«
æå¾ã¯ãªããä»ãã¦ãã¾ãã¾ããããã¨ã£ã¦ã楽ããSQLã§éã¶ãã¨ãåºæ¥ã¾ããã
ãéæ³ã¯æ¢ãæ±ãã¦ããæãä¸çªãã®ãããã¨æåãªæ¸ç©ã«ãæ¸ãã¦ããã¾ããããå®å
¨ã«åæã§ãã
以ä¸ããã£ã¦SQLã®å®é¨è¨é²ã¿ãããªè¨äºãçµãã¾ãããèªã¿ãã ããããããã¨ããããã¾ããã
We are hiring!
æ ªå¼ä¼ç¤¾ãã¯ã³ã´ã®æè²äºæ¥ã§ã¯ãä¸ç·ã«æªæ¥ã®å½ããåã®æè²ãã¤ããã¡ã³ãã¼ãåéãã¦ãã¾ãã
ç§ã®æå±ãããã¼ã ã¯ä¼ç»è·ã¨ã¨ã³ã¸ãã¢ããã¼ã¿ãµã¤ã¨ã³ãã£ã¹ããæ··å¨ãã¦æ§ã
ãªå®é¨çãªåãçµã¿ãä¸ç·ã«ãã©ã¤ãããã¨ã£ã¦ãåºæ¿çãªè·å ´ã§ãï¼ææããããã®ã¯ååã®ãã¼ã¿ãµã¤ã¨ã³ãã£ã¹ãã§ãï¼ãç°ãªãã¹ãã«ã»ãããæã£ãåãå¿ã®ã¡ã³ãã¼ãéã¾ã£ã¦ããã¨ããã ãã§ã¯ã¯ã¯ã¯ãã¾ãããï¼
ã«ã¸ã¥ã¢ã«é¢è«ãè¡ã£ã¦ãã¾ãããæ°è»½ã«ãé£çµ¡ãã ããï¼
ã«ã¸ã¥ã¢ã«é¢è«å¿åãã©ã¼ã ã¯ãã¡ã
éçºãã¼ã ã®åãçµã¿ãæè²äºæ¥ã®ä»å¾ã«ã¤ãã¦ã¯ãä»ã®è¨äºãæ¡ç¨è³æãã覧ãã ããã