[완료]SQL DB 관련 질문입니다.

hurxx의 이미지

Oracle 9i 입니다.
SQL에서

select
ename,to_char(sal,'999,990') as 연봉,
to_char(hiredate,'yyyy"년"mm"월"dd"일"') as 입사일,
deptno from emp where sal in(
select max(sal) from emp group by deptno)
order by deptno asc;

결과는....

ENAME 연봉 입사일 DEPTNO
KING 5,000 10
Luna 5,000 10
Kim 6,850 10
Tom 5,000 20
Nam 3,500 30

부서별 최고 연봉자를 조회하는 문제 인데요...
20번 부서의 최고연봉자와 같은 연봉을 받는 다른 부서의 연봉자도 같이 조회되 버리네요...

뭐가 문제죠?

siabard의 이미지

저렇게하면 모든 레코드중에서 부서별 최고 sal 값과 동일한 sal값을 가지는 레코드를 다 가지고 옵니다.

그러니 1번 부서에 최고 10,000 이 있고 2번 부서에 5,000 이 있으면 모든 부서에서 sal이 10,000이나 5,000 인 것을 다 가져오게 되는 것입니다.

부서별 최고 연봉자를 구하고자한다면 이런 것은 어떨까요..

select
    ename,to_char(sal,'999,990') as 연봉,
    to_char(hiredate,'yyyy"년"mm"월"dd"일"') as 입사일,
    deptno from emp,
    ( select deptno, max(sal) as sal from emp group by depno) as emp_sub
where emp.deptno = emp_sub.deptno AND emp.sal=emp_sub.sal
order by emp.deptno asc;

오라클 9i의 문법이 맞는지는 모르겠습니다...
--

새로움을 느끼기에 삶은 즐겁다..
모험가 아돌 크리스틴을 꿈꾸며..
Sia..

새로움을 느끼기에 삶은 즐겁다..
모험가 아돌 크리스틴을 꿈꾸며..
Sia..

hurxx의 이미지

5행에 SQL 명령어가 올바르게 종료되지 않았습니다
라고 나오네요 ...

siabard의 이미지

as를 빼보십시오. 오라클은 만져본적이 너무 오래되서 중첩 Select 문을 어떻게 써야할지 모르겠군요.

select
    ename,to_char(sal,'999,990') as 연봉,
    to_char(hiredate,'yyyy"년"mm"월"dd"일"') as 입사일,
    deptno from emp,
    ( select deptno, max(sal) as sal from emp group by depno)  emp_sub
where emp.deptno = emp_sub.deptno AND emp.sal=emp_sub.sal
order by emp.deptno asc;

--
새로움을 느끼기에 삶은 즐겁다..
모험가 아돌 크리스틴을 꿈꾸며..
Sia..

새로움을 느끼기에 삶은 즐겁다..
모험가 아돌 크리스틴을 꿈꾸며..
Sia..

rgbi3307의 이미지

아래와 같이 SQL문을 수정하시면 되겠습니다.

select
ename
, to_char(sal,'999,990') as 최고연봉
, to_char(hiredate,'yyyy"년"mm"월"dd"일"') as 입사일
, deptno
from emp
where (deptno, sal) in (
select deptno, max(sal) from emp group by deptno
)
order by deptno asc;

또한,
emp 테이블의 deptno 컬럼에 인덱스가 생성되어 있다면,
오라클 옵티마이저가 deptno 인덱스 스캔을 하므로 속도도 빨라지고,
order by deptno asc 구문은 생략해도 되겠습니다.

From:
*알지비 (메신저: rgbi3307@nate.com)
*학창시절 마이크로마우스를 만들었고, 10년동안 IT관련 개발자로 일하고 있음.
*틈틈히 커널연구회(http://www.kernel.kr/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

From:
*알지비 (메일: rgbi3307(at)nate.com)
*커널연구회(http://www.kernel.bz/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

hurxx의 이미지

잘되네요.

where (deptno, sal)

요게 잘 이해가 안되느데 무엇을 뜻하는건지 알수 있을까요?

rgbi3307의 이미지


where (deptno, sal) in (
select deptno, max(sal) from emp group by deptno
)

hurxx님이 처음에 where 조건절에 sal 하나만 기술하신게 잘못이었구요.
여기에 컬럼 검색조건 deptno가 하나더 추가된것 뿐입니다.
deptno = deptno and sal = max(sal) 조건이 되는 셈이죠.

From:
*알지비 (메신저: rgbi3307@nate.com)
*학창시절 마이크로마우스를 만들었고, 10년동안 IT관련 개발자로 일하고 있음.
*틈틈히 커널연구회(http://www.kernel.kr/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

From:
*알지비 (메일: rgbi3307(at)nate.com)
*커널연구회(http://www.kernel.bz/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.