SAK }‘ŠÙ

SAK Streets ƒƒCƒ“ƒXƒgƒŠ[ƒg }‘ŠÙEƒJƒtƒF ˆê—— SAK Streets ŒŸõ
| VB | SQL | Perl | PHP | ASP | JSP | C | PC | HP | FAQ | ƒtƒŠ[ƒ\ƒtƒg |

SQL –â‚¢‡‚킹 - •ªÍƒtƒ@ƒ“ƒNƒVƒ‡ƒ“AoverApartition byArangeAdense_rank

•ªÍƒtƒ@ƒ“ƒNƒVƒ‡ƒ“‚ÌŽg‚¢•û‚ª—Ç‚­•ª‚©‚ç‚È‚¢‚Ì‚ÅŽŽ‚µ‚Ä‚Ý‚½B
‚Ù‚Æ‚ñ‚Ç‚ÌWŒvŠÖ”‚ª•ªÍŠÖ”‚Æ‚µ‚ÄŽg‚¦‚é‚Ý‚½‚¢‚Å‚ ‚éB
‚±‚ê‚Í Oracle8i ‚©‚ç‚ ‚Á‚½‹@”\‚È‚ñ‚¾‚낤‚©B

¡PARTITION BY

  EƒOƒ‹[ƒvWŒvŠî–{Œ^B

select •i”Ô, count(*), sum(Žó’”), avg(Žó’”) from Žó’m group by •i”Ô;

•i”Ô                 COUNT(*) SUM(Žó’”) AVG(Žó’”)
------------------ ---------- ----------- -----------
a001                       25       10562      422.48
b001                        7       16797  2399.57143
c001                        3        6010  2003.33333

  E‘WŒvŠî–{Œ^B

select count(*), sum(Žó’”), avg(Žó’”) from Žó’m;

  COUNT(*) SUM(Žó’”) AVG(Žó’”)
---------- ----------- -----------
        35       33369       953.4

  E•i”Ô’PˆÊ‚Ì•½‹ÏŽó’”‚ð–¾×‚ɒljÁB
    partition by ‚ÍAƒOƒ‹[ƒv‰»•ªŠ„‚·‚éB

select Žó’”Ô†, •i”Ô, Žó’”,
  avg(Žó’”) over (partition by •i”Ô order by •i”Ô) aŽó’”
  from Žó’m order by •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”    AŽó’”
---------- ------------------ ---------- ----------
000001-001 a001                       70     422.48
000001-002 a001                      500     422.48
000001-056 a001                      500     422.48
...

  Epartition by ‚ª‚È‚¢‚Æ‘WŒv‚É‚È‚éB

select Žó’”Ô†, •i”Ô, Žó’”,
  avg(Žó’”) over () aŽó’”
  from Žó’m order by •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”    AŽó’”
---------- ------------------ ---------- ----------
000001-001 a001                       70      953.4
000001-002 a001                      500      953.4
000001-056 a001                      500      953.4
...

¡RANGEABETWEEN

  E•i”Ô’PˆÊ‚ÌŒ”‚ð–¾×‚ɒljÁB
    partition by ‚ÍAƒOƒ‹[ƒv‰»•ªŠ„‚·‚éB

select Žó’”Ô†, •i”Ô, Žó’”,
  count(*) over (partition by •i”Ô order by •i”Ô) ct
  from Žó’m order by •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”         CT
---------- ------------------ ---------- ----------
000001-001 a001                       70         25
000001-002 a001                      500         25
000001-056 a001                      500         25
...

  EŽó’”‚æ‚è -1000 ` +4500 ‚Ì•i”Ô–ˆ‚ÌŒ”‚ð–¾×‚ɒljÁB

select Žó’”Ô†, •i”Ô, Žó’”,
  count(*) over (partition by •i”Ô order by Žó’”
    range between 1000 preceding and 4500 following) ct
  from Žó’m order by •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”         CT
---------- ------------------ ---------- ----------
000001-061 c001                     1500          2
000921-001 c001                       10          3
100011-002 c001                     4500          1
...

¡DENSE_RANKARANK

  EŽó’”‚Ì‘½‚¢‡‚Ƀ‰ƒ“ƒN‚ð•t‚¯‚éB

select Žó’”Ô†, •i”Ô, Žó’”,
  dense_rank() over (order by Žó’” desc) rank
  from Žó’m order by rank, •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”       RANK
---------- ------------------ ---------- ----------
000011-001 b001                     6000          1
000019-001 b001                     5555          2
100011-002 c001                     4500          3
...

  Erank ‚Í“¯‚¶ƒ‰ƒ“ƒN‚ª‚ ‚é‚ƃ‰ƒ“ƒN’l‚ª”ò‚ÔB

select Žó’”Ô†, •i”Ô, Žó’”,
  rank() over (order by Žó’” desc) rank
  from Žó’m order by rank, •i”Ô, Žó’”Ô†
;

Žó’”Ô†   •i”Ô                   Žó’”       RANK
---------- ------------------ ---------- ----------
000011-001 b001                     6000          1
000019-001 b001                     5555          2
100011-002 c001                     4500          3
...

  E—á‚͈«‚¢‚ªAŽd“üæCD ‚Ìʼn‚̃‰ƒ“ƒN‚ɑ΂·‚éŬŽó’”‚Æ
    Žd“üæCD ‚ÌÅŒã‚̃‰ƒ“ƒN‚ɑ΂·‚éÅ‘åŽó’”‚ð‹‚ß‚éB

select •i”Ô,
  min(Žó’”) keep (dense_rank first order by “¾ˆÓæCD) min,
  max(Žó’”) keep (dense_rank last order by “¾ˆÓæCD) max
  from Žó’m group by •i”Ô
;

•i”Ô                      MIN        MAX
------------------ ---------- ----------
a001                       10       4000
b001                     2000       6000
c001                       10       1500
...

  E“¯‚¶‚­–¾×ƒŒƒxƒ‹‚Ås‚¤B

select Žó’”Ô†, •i”Ô,
  min(Žó’”) keep (dense_rank first order by “¾ˆÓæCD)
    over(partition by •i”Ô) min,
  max(Žó’”) keep (dense_rank last order by “¾ˆÓæCD)
    over(partition by •i”Ô) max
  from Žó’m order by •i”Ô
;

Žó’”Ô†   •i”Ô                      MIN        MAX
---------- ------------------ ---------- ----------
000001-002 a001                       10       4000
000001-001 a001                       10       4000
000001-056 a001                       10       4000
...

¡SQL Šî‘bŽÀ’n•ÒŽ‘—¿
¡SQL Šî‘b•ÒŽ‘—¿
¡SQL ƒ`ƒ…[ƒjƒ“ƒO•ÒŽ‘—¿
¡Oracle PL/SQL •ÒŽ‘—¿
¡PostgreSQL •ÒAJAVA ServletAJSP •ÒŽ‘—¿
¡MySQL •ÒŽ‘—¿
| VB | SQL | Perl | PHP | ASP | JSP | C | PC | HP | FAQ | ƒtƒŠ[ƒ\ƒtƒg |

SAK Streets ƒƒCƒ“ƒXƒgƒŠ[ƒg }‘ŠÙEƒJƒtƒF ˆê—— SAK Streets ŒŸõ



Copyright (C) 1999-2024 Y.SAK