ã¯ããã«
ã2009å¹´7æã«æ£å¼ãªãªã¼ã¹ãããPostgreSQL8.4ã§ãåæé¢æ°ï¼windowé¢æ°ï¼ããµãã¼ãããã¾ãããæ¬é£è¼ã§ã¯ãåæé¢æ°ã®è¡æã·ãªã¼ãºããPostgreSQLç¨ã«ã¢ã¬ã³ã¸ããå 容ã¨ãOracleãDB2ã®åæé¢æ°ãPostgreSQL8.4ã§ä»£ç¨ããæ¹æ³ãæ±ãã¾ãã
ãæ¬ç¨¿ã§ã¯ããåæé¢æ°ã®è¡æ4ï¼å®çµç·¨ï¼ããPostgreSQL8.4ç¨ã«ãªãã¥ã¼ã¢ã«ããå 容ãæ±ãã¾ãã
対象èªè
- PostgreSQLã§windowé¢æ°ã使ã£ã¦ã¿ããæ¹
- åæé¢æ°ã®ç解ãæ·±ãããæ¹
ããSQLã§éåæ¼ç®ã㨠ã帰ã£ã¦ããHAVINGå¥ãã«è¨è¼ããã¦ããSQLãwindowé¢æ°ã使ã£ã¦è¨è¿°ãã¦ããã¾ãã®ã§ããSQLã§éåæ¼ç®ãã¨ã帰ã£ã¦ããHAVINGå¥ãããèªã¾ãã¦ããã®æ¹ãç解ããããã¨æãã¾ãã
å¿ è¦ãªç°å¢
ãæ¬ç¨¿ã§æ±ãSQLã¯ãPostgreSQL 8.4beta2ã§åä½ç¢ºèªãã¾ããããã®ä»ã次ã®ç°å¢ã§ãå¿ç¨ãå¯è½ã§ãã
- Oracle
- DB2
- SQL Server
1. 2ã¤ã®ãã¼ãã«ãç¸çãªããçããããããã§ãªããã°ãç°ãªãããè¿ãã¯ã¨ãª
ãã¾ãã¯ã2ã¤ã®ãã¼ãã«ãç¸çãããå¤å®ããçµæãè¿ãã¯ã¨ãªã«ã¤ãã¦èãã¦ã¿ã¾ãããããSQLã§éåæ¼ç®ãã§ã¯ã以ä¸ã®SQLãæ示ããã¦ãã¾ãã
SELECT CASE WHEN COUNT(*) = 0 THEN 'çãã' ELSE 'ç°ãªã' END AS result FROM ((SELECT * FROM tbl_A UNION SELECT * FROM tbl_B) except all (SELECT * FROM tbl_A INTERSECT SELECT * FROM tbl_B)) TMP;
ããããwindow
é¢æ°ã§æ¸ãæãã¦ã¿ã¾ããã¾ãã¯ãæ°å¦ã®è¦ç¹ããéåãçããã¨ããæå³ã«ã¤ãã¦èå¯ãã¦ã¿ã¾ããæ°å¦ã®éåã§ã¯éåã®ç¸çæ§ã調ã¹ãå
¬å¼ã¨ãã¦ã次ã®å¼ãæç«ãããã¨ãç¥ããã¦ãã¾ãã
ï¼A ⊆ B ï¼ ã㤠ï¼A ⊇ Bï¼ ⇔ ï¼A = Bï¼
ä¸æ¹ãä¸è¨ã®å ´åãåæ§ã§ãã
ï¼éåAã¨éåBã®è¦ç´ æ°ãçããï¼ ã㤠ï¼A ⊆ Bï¼ ⇔ ï¼A = Bï¼
ãéåAã¨éåBã両æ¹ã¨ã空éåã®å ´åã«å¼ãæ£ãããã¨ã¯æããã§ããã¾ãã両æ¹ã¨ã空éåã§ãªãå ´åã¯ãA = B
ã®å ´åã®ã¿å·¦è¾ºã®æ¡ä»¶ãæç«ãã¾ãã
ãè¦ç´ æ°ã¯window
é¢æ°ã®count
é¢æ°ã使ãã°æ±ãããã¾ãããå
å«é¢ä¿ã¯å·®éåã空éåã¨ãªããã調ã¹ãã°åããã¾ããwindow
é¢æ°ã§æ¸ãæããSQLã¯ä¸è¨ã¨ãªãã¾ãã
select case when count(*) = 0 then 'çãã' else 'ç°ãªã' end as result from (select count(*) over(),* from tbl_A except all select count(*) over(),* from tbl_B) a;
ããã ããä¸è¨ã®SQLã ã¨ãtbl_Aã空éåï¼ã¬ã³ã¼ãããªãï¼ã®å ´åã¯ãå¿ ãçããã¨å¤å®ããã¦ãã¾ãã¾ãããããèæ ®ããSQLã¯ä¸è¨ã®ããã«ãªãã¾ãã
select case when count(*) = 0 then 'çãã' else 'ç°ãªã' end as result from ((select count(*) over(),* from tbl_A except all select count(*) over(),* from tbl_B) union all (select count(*) over(),* from tbl_B except all select count(*) over(),* from tbl_A)) a;
ãå®éã®æ¥åã«ããã¦ã¯ãã¼ãã«ã®ä¸èº«ãã2ã¤ã®SQLã®åºåçµæãæ¯è¼ããå ´åã空éåãèæ ®ããå¿ è¦ããªããã¨ãã»ã¨ãã©ã§ããã¾ããSQLã®çµæ表示ãéä¸ã§ä¸æ¢ãããã¨ãã§ããã®ã§ãé常ã¯ä¸è¨ã®SQLã§ååã§ãããã
select count(*) over(),* from tbl_A except all select count(*) over(),* from tbl_B;