ã¯ããã«
ãCASE
å¼ã¯ãSQL-92ã§æ¨æºã«åãå
¥ãããã¾ãããæ¯è¼çæ°ããéå
·ã§ãããããã便å©ãªããã«ãã®ç価ããã¾ãç¥ããã¦ããããå©ç¨ããã¦ããªãã£ãããCASE
å¼ã®ç°¡ç¥çã§ããDECODE
ï¼Oracleï¼ãIF
ï¼MySQLï¼ãªã©ã®é¢æ°ã§ä»£ç¨ããã¦ããããã¾ãã
ãããããã¸ã§ã¼ã»ã»ã«ã³ããããã°ã©ãã®ããã®SQL 第2çãã®ä¸ã§ã
SQL-92ã§è¿½å ãããä¸ã§æãæç¨ããããã¾ãã
ãã¨è¨ãããã«ï¼æ³¨1ï¼ãCASE
å¼ãæ´»ç¨ããã¨SQLã§ã§ãããã¨ã®å¹
ããã£ã¨åºãããæ¸ãæ¹ãã¹ãã¼ãã«ãªãã¾ããããããå®è£
éä¾åã®æè¡ãªã®ã§ã³ã¼ãã®æ±ç¨æ§ãé«ã¾ãã¾ããç¹ã«DECODE
é¢æ°ã使ã£ã¦ããOracleã¦ã¼ã¶ã¼ã«ã¯ããã²CASE
å¼ã¸ã®ä¹ãæãããè¦ããã¾ãã
ããã®ããã¹ãã¯ããããªãããã¨ãã£ã±ãã®CASE
å¼ã®ä¾¿å©ãªä½¿ãæ¹ãç´¹ä»ãããã®ã§ãã
ãç§ã¯åæ¸ããã
CASE
å¼ã«éãããSQLã¨ãã¼ã¿ãã¼ã¹ã«ã¤ãã¦å¤ãã®ãã¨ãå¦ã³ã¾ããããã®ããã¹ãã¯è©°ã¾ãã¨ããããã®æ¬ã®ã¸ã®å
¥éãªããã¯è§£èª¬ã¨ãã¦æ¸ããããã®ã§ããæªèªã®æ¹ã«ã¯ãä¸èªããè¦ããã¾ããå¿ è¦ãªç°å¢
ã次ã®ããããã®ãã¼ã¿ãã¼ã¹ï¼MS Accessã¯å¯¾è±¡å¤ï¼ã
- Oracleï¼9i以éï¼
- SQL Server
- DB2
- PostgreSQL
- MySQL
対象èªè
ãå ¥éè ï½ä¸ç´è ã
å°å ¥ï¼CASEå¼ã¨ã¯
ãã¾ããåºæ¬çãªææ³ãã解説ãã¾ããããCASE
å¼ã®æ¸å¼ã«ã¯ãåç´CASEå¼ï¼simple caseï¼ã¨æ¤ç´¢CASEå¼ï¼searched caseï¼ã®2éãããã¾ããããããã次ã®ããã«æ¸ãã¾ãã
--åç´CASEå¼ CASE sex WHEN '1' THEN 'ç·' WHEN '2' THEN '女' ELSE 'ãã®ä»' END --æ¤ç´¢CASEå¼ CASE WHEN sex = '1' THEN 'ç·' WHEN sex = '2' THEN '女' ELSE 'ãã®ä»' END
ããã®2ã¤ã¯ãã©ã¡ããåãåä½ããã¾ãããæ§å¥ï¼sex
ï¼ãåã'1'
ãªããç·ãã¸ã'2'
ãªãã女ãã¸èªã¿æ¿ãã¦ããããã§ããåç´CASE
å¼ã®æ¹ãããã®åã®éãç°¡æ½ã«æ¸ãã¾ãããã§ãããã¨ãéããã¦ãã¾ããåç´CASE
å¼ã§æ¸ããæ¡ä»¶ã¯ãæ¤ç´¢CASE
å¼ã§ãæ¸ããã¨ãã§ããã®ã§ãæ¬ç¨¿ã§ã¯ãæ¤ç´¢CASE
å¼ã®æ¹ãå¤ç¨ãã¾ãã
ãã¾ããCASE
å¼ã®è©ä¾¡ã¯ãçã«ãªãWHEN
å¥ãè¦ã¤ãã£ãæç¹ã§æã¡åããã¦ãæ®ãã®WHEN
å¥ã¯ç¡è¦ãããã®ã§ããã®ãã¨ãæèãã¦ã³ã¼ãã£ã³ã°ããå¿
è¦ãããã¾ããç¡ç¨ã®æ··ä¹±ãé¿ããããã«ã WHEN
å¥ã¯æä»çã«è¨è¿°ããã®ãè¯ãã§ãããã
--ä¾ãã°ããããªãµãã«æ¸ãã¨ãçµæã«ã¯ã2çªããç¾ããªã CASE WHEN col_1 IN ('a', 'b') THEN '1çª' WHEN col_1 IN ('a') THEN '2çª' ELSE 'ãã®ä»' END