쿼리문에서 결과의 합 구하기.

hurryon의 이미지

특정 필드에서 큰 값 2개 혹은 3개의 합을 구하려고 합니다. 일단 검색은 아래와 같이 검색은 하겠는데 쿼리 한방으로 합을 구할수 없을까요?

select aCPU from resource order by aCPU DESC limit 2 ;
조성현의 이미지

select sum(aCPU) from resource order by aCPU DESC limit 2 ;

하면 안됩니까? mysql에선 잘 되는데요;;

hurryon의 이미지

성현* wrote:
select sum(aCPU) from resource order by aCPU DESC limit 2 ;

하면 안됩니까? mysql에선 잘 되는데요;;

위와 같은 쿼리를 이용하면 해당하는 필드의 전체 레코드 합이 나옵니다. ㅡㅡ; 또한 지금 가지고 있는 디비 서버가 mysql 4.0 이기 때문에 서브 쿼리가 지원되지 않습니다. 서브 쿼리가 지원된다면 간단히 처리 할텐데 말이죠. 아래와 같이...

select sum(cpu) from (select cpu from resource order by cpu desc limit 2) as foo;
조성현의 이미지

@변수를 사용하면 조금 될 듯 하네요.

select @no2 := 0 as no2;
select no,@no2 := (ifnull(@no2,0) + no) as no1 from user order by no desc limit 2;

2개의 row중 row,col = 2,2 부분이 합으로 나오는 군요;;

select @cpu2 := 0 as cpu2;
select cpu, @cpu2 := (ifnull(@cpu2,0) + cpu) as cpu2 from resource order by cpu desc limit 2;

가 될려나? 도움되셨길.

참고 사이트 : http://dev.mysql.com/doc/mysql/en/Arithmetic_functions.html

hurryon의 이미지

음. 이런 방법이 있었군요. 참고하겠습니다. 혹시 쿼리 한방에 깔끔하게 하는 방법은 없을지...좀 찾아 봐야 겠군요.

어쨌든 감사합니다!

sh.의 이미지

제일 큰 값 2~3개가 아니라 특정 범위라면

SELECT CASE WHEN aCPU BETWEEN 0 AND 0.5 THEN 'A'
                      WHEN aCPU BETWEEN 0.51 AND 1 THEN 'B'
              END AS level,
              SUM(aCPU) 
    FROM resource 
GROUP BY 1

대략 이런 방법도 가능할텐데요.. 큰것 순서대로 구해야되기때문에 안되겠네요

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.