SQLã§ï¼ãã¼ãã«ã«åå¨ããªãè¤æ°è¡ã®ãã¼ã¿ãåå¾ããæ¹æ³ãï¼PostgreSQLã® generate_seriesé¢æ°ããã¹ã¿ã¼ãããï¼
PostgreSQLã§ï¼ãã¼ãã«ã使ããã«ï¼è¤æ°è¡ã®ãã¼ã¿ãåå¾ããæ¹æ³ã
- SQLã®ä¸ã§ï¼å¤§éã®å®æ°ãã¼ã¿ã使ãå¿ è¦ããããï¼ä¾ãã°ï¼ï¼ã¶æéã®æ¥ä»å ¨é¨ã¨ãï¼1000ã¾ã§ã®ç´ æ°å ¨é¨ã¨ãï¼
- ã ãï¼ãããã®ãã¼ã¿ãï¼åãã£ã¦ã©ããã®ãã¼ãã«ã«æ ¼ç´ãã¦ãããã¨ã¯ã§ããªããï¼ãã¼ã¿ãå¤ãããããã»ã»ã»ï¼
ãããªã¨ãï¼ãè¤æ°è¡ã®ä½¿ãæ¨ã¦ãã¼ã¿ãåçã«çæãããã¨ãããã¯ããã¯ãå½¹ç«ã¤ã
ãã¼ãã«ãã·ã¼ã±ã³ã¹ã¯ä½¿ããªãã
以ä¸ã¯ãã®æ¹æ³ã
ç¹ã«ï¼Webã¢ããªã®éçºã§ã©ãå½¹ç«ã¤ã®ãï¼ã¨ããç¹ã«éããç½®ãã¦ããã
ã§ãããã¨ã®ä¸è¦§ï¼
- æ°åï¼æ¥ä»ï¼ãã®ä»ã®å®æ°ãã¼ã¿ã大éã«çæããæ¹æ³ã
- çæãããè¤æ°è¡ã®ãã¼ã¿ã使ã£ã¦ãè£éããè¡ãªããã¨ãã§ããã
- ç¡çããã°ï¼ä¸è¬ã®è¨èªã§ã®ãé åãã¿ãããªãã¨ãSQLã§ã§ããï¼ï¼
ç®æ¬¡ï¼
- ï¼ï¼ï¼è¤æ°åã®ãã¼ã¿ãSQLã§å®ç¾©ãã¦è¿ãæ¹æ³
- ï¼ï¼ï¼generate_series() ã使ã£ã¦ã¿ãã
- ï¼ï¼ï¼åå¾ããé£çªãå å·¥ãã¦ã¿ãã
- ï¼ï¼ï¼è£éããã¦ã¿ãã
- ï¼ï¼ï¼ãé åãã®ãããªãã®ã使ã
- ï¼ï¼ï¼generate_seriesã§ã§ããªããã¨
- è£è¶³
ï¼ï¼ï¼è¤æ°åã®ãã¼ã¿ãSQLã§å®ç¾©ãã¦è¿ãæ¹æ³
ãè¤æ°è¡ãã§ã¯ãªããè¤æ°åãã§ããã°ï¼è©±ã¯ç°¡åã ã
SELECT now(), 'hoge', 1 ;
å®è¡çµæï¼
2010-03-08 12:00:00.000000000 hoge 1
'hoge'ã 1 ã¯å®æ°ï¼now() ã¯é¢æ°å¼ã³åºãã§ããï¼ãããããè©ä¾¡å¼ãã®ä¸ç¨®ã
è©ä¾¡å¼ã¨ã¯ï¼ç°¡åã«è¨ãã°ï¼ãã¼ãã«ã«åå¨ãã¦ããªããã¼ã¿ãã¹ã«ã©ã¨ãã
4.1. èªå½ã®æ§æï¼å®æ°ã«ã¤ãã¦
http://www.postgresql.jp/document/pg8...
è©ä¾¡å¼ã«ã¤ãã¦
http://www.postgresql.jp/document/pg8...
è¤æ°ãåãã®å®æ°ã¨ãï¼è¤æ°åã®é¢æ°å¼ã³åºããã³ã¼ãã£ã³ã°ãããã¨ã¯ç°¡åã ã
ãããããã ã¨æ°ãå¤ããªã£ãå ´åï¼ååã«å¯¾ãã¦ï¼ææ¸ãã§ãã¼ã¿ã®å 容ãæ¸ããªããã°ãããªãã®ã§éå¹ççã
è¤æ°ã®ãã¼ã¿ï¼ãã¼ãã«ã«ç¡ãï¼æ¬ä¼¼çãªã¬ã³ã¼ãï¼ãå¹ççã«çæãã¦ï¼SELECTæã§åå¾ãããã¨ã¯å¯è½ãï¼
ï¼ï¼ï¼generate_series() ã使ã£ã¦ã¿ãã
ãã¹ã°ã¬ã® generate_series ã¨ããé¢æ°ã使ãã°ï¼è¤æ°è¡ã®ãã¼ã¿ãè¿ããã¨ãå¯è½ã
ãã®é¢æ°ã¯ï¼é£ç¶ããæ°å¤ï¼é£çªï¼ãè¤æ°è¡ã§è¿ãã
9.18. éåãè¿ãé¢æ°
http://www.postgresql.jp/document/pg8...
â»postgres8.0以éãæ¡ä»¶
ãã£ãã使ã£ã¦ã¿ããã
1ãã10ã¾ã§ã®é£çªã表示ï¼
SELECT generate_series( 1, 10 ) ;
ã¾ãã¯
SELECT * FROM generate_series( 1, 10 ) ;
å®è¡çµæï¼
1 2 3 4 5 6 7 8 9 10
ã·ã¼ã±ã³ã¹ã¨éãï¼ä¸æçãªé£çªãä½ãããä¿åãããªãã
ã使ãæ¨ã¦ã·ã¼ã±ã³ã¹ãã¨ãã£ãæãã
â»æ¬æ¥ã®ã·ã¼ã±ã³ã¹ã¯ï¼ãã¼ãã«å ã®ç¹å®ã®ã«ã©ã ã«é£çªãä¿åãã¦ããããã®ä»çµã¿ã使ãæ¨ã¦ã§ã¯ãªãã
第ä¸å¼æ°ã§ï¼å»ã¿å¹ ãæå®ã§ããã
SELECT * FROM generate_series( 2, 10, 3 ) ;
å®è¡çµæï¼
2 5 8
ãã¼ãã«ã®ããã«ï¼å¥åï¼ã¨ã¤ãªã¢ã¹ï¼ãå©ç¨ã§ããã
SELECT * FROM generate_series( 1, 10 ) AS s(i) WHERE s.i > 5 ;
å®è¡çµæï¼
6 7 8 9 10
s ãæ¬ä¼¼ãã¼ãã«ã®ã¨ã¤ãªã¢ã¹ã®ãããªãã®ã
i ãæ¬ä¼¼ã«ã©ã åã®ãããªãã®ã
s.iã®ãããã«ï¼temp_table.temp_column ã®ãããªååãã¤ãã¦ã¿ãã°ï¼
æ®éã®ãã¼ãã«ã¨åãããã«ä½¿ãããã ã¨ããäºãä¼ããããããããããªãã
ãããã¯ï¼s(i) ã¨ãããããã«ï¼æãåã£ã¦ arr(i) ã¨ãã¦ã¿ãã°ï¼
é£ç¶ããã¬ã³ã¼ãã並ãã§ãããã ã¨ããææ表示ãããäºãã§ãããããããªãã
æ°å¤ä»¥å¤ã®ãã¨ã«ãå½¹ç«ã¤ã
'hoge'ãï¼ã¤åå¾ãããå ´å
SELECT 'hoge' FROM generate_series( 1, 3 ) ;
å®è¡çµæï¼
hoge hoge hoge
ãã£ãï¼ã¤ãªãææ¸ãã§ï¼åãhogeãã¨ç´æã¡ããã»ããæ©ããï¼
æ°ã大ãããªã£ã¦ããã¨generate_seriesãå½¹ç«ã¤ã
ï¼Excelã§ï¼åæ¹åã®ãªã¼ããã£ã«ãè¡ãªããããªã¤ã¡ã¼ã¸ãï¼
ä¾ãã°ï¼ãã 'hoge' ã®é¨åã random() ã«å¤ããã°ï¼ç¬ç«ããä¹±æ°ããã£ãºãã«è¤æ°åå¾ããã¨ãã§ããã
â»random()é¢æ°ã®ä½¿ç¨æ³ã«ã¤ãã¦ã¯ä¸è¨ãåç §ã
ã©ã³ãã ã«ãã¼ã¿ãåãåºããPostgreSQLã
http://www.programming-magic.com/2008...
æ°åãèªåçæã§ããã®ã§ï¼æ°å¦çãªèª¿æ»ãè¡ãªãã¢ã«ã´ãªãºã ã«ã¯éå®ããã
ï¼ï¼ï¼ã®ç´æ°(ï¼ï¼ï¼ï¼ãå²ãåãæ°)ãå ¨é¨æ±ããSQLï¼
SELECT * FROM generate_series( 1, 100 ) as s(i) WHERE MOD( 100, s.i ) = 0 ;
å®è¡çµæï¼
1 2 4 5 10 20 25 50 100
1000以ä¸ã®ç´ æ°ãæ±ããSQLï¼
SELECT s1.i AS prime FROM generate_series( 1, 1000 ) as s1(i) WHERE s1.i > 1 AND NOT EXISTS( SELECT * FROM generate_series( 1, 1000 ) as s2(i) WHERE s2.i > 1 AND s2.i <= s1.i / 2 AND MOD( s1.i, s2.i ) = 0 ) ORDER BY s1.i ;
å®è¡çµæï¼
2 3 5 7 ... ... 983 991 997
ã¾ãã«ï¼ç®ããã¦ãã³ã¨ããæãã®SQLã§ã¯ãªããï¼
NOT EXISTS(ãã) ã®é¨åã¯ï¼ãs1.i ã«ã¯ç´æ°ãåå¨ããªããï¼ãã¨ããå¤å®å¦çããã¦ããã
ä¸è¨ã®URLã§ã¯ï¼åãäºãããããã«ãã¼ãã«ä½æãå¿ è¦ã
ãããï¼generate_series() ã使ãã°ï¼ãã¼ãã«ã¯ä¸è¦ã ã
ãªã®ã§ï¼æ°è»½ã«SQLãå®è¡ã§ããã
SQLã§æ°å¦ããºã«ã解ãï¼æ°è«ç·¨-解çï¼
http://www.geocities.jp/mickindex/dat...
â»ä¸è¨SQLã«ã¤ãã¦è£è¶³ï¼èªå·±ç¸é¢ãµãã¯ã¨ãªã«ã¤ãã¦ï¼ï¼
NOT EXISTSãã®ãµãã¯ã¨ãªã®é¨åã ããã¾ã¨ãã¦ï¼
- s1.i ã¨ããï¼ä»¶ã®æ°å¤ã渡ãã¨
- ãã®æ°å¤ã«ç´æ°ãåå¨ãããã©ãããå¤å®ãã¦è¿ãã¦ããã
ã¨ããå½¹ç®ãæã£ãï¼å¤å®ç¨ã®ãé¢æ°ãã®ãããªç©ã ã¨æãã°ããã
以ä¸ã¯å¿ç¨ä¾ã
ï¼ï¼ï¼åå¾ããé£çªãå å·¥ãã¦ã¿ãã
ä»æ¥ããï¼æ¥éã®æ¥ä»ã表示
SELECT current_date + arr.i FROM generate_series( 0, 6 ) AS arr( i ) ;
å®è¡çµæï¼
2010-03-09 2010-03-10 2010-03-11 2010-03-12 2010-03-13 2010-03-14 2010-03-15
ã¡ãã£ã¨å·¥å¤«ãã¦ï¼ä»æ¥ã®æ¥ä»ã®ã¨ããã«ã¢ã¤ã³ã³ãåºãã¦ã¿ããã
SELECT CASE WHEN arr.i = 0 THEN 'â ä»æ¥â ' WHEN arr.i < 0 THEN 'ã â ã' ELSE '' END, to_char( current_date + arr.i, 'YYYY/MM/DD' ), to_char( current_date + arr.i, 'Day' ) FROM generate_series( -7, 7 ) AS arr( i ) ;
å®è¡çµæï¼
ã â ã 2010/03/02 Tuesday ã â ã 2010/03/03 Wednesday ã â ã 2010/03/04 Thursday ã â ã 2010/03/05 Friday ã â ã 2010/03/06 Saturday ã â ã 2010/03/07 Sunday ã â ã 2010/03/08 Monday â ä»æ¥â 2010/03/09 Tuesday 2010/03/10 Wednesday 2010/03/11 Thursday 2010/03/12 Friday 2010/03/13 Saturday 2010/03/14 Sunday 2010/03/15 Monday 2010/03/16 Tuesday
ã¡ãã£ã¨ããã«ã¬ã³ãã¼ã
ãã¼ãã«ä¸è¦ã¨ããã®ãè¯ãã¨ããã
â»å¿µã®ãã注ææ¸ãï¼ã¬ã¤ã¢ã¦ããè¦ãæ¹ã«é¢ããå¦çã¯ï¼æ¬å½ã¯DBãè¡ãªãã¹ãã§ã¯ãªãã
PostgreSQL ã§é£çªãçæãã generate_series
http://www.deftrash.com/blog/archives...
ããã¾ã§ããã°ã©ã ã§ããããé å¼µã£ã¦ããé¨åã®ããã¤ãã¯ãgenerate_series ã使ããã¨ã§ãSQLã ãã§æ¸ããããã«ãªã
é£çªãã¼ã¿çææ©ã®generate_series(start, stop, step)
http://d.hatena.ne.jp/poch-7003/20091...
to_charã§å©ç¨å¯è½ãªãã©ã¼ãããã®ä¸è¦§
http://www.postgresql.jp/document/pg8...
ï¼ï¼ï¼è£éããã¦ã¿ãã
足ããªãè¡ãèªåçã«åãåããã¦ãããï¼ã¨ããæå³ã§ã®ããã¼ã¿è£éãããã£ã¦ã¿ããã
æ稿ãããè¨äºã管çããããã® articles ã¨ãããã¼ãã«ãããï¼
åã¬ã³ã¼ãã«ã¯æ°è¦ä½ææ¥æã¨ã㦠created_at ã¨ããTIMESTAMPåã®ã«ã©ã ãããã¨ããã
ï¼Ruby on Railsã§ããã°ï¼created_atã¯ããã£ã¡ã ãç®ã«ããã¯ãï¼
æ稿件æ°ãæ¥å¥ã«éè¨ãããå ´åï¼ä¸è¨ã®ãããªSQLã«ãªãã ããã
--ï¼ï¼ï¼ SELECT to_char( created_at, 'YYYY/MM/DD' ) as c, count(*) FROM articles GROUP BY c ORDER BY c ;
å®è¡çµæã®ä¾ï¼
2010/01/02 2 2010/01/03 1 2010/01/05 3 2010/01/08 3 2010/01/09 1
group by created_atã ã¨ï¼æ¥åä½ã§éè¨ãããï¼ãã¤ã¯ãç§åä½ã§éè¨ããã¦ãã¾ãï¼TIMESTAMPåã ããï¼ã
ããã§to_charé¢æ°ã§ãã©ã¼ããããå¹´ææ¥ã«ããã°ï¼åãæ¥ä»ã®ã¬ã³ã¼ãã¯åãé¨åéåã«å±ããããã«ãªãããï¼æ¥åä½ã§éè¨ãããã¨ãã§ããã
timestampåã®ã«ã©ã ãæ¥ä»ã§group byããã
http://blog.goo.ne.jp/rumine/e/3bc25d...
ãã¦ï¼ä¸è¨ã®å®è¡çµæã«ã¯ã0件ãã¨ããè¡ãåºã¦ããªãã
æ稿ãç¡ãã£ãæ¥ã¯ï¼çµæã«ç¾ããªãã®ã ã
ãããï¼éè¨è¡¨ãä½ã£ããï¼æãç·ã°ã©ããæç»ããæãªã©ã«ã¯ï¼
ã0件ãã®æ¥ãå«ãã¦éè¨ãããã¨ããå ´åãããã
ï¼æ¯æãã®ã¬ã³ã¼ããè£éãããï¼ã¨ãããã¨ãï¼
ãã®ããã«generate_seriesãå½¹ç«ã¤ã
ã¾ãï¼ç¹å®ã®æ¥ãã31æ¥éã®ããã ã®å ¨æ¥ä»ã¯ï¼ä¸è¨ã®SQLã§åå¾ã§ããï¼
--ï¼ï¼ï¼ SELECT '2010-01-01'::Date + arr.i FROM generate_series( 0, 30 ) as arr(i) ;
å®è¡çµæï¼
2010-01-01 2010-01-02 2010-01-03 ... ... 2010-01-29 2010-01-30 2010-01-31
ã::Dateãã¯åå¤æã®æ¼ç®åã
Type casting
http://www.microolap.com/products/con...
ãã®å
¨æ¥ä»ï¼ï¼ï¼ã¨ï¼æ¥å¥ã®éè¨çµæï¼ï¼ï¼ã¨ãï¼JOINããã°ããã
--ï¼ï¼ï¼ï¼ï¼ï¼ï¼ã¨ï¼ï¼ï¼ãJOINãããã® SELECT master_calendar.date1, COALESCE( counts_by_date.cnt, 0 ) as cnt FROM ( SELECT to_char( '2010-01-01'::Date + arr.i, 'YYYY/MM/DD' ) as date1 FROM generate_series( 0, 30 ) as arr(i) ) as master_calendar LEFT JOIN ( SELECT to_char( created_at, 'YYYY/MM/DD' ) as date2, count(*) as cnt FROM articles GROUP BY date2 ) as counts_by_date ON master_calendar.date1 = counts_by_date.date2 ORDER BY master_calendar.date1 ;
å®è¡çµæï¼
2010/01/01 0 2010/01/02 2 2010/01/03 1 2010/01/04 0 2010/01/05 3 ... ... 2010/01/30 0 2010/01/31 0
0件ã®æ¥ã®ã¬ã³ã¼ããè£éããã¦ããã
counts_by_dateã«åå¨ããªãæ¥ä»ã®éè¨çµæã¯ï¼JOINæã«NULLã¨ãã¦è¨ä¸ãããããï¼
COALESCEã使ã£ã¦ï¼NULLã0ã«å¤æãã¦ããã
DBå´ã§ãããã£ãè£éãæ¸ã¾ãã¦ããã°ï¼
éè¨çµæãåãåã£ãã¢ããªå´ã¯ï¼ãã¼ã¿ã®æ´åæ§äºã ã«ç ©ããããªãã
ã¢ã¯ã·ã§ã³ã®æ¯ãåããï¼ç»é¢ä¸ã§ã®è¦ãæ¹ã ãã«å°å¿µã§ããã
ä¾ãã°ï¼ä¸è¨ã®ã°ã©ãæç»ã©ã¤ãã©ãªï¼jQuery.flotï¼ã使ã£ã¦ï¼ãã©ã¦ã¶ä¸ã§æãç·ã°ã©ããæç»ããå ´åã
JavaScript ã§ï¼ã¯ãªãã¯ãã座æ¨ã«ç¹ã追å ã§ããã°ã©ããã£ã¼ããæç»ããæ¹æ³ãï¼jQuery ã®ãã©ã°ã¤ã³ jquery.sparklines / jquery.flotã®ä½¿ãæ¹)
http://language-and-engineering.hatenablog.jp/entry/20081120/1227114053
æ¥ä»ã¨ï¼åæ¥ä»ã«ããããã¼ã¿å¤ããã®ã¹ã¯ãªããã«èªã¿è¾¼ã¾ããããã«ã¯ï¼ä¸è¨ã®ãããªã³ã¼ããæ¸ãã
$.plot( $( æç»é åè¦ç´ ), [ { label : "ç³»åå", data : [ // é£çª, y軸ãã¼ã¿å¤ [ 0, 0 ], [ 1, 2 ], [ 2, 0 ], [ 3, 1 ], ... [ 27, 3 ], [ 28, 0 ], [ 29, 0 ], [ 30, 0 ] ] } ], { lines : { show : true }, points : { show : true }, xaxis : { ticks : [ // é£çª, x軸ç®çãã®ã©ãã« [ 0, "2010/01/01" ], [ 1, "2010/01/02" ], [ 2, "2010/01/03" ], [ 3, "2010/01/04" ], ... [ 27, "2010/01/28" ], [ 28, "2010/01/29" ], [ 29, "2010/01/30" ], [ 30, "2010/01/31" ] ] }, yaxis : { tickDecimals : 0 } } );
ticksã¨dataã®ã¨ããã«ï¼ããããx軸ã»y軸ã®å¤ãããã
ï¼ãã®é¨åã®ã³ã¼ãã¯ï¼ãµã¼ãå´ã§DBãèªã¿ãªããåçã«çæãããã¨ã«ãªãã ãããï¼
ããDBå´ã§å ã»ã©ã®ããã«å¤ã®è£éãæ¸ã¾ãã¦ããã°ï¼ãã®ã¹ã¯ãªããå ã§ã¯ï¼0件ã®å ´åã®ä¾å¤å¦çãæ°ã«ããå¿ è¦ããªãã
ããã¦ï¼æ¥ä»ãã¹ã¿ãã¼ãã«ã¿ãããªãã®ãç¨æãããã¨ãªããããå®ç¾ããããã«ã¯ï¼generate_seriesã®ãä¸è©±ã«ãªãã®ã ã
ï¼ï¼ï¼ãé åãã®ãããªãã®ã使ã
ããã¾ã§ã§ï¼è¤æ°è¡ã®ãã¼ã¿ããã¼ãã«ç¡ãã«åçã«çæããæ¹æ³ã¨ãã¦ï¼é£ç¶æ°å¤ãé£ç¶æ¥ä»ãæ±ã£ãã
ããã«ä¸æ©é²ãã¦ï¼é£ç¶æ§ã®ç¡ãï¼ä»»æã®ä¸¦ã³é ã®ãã¼ã¿ãè¤æ°è¡åçæãã¦ã¿ããã
以ä¸ã®ãããªåºåãå¾ããã¨ããã
ãã ãï¼ãã®ãããªå¤ãè¨é²ããããã¼ãã«ã¯ã©ãã«ããªãã
a b c
ããã§ï¼ä¸è¬çãªããã°ã©ãã³ã°è¨èªã§ã®ãé
åããã¤ã¡ã¼ã¸ãã¦ã¿ããã
Cè¨èªãVBã®éçé åã®å ´åï¼é åã使ãã¨ãã¯ã»ã»ã»
- ã¾ãï¼é åã宣è¨ãããå¤æ°åã¨ï¼é åã®ãµã¤ãºã決ããã
- 次ã«ï¼é åã®ä¸ã«ï¼é çªã«è¦ç´ ãæ ¼ç´ãã¦ããã
ã¨ããæé ã ã
ããã¨åãäºãï¼SQLã§å®ç¾ãã¦ã¿ããã
SELECT CASE i WHEN 1 THEN 'a' WHEN 2 THEN 'b' WHEN 3 THEN 'c' END as value FROM generate_series( 1, 3 ) as arr(i) ;
å®è¡çµæï¼
a b c
FROMå¥ã®ä¸ã§ã¯ï¼ããã°é
åã®ã宣è¨ããè¡ãªã£ã¦ããã
ååã¯arrã§ï¼ãµã¤ãºã¯3ã ã
SELECTå¥ã®ä¸ã§ã¯ï¼ããã°è¦ç´ ã®ãæ ¼ç´ããè¡ãªã£ã¦ããã
é åã®æ·»ãåã1, 2, 3ã®ããããã®å ´åã«ã¤ãã¦ï¼ããã«ããã¹ãè¦ç´ ã®å 容ãæå®ãã¦ããã
æç´ãªã³ã¼ãã§ã¯ãããï¼è¤æ°è¡ã®å®æ°ãSQLã ãã§çæãããã¨ãã§ããã
â»SELECTå¥ã®ä¸èº«ãå¢ããã°ï¼è¤æ°è¡ãã¤è¤æ°åã®ãã¼ã¿ãOKã
ãªãï¼è¤æ°ãåãã®ã»ããå§åçã«ç°¡åã ã
SELECT 'a', 'b', 'c' ;
ã ãã§æ¸ãã®ã ããã
ããã§ãï¼ããã¾ã§ã§èããããã«è¡æ¹åã«ãã¼ã¿ãå±éãããã¨ãã§ããã°ï¼
ãããã®åãã¼ã¿ã¯SQLã®æ§è³ªä¸ãå¥ã¬ã³ã¼ããã¨ã¿ãªãããã®ã§ï¼ä½¿ãéãåºããã
â»æ³¨ï¼
PostgreSQLã«ã¯ãé
ååãã¨ãããã£ãã¨ãããã¼ã¿åãåå¨ããã
ãªã®ã§ï¼ããã§è©¦ããè¤æ°è¡ã®ã¬ã³ã¼ããï¼çã®æå³ã§ãé
åãã¨å¼ã¶ãã¨ã¯ã§ããªãã
PostgreSQLã§é ååã®ã«ã©ã ã使ã£ã¦ã¿ã
http://d.hatena.ne.jp/pasela/20070301...
â»ã¡ãªã¿ã«ï¼ä¸è¨ã®ãããªéªéã ãã·ã³ãã«ãªæ¹æ³ãããã
SELECT 'a' UNION SELECT 'b' UNION SELECT 'c' ;
å®è¡çµæï¼
a b c
ãããã«è¤æ°è¡ã ãâ¦ã
ããã«ï¼æ±ç¨æ§ã¯å¤±ããããï¼ä»¥ä¸ã®ããã«æååå解ãããã¨ããæ¹æ³ãã
SELECT regexp_split_to_table( 'a,b,c', ',' ) ;
ãããã¯
SELECT regexp_split_to_table( 'abc', '' ) ;
å®è¡çµæï¼
a b c
ã«ã³ãåºåãã®æååãè¤æ°ã®è¡ã«åå²ããæ¹æ³ï¼regexp_split_to_table
http://postgresql.g.hatena.ne.jp/iaki...
ï¼ï¼ï¼generate_seriesã§ã§ããªããã¨
ãSQLã使ã£ã¦ï¼æ¤ç´¢çµæã«é£çªããµããããã¨ãããã¼ãºããããããï¼ããã¯generate_seriesã§ã¯ã§ããªãã
ç¸é¢ãµãã¯ã¨ãªã使ã£ã¦ï¼éãå¦çã¨ãã¦å®è¡ãããã¨ã¯ã§ããï¼
--usersãã¼ãã«ããï¼ååããå±±ç°ãã§ãããããªã¬ã³ã¼ããæ¤ç´¢ãï¼è¡çªå·ä»ãã§è¡¨ç¤ºãã SELECT ( SELECT count(*) FROM users AS u2 WHERE u2.name = 'å±±ç°' AND u2.id <= u1.id ) AS bangou, u1.id, u1.name FROM users AS u1 WHERE u1.name = 'å±±ç°' ORDER BY bangou ;
å®è¡çµæã®ä¾ï¼
bangou id name 1 3 å±±ç° 2 10 å±±ç° 3 11 å±±ç° 4 25 å±±ç°
è¡çªå·ã¨ãã¦bangouã«ã©ã ã表示ããã¦ããã
â»bangouã§ã½ã¼ããããã¨ã«ãã£ã¦ï¼èªåçã«idãè¥ãé ã«ã½ã¼ããããã
ãªããªãï¼bangouã®å¤§å°ã¯ãèªidãããè¥ãidã®ä»¶æ°ãï¼ãidã®è¥ããã§æ±ºã¾ãããã
ãã ãï¼ãã®SQLã¯éãã®ã§å®è¡ãã¦ã¯ãªããªãã
åèï¼
ç´¯ç©ãè¨ç®ããSQLï¼MySQLï¼
http://blog.xole.net/article.php?id=162
æ¤ç´¢çµæã«å¯¾ãã¦ç°¡åã«è¡çªå·ããµããªãçç±ã¯ï¼PostgreSQLã«ãrowidããç¡ãããã ã
ãPostgreSQLã OIDã¨SERIAL
http://www.cisvul.jp/blog/2007/08/ent...
- Oracleã®ROWIDã«ç¸å½ããã®ã¯ï¼postgresã§ã¯OID
- ããã©ã«ãã§ã¯OIDã¯ç¡å¹ã«ãªã£ã¦ããããï¼å©ç¨ã§ããªãã
CTID ã¨ã¯ä½ã§ããï¼
http://www.linux.or.jp/JF/JFdocs/Post...
- OIDã¨å¥ã«ï¼ç¹å®ã®è¡ãèå¥ããããã«ä½¿ãããCTIDã¨ããã«ã©ã ãããã
- ãããï¼å¤§å°æ¯è¼ã¨ãé£çªã¨ãã®ç¨éã§ä½¿ãã«ã©ã ã§ã¯ãªãã
ããã¯ã¢ããªå´ã§å¯¾å¦ãããããªãã ããã
è£è¶³
id:umitanukiæ§ããï¼æ¬ã¨ã³ããªã¼ã«ã¤ãã¦ã®æçãªææãé ããã
SETOFé¢æ°ã®ãããã
http://postgresql.g.hatena.ne.jp/umit...
- generate_seriesãå§ãã¨ããSETOFé¢æ°è¨åãã¼ã¸ã¸ã®ãªã³ã¯
- é ååã«ã¤ãã¦
- 8.4ãã®æ å ±
ãªã©ãªã©