æåããããã®å ŽåãOracleã¯éåçãªSQLèšèªãšããŠæ瀺ãããŠããããšãæ確ã«ããããšæããŸããã°ã«ãŒãåãšãã®é©çšæ¹æ³ã¯ãSQLãã¡ããªå šäœïŒããã§ã¯æ§é åã¯ãšãªèšèªãšããŠç解ãããŠããŸãïŒã«é©çšãããåèšèªã®æ§æãä¿®æ£ããããã¹ãŠã®ã¯ãšãªã«é©çšãããŸãã
å¿ èŠãªæ å ±ããã¹ãŠç°¡æœãã€ç°¡åã«2ã€ã®ããŒãã«åããŠèª¬æããŸãããã®æçš¿ã¯ãåå¿è ã®éçºè ã«ãšã£ãŠãããã圹ç«ã€ã§ãããã誰ãæ°ã«ãã-ç«ãžããããã
ããŒã1ïŒãªãã¡ãªã³ã°ã®æ³šæãã°ã«ãŒãåãææ
ããã§ã¯ã䞊ã¹æ¿ã-䞊ã¹æ¿ããã°ã«ãŒãå-ã°ã«ãŒãåããã£ã«ã¿ãªã³ã°-ææãããã³ã¯ãšãªãã©ã³ã«ã€ããŠèª¬æããŸãããããããŸãæåã«ã
泚æãã
Order byæŒç®åã¯ãåºåå€ã䞊ã¹æ¿ããŸããååŸããå€ãç¹å®ã®åã§äžŠã¹æ¿ããŸãã䞊ã¹æ¿ãã¯ãæŒç®åã䜿çšããŠå®çŸ©ãããåãšã€ãªã¢ã¹ã«ãã£ãŠé©çšããããšãã§ããŸãã
Order byã®å©ç¹ã¯ãæ°å€åãšæåååã®äž¡æ¹ã«é©çšã§ããããšã§ããæåååã¯éåžžãã¢ã«ãã¡ãããé ã«ãœãŒããããŸãã
æé ã®äžŠã¹æ¿ããããã©ã«ãã§é©çšãããŸããåãéé ã§äžŠã¹æ¿ããå Žåã¯ãè¿œå ã®DESCæŒç®åã䜿çšããŸãã
æ§æïŒ
SELECT column1ãcolumn2ã âŠïŒååã瀺ããŸãïŒ
FROM table_name
ORDER BY column1ãcolumn2 ⊠ASC | DESC ;
äŸãæããŠãã¹ãŠãèŠãŠã¿ãŸãããã
æåã®è¡šã§ã¯ããã¹ãŠã®ããŒã¿ãååŸããIDåã®æé ã§äžŠã¹æ¿ããŠããŸãã
2çªç®ã§ã¯ããã¹ãŠã®ããŒã¿ãååŸããŸããDESCããŒã¯ãŒãã䜿çšããŠãIDåã§éé ã§äžŠã¹æ¿ããŸãã
3çªç®ã®ããŒãã«ã¯ãããã€ãã®äžŠã¹æ¿ããã£ãŒã«ãã䜿çšããŸãããŸããéšéå¥ã®äžŠã¹æ¿ãã§ããåãéšéã®ãã£ãŒã«ãã§æåã®æŒç®åãçããå Žåã2çªç®ã®ãœãŒãæ¡ä»¶ãé©çšãããŸããç§ãã¡ã®å Žåãããã¯çµŠäžã§ãã
ãšãŠãç°¡åã§ããè€æ°ã®äžŠã¹æ¿ãæ¡ä»¶ãèšå®ã§ãããããåºåãªã¹ããããã€ã³ããªãžã§ã³ãã«äžŠã¹æ¿ããããšãã§ããŸãã
ã°ã«ãŒãå
SQLã§ã¯ãGroup byå¥ã¯ãç¹å®ã®ã°ã«ãŒãã®ããŒã¿ããŒã¹ããååŸãããããŒã¿ãåéããŸããã°ã«ãŒãåã«ããããã¹ãŠã®ããŒã¿ãè«çã»ããã«åå²ããããããçµ±èšèšç®ãåã°ã«ãŒãã§åå¥ã«å®è¡ã§ããŸãã
ãã®æŒç®åã¯ã1ã€ä»¥äžã®åã«ããéžæã®çµæãçµã¿åãããããã«äœ¿çšãããŸããã°ã«ãŒãåããåŸãåã§äœ¿çšãããå€ããšã«1ã€ã®ãšã³ããªã®ã¿ããããŸãã
SQL Group byã¹ããŒãã¡ã³ãã®äœ¿çšã¯ãéçŽé¢æ°ã®äœ¿çšããã³SQLHavingã¹ããŒãã¡ã³ããšå¯æ¥ã«é¢é£ããŠããŸãã SQLã®éçŽé¢æ°ã¯ãäžé£ã®åå€ã«å¯ŸããŠåäžã®å€ãè¿ãé¢æ°ã§ããäŸïŒCOUNTïŒïŒãMINïŒïŒãMAXïŒïŒãAVGïŒïŒãSUMïŒïŒ
æ§æïŒ
SELECT column_nameïŒsïŒ
FROM table_name
WHERE æ¡ä»¶
GROUP BY COLUMN_NAMEïŒSïŒ
BY ORDER COLUMN_NAMEïŒSïŒ;
ã°ã«ãŒãåã¯ãSELECTã¯ãšãªã®æ¡ä»¶ä»ãWHEREå¥ã®åŸã«è¡šç€ºãããŸãããªãã·ã§ã³ã§ãORDERBYã䜿çšããŠåºåå€ã䞊ã¹æ¿ããããšãã§ããŸãã ãããã£ãŠãåã®äŸã®è¡šã«åºã¥ããŠãåéšéã®åŸæ¥å¡ã®æ倧絊äžãèŠã€ããå¿ èŠããããŸããæçµçãªãµã³ãã«ã«ã¯ãéšéã®ååãšæé«çµŠäžãå«ããå¿ èŠããããŸãã 解決ç1ïŒã°ã«ãŒãåã䜿çšããªãïŒïŒ
SELECT DISTINCT
ie.department
ie.slary
FROM itx_employee ie
WHERE ie.salary = (
SELECT
max(ie1.salary)
FROM itx_employee ie1
WHERE ie.department = ie1.department
)
解決ç2ïŒã°ã«ãŒãåã䜿çšïŒïŒ
SELECT
department,
max(salary)
FROM itx_employee
GROUP BY department
æåã®äŸã§ã¯ãã°ã«ãŒãåã䜿çšããã«ãå¯éžæã䜿çšããŠåé¡ã解決ããŸãã1ã€ã®éžæã«2çªç®ãå ¥ããŸãã2çªç®ã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ãã°ã«ãŒãåã䜿çšããŸãã
2çªç®ã®äŸã¯ãæåã®äŸãšåãæ©èœãå®è¡ããŸãããçããŠèªã¿ããããªã£ãŠããŸãã
Group byã®ä»çµã¿ïŒæåã«2ã€ã®éšéãqaã°ã«ãŒããšdevã°ã«ãŒãã«åå²ããŸãããããã圌ã¯ãããã®ããããã®æ倧絊äžãæ¢ããŸãã
æã£ãŠãã
æã€ããšã¯ãã£ã«ã¿ãªã³ã°ããŒã«ã§ããéèšæ©èœãå®è¡ããçµæã瀺ããŸããæã€å¥ã¯ãWHEREã䜿çšã§ããªãSQLã§äœ¿çšãããŸãã
WHEREå¥ã§è¡ããã£ã«ã¿ãªã³ã°ããããã®è¿°èªãå®çŸ©ãããŠããå Žåãã°ã«ãŒãåã®åŸã«ãHavingãã䜿çšããŠãéèšé¢æ°ã®å€ã§ã°ã«ãŒãããã£ã«ã¿ãªã³ã°ããè«çè¿°èªãå®çŸ©ããŸãããã®å¥ã¯ãè¡ã°ã«ãŒãã®éèšé¢æ°ã䜿çšããŠååŸããå€ããã¹ãããããã«å¿ èŠã§ãã
æ§æïŒ
SELECT column_nameïŒsïŒ
FROM table_name
WHERE condition
GROUP BY column_nameïŒsïŒ
HAVING condition
æåã«ãå¹³å絊äžã4000ãè¶ ããéšéã衚瀺ããŸãã次ã«ããã£ã«ã¿ãªã³ã°ã䜿çšããŠæ倧絊äžã衚瀺ããŸãã
解決ç1ïŒGROUP BYããã³HAVINGã䜿çšããªãïŒïŒ
SELECT DISTINCT
ie.department AS "DEPARTMENT",
(
(SELECT
AVG(ie1.salary)
FROM itx_employee ie1
WHERE ie1.department = ie.department)
) AS "AVG SALARY"
FROM itx_employee ie
where (SELECT
AVG(ie1.salary)
FROM itx_employee ie1
WHERE ie1.department = ie.department) > 4000
解決ç2ïŒGROUP BYããã³HAVINGã䜿çšïŒïŒ
SELECT
department,
AVG(salary)
FROM itx_employee
GROUP BY department
HAVING AVG(salary) > 4000
æåã®äŸã§ã¯ã2ã€ã®å¯éžæã䜿çšãã1ã€ã¯æ倧絊äžãæ€çŽ¢ãããã1ã€ã¯å¹³å絊äžããã£ã«ã¿ãªã³ã°ããŸãã2çªç®ã®äŸããã¯ããã«åçŽã§ç°¡æœã«ãªããŸããã
ãã©ã³ã®ãªã¯ãšã¹ã
ãªã¯ãšã¹ãã«æéãããããã¡ã¢ãªãšãã£ã¹ã¯ã®å€§éã®ãªãœãŒã¹ãæ¶è²»ããå ŽåããããããŸããã¯ãšãªãé·ãéå¹ççã«å®è¡ãããŠããçç±ãç解ããããã«ãã¯ãšãªãã©ã³ã確èªã§ããŸãã
ã¯ãšãªãã©ã³ã¯ãã¯ãšãªã®æå³ãããå®è¡ãã©ã³ã§ãã DBMSããããå®è¡ããæ¹æ³ã DBMSã¯ããµãã¯ãšãªå ã§å®è¡ããããã¹ãŠã®æäœãèšè¿°ããŸãããã¹ãŠãåæããåŸãã¯ãšãªã®åŒ±ç¹ãã©ãã«ããããç解ããã¯ãšãªãã©ã³ã䜿çšããŠããããæé©åããããšãã§ããŸãã
Oracleã§SQLã¹ããŒãã¡ã³ããå®è¡ãããšããããããå®è¡èšç»ããååŸãããŸãããã®ã¯ãšãªå®è¡èšç»ã¯ãå®è¡äžã®SQLã¹ããŒãã¡ã³ãã«åŸã£ãŠOracleãããŒã¿ããã§ããããæ¹æ³ã®èª¬æã§ãããã©ã³ã¯ãã¹ãããã®é åºãšãããã®éã®é¢ä¿ãå«ãããªãŒã§ãã
ã¯ãšãªã®æšå®å®è¡èšç»ãååŸã§ããããŒã«ã«ã¯ãToadãSQL NavigatorãPL / SQL Developerãªã©ããããŸãããããã®ããŒã«ã¯ãã¯ãšãªã®ãªãœãŒã¹æ¶è²»ã瀺ãããã€ãã®ææšãæäŸããŸãããã®äž»ãªãã®ã¯ãã³ã¹ã-å®è¡ã³ã¹ããšã«ãŒãã£ããªãã£ïŒãŸãã¯è¡ïŒ-ã«ãŒãã£ããªãã£ïŒãŸãã¯æ°éïŒã§ããè¡ïŒã
ãããã®ã€ã³ãžã±ãŒã¿ãŒã®å€ãé«ãã»ã©ãã¯ãšãªã®å¹çã¯äœäžããŸãã
以äžã«ãã¯ãšãªãã©ã³ã®åæã瀺ããŸããæåã®ãœãªã¥ãŒã·ã§ã³ã¯å¯éžæã䜿çšãã2çªç®ã®ãœãªã¥ãŒã·ã§ã³ã¯ã°ã«ãŒãåã䜿çšããŸããæåã®ãœãªã¥ãŒã·ã§ã³ã¯22è¡ãåŠçãã2çªç®ã®ãœãªã¥ãŒã·ã§ã³ã¯15è¡ãåŠçããããšã«æ³šæããŠãã ããã
ã¯ãšãªãã©ã³åæïŒ
2ã€ã®å¯éžæã䜿çšããå¥ã®ã¯ãšãªãã©ã³åæïŒ
ãã®äŸã¯ãSQLããŒã«ã®éå¹ççãªäœ¿çšã®å€åœ¢ãšããŠæ瀺ãããŠãããããã¯ãšãªã§äœ¿çšããããšã¯ãå§ãããŸããã
äžèšã®ãã¹ãŠã®æ©èœã«ãããã¯ãšãªãäœæããéã®äœæ¥ã楜ã«ãªããã³ãŒãã®å質ãšèªã¿ããããåäžããŸãã
ããŒã2ïŒãŠã£ã³ããŠæ©èœ
ãŠã£ã³ããŠé¢æ°ã¯ãMicrosoft SQL Server 2005ã«ãŸã§ããã®ãŒããŸãããããã¯ãSelectå¥å ã®ç¹å®ã®ç¯å²ã®è¡ã«å¯ŸããŠèšç®ãå®è¡ããŸããèŠããã«ãããŠã£ã³ããŠãã¯ãèšç®ãè¡ãããäžé£ã®è¡ã§ãã ããŠã£ã³ããŠãã䜿çšãããšãããŒã¿ãåæžããŠããé©åã«åŠçã§ããŸãããã®æ©èœã䜿çšãããšãããŒã¿ã»ããå šäœããŠã£ã³ããŠã«åå²ã§ããŸãã
ãŠã£ã³ããŠåŠçã«ã¯å€§ããªå©ç¹ããããŸããèšç®çšã®ããŒã¿ã»ãããäœæããå¿ èŠã¯ãããŸãããããã«ãããã»ããã®ãã¹ãŠã®è¡ãäžæã®IDã§ä¿åã§ããŸãããŠã£ã³ããŠé¢æ°ã®çµæã¯ããã1ã€ã®ãã£ãŒã«ãã®çµæã®éžæã«è¿œå ãããŸãã
æ§æïŒ
SELECT column_nameïŒsïŒ
éèšé¢æ°ïŒèšç®ãããåïŒ
OVERïŒ[ PARTITION BYåããã°ã«ãŒããž]
FROM table_name
[ ORDERBYåãããœãŒã]
[ã°ã«ãŒãå ã®è¡ãå¶éããããã®ROWSãŸãã¯RANGEåŒ]ïŒ
OVER PARTITION BYã¯ããŠã£ã³ããŠãµã€ãºãèšå®ããããã®ããããã£ã§ããããã§ã¯ãè¿œå æ å ±ãæå®ãããããµãŒãã¹ã³ãã³ããæå®ãããã§ããŸããããšãã°ãè¡çªå·ãè¿œå ã§ããŸãããŠã£ã³ããŠé¢æ°ã®æ§æã¯ãåã®éžæã«ãŽã£ããåããŸãã
äŸãæããŠãã¹ãŠãèŠãŠã¿ãŸããããå¥ã®éšéãããŒãã«ã«è¿œå ãããããŒãã«ã«15è¡ãè¿œå ãããŸãããç§ãã¡ã¯ãåŸæ¥å¡ããã®çµŠäžãããã³çµç¹ã®æ倧絊äžã®åŒãåºããè©Šã¿ãŸãã
æåã®ãã£ãŒã«ãã§ã¯ååãåãã2çªç®ã®ãã£ãŒã«ãã§ã¯çµŠäžãåããŸãã次ã«ãïŒïŒäžã§ãŠã£ã³ããŠé¢æ°ã䜿çšããŸã..ã ããŠã£ã³ããŠãã®ãµã€ãºã瀺ãããŠããªããããçµç¹å šäœã§æ倧ã®çµŠäžãååŸããããã«äœ¿çšããŸãã空ã®æ¬åŒ§ä»ãã®ãªãŒããŒïŒïŒã¯ãéžæå šäœã«é©çšãããŸãããããã£ãŠãã©ãã§ãæ倧絊äžã¯10,000ã§ãããŠã£ã³ããŠé¢æ°ã®ã¢ã¯ã·ã§ã³ã®çµæãåè¡ã«è¿œå ãããŸãã
ã¯ãšãªã®4è¡ç®ãããŠã£ã³ããŠé¢æ°ã®èšåãåé€ãããšãmaxïŒsalaryïŒã®ã¿ãæ®ãããªã¯ãšã¹ãã¯æ©èœããŸãããæ倧絊äžã¯åçŽã«èšç®ã§ããŸããã§ãããããŒã¿ã¯1è¡ãã€åŠçããããããmaxïŒsalaryïŒãåŒã³åºãæç¹ã§ã¯ãçŸåšã®è¡ã«ã¯1ã€ã®çªå·ãããããŸãããçŸåšã®åŸæ¥å¡ãããã§ããŠã£ã³ããŠæ©èœã®å©ç¹ã確èªã§ããŸããé話æã«ã¯ããŠã£ã³ããŠå šäœãšå©çšå¯èœãªãã¹ãŠã®ããŒã¿ã§æ©èœããŸãã
åéšéã®æ倧絊äžã衚瀺ããå¿ èŠãããå¥ã®äŸãèŠãŠã¿ãŸããã
ãå®éãããŠã£ã³ããŠãã®ãã¬ãŒã ãèšå®ããŠãéšéã«åå²ããŠããŸããã©ã³ãã³ã°ã®äŸãšããŠéšéã䜿çšããŸããdevãqaãsalesã®3ã€ã®éšéããããŸãã
ããŠã£ã³ããŠãã¯ãåéšéã®æ倧絊äžãæ€çŽ¢ããŸããéžæã®çµæãæåã«devã次ã«qaã次ã«salesã®æ倧絊äžãèŠã€ãã£ãããšãããããŸããåè¿°ã®ããã«ããŠã£ã³ããŠé¢æ°ã®çµæã¯ãåè¡ã®ãã§ããçµæã«æžã蟌ãŸããŸãã
åã®äŸã§ã¯ãoverã®åŸã®æ¬åŒ§ã¯æå®ãããŠããŸãããããã§ã¯ããŠã£ã³ããŠã®ãµã€ãºãèšå®ã§ããPARTITIONBYã䜿çšããŸãããããã§ã¯ãããã€ãã®è¿œå æ å ±ãæå®ããããè¡çªå·ãªã©ã®ãµãŒãã¹ã³ãã³ããéä¿¡ãããã§ããŸãã
çµè«
SQLã¯ãäžèŠããã»ã©åçŽã§ã¯ãããŸãããäžèšã®ãã¹ãŠããŠã£ã³ããŠæ©èœã®åºæ¬æ©èœã§ãã圌ãã®å©ããåããŠãããªãã¯ç§ãã¡ã®èŠæ±ããåçŽåãããããšãã§ããŸãããã ãããããã«ã¯ããã«å€ãã®å¯èœæ§ãé ãããŠããŸããã¯ãšãªã«æ©èœãè¿œå ããããã«çµã¿åãããããšãã§ãããŠãŒãã£ãªãã£æŒç®åïŒROWSãRANGEãªã©ïŒããããŸãã
ãã®æçš¿ããã®ãããã¯ã«é¢å¿ã®ãããã¹ãŠã®äººã«åœ¹ç«ã€ããšãé¡ã£ãŠããŸãã