mysql query 질문입니다.

mysql query 질문입니다.

QA

mysql query 질문입니다.

본문

테이블

num price id
1 50000 a
2 200000 b
3 250000 c
4 300000 d
5 350000 e
6 380000 f
7 400000 g
8 450000 h
9 480000 i

 

 

결과

price_range cnt cnt_max_chk
0~99999 1 N
100000~199999 0 N
200000~299999 2 N
300000~399999 3 Y
400000~499999 3 Y
500000~599999 0 N

 

금액범위에 따라 인원수와 인원수가 제일 높은영역은 "Y" 그 외 "N"으로 체크할 수 있는 쿼리 부탁드립니다.

 

계속 쿼리를 짜도 잘 안됩니다.

 

고수님들 꼭 부탁드립니다. ㅠㅠ

이 질문에 댓글 쓰기 :

답변 4


select t1.price_range, ifnull(t2.cnt, 0)
  from (select '0~99999' as price_range 
        union all 
           select '100000~199999'
            union all 
           select '200000~299999'
            union all 
           select '300000~399999'
            union all 
           select '400000~499999'
            union all 
           select '500000~599999'           
      ) t1
      left outer join 
      (select price_range, count(*) cnt
         from (select 
                 case when price between 0 and 99999 then '0~99999'
                      when price between 100000 and 199999 then '100000~199999'
                      when price between 200000 and 299999 then '200000~299999'
                      when price between 300000 and 399999 then '300000~399999'
                      when price between 400000 and 499999 then '400000~499999'
                      when price between 500000 and 599999 then '500000~599999' end as price_range        
                 from [테이블]) tmp
        group by price_range
      ) t2 
      on t1.price_range = t2.price_range

 

max 값 체크는 못했습니다.

max 값 구하는 쿼리를 별도로 실행한 후, 출력하는 곳에서 처리해 줄 수는 있을 듯 합니다.

맞아요 max값때문에 저도 임시테이블을 쓸수 밖에 없었어요 ㅋㅋ
query만으로 하는것보다 프로그램단에서 하는게 더 나을거 같고
프로그램단에서 처리할거면 굳이 union까지 써가며 할 필요도 없을거 같아요 ㅎㅎ

왠만하면 프로그램단에서 처리하시죠 ㅡ.ㅡ

억지로 만들긴 했지만... 너무 비효율적인데 ㅋㅋㅋ ㅋㅋ 재대로 만든건지도 모르겠네요

일단 결과는 나왔습니다 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ


CREATE TEMPORARY TABLE tmp_tb1 (
    price_rang VARCHAR(20) NOT NULL
    ,cnt INT(2) NOT NULL DEFAULT 0.00
);
CREATE TEMPORARY TABLE tmp_tb2 (
    mx INT(2) NOT NULL DEFAULT 0.00
);
insert into tmp_tb1(price_rang,cnt)
select price_rang, cnt from 
(
    select "0~99999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 0 and 99999    
    union
    select "100000~199999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 100000 and 199999
    union
    select "200000~299999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 200000 and 299999
    union
    select "300000~399999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 300000 and 399999
    union
    select "400000~499999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 400000 and 499999
    union
    select "500000~599999" as price_rang, count(*) as cnt 
    from test where price BETWEEN 500000 and 599999
) as tb;
insert into tmp_tb2(mx) select max(cnt) as mx from tmp_tb1;
select *, case when cnt = (select * from tmp_tb2) then "Y" else "N" end as cnt_max_chk from tmp_tb1 group by price_rang;
DROP TABLE tmp_tb1;
DROP TABLE tmp_tb2;

이 방식은 한번에 쿼리를 만들어 내기 보다는 해당 데이터 max min 값을 가지고 between 방식으로 데이터 카운팅을 가져와 출력해 주는 방식으로 처리해 주셔야 합니다.

계속 퀴리를 자도 잘 안된다는 .... 그 쿼리를 올려보세요.

그래야 어떤 문제가 있어서 안되는지 공부가 됩니다.

내가 이만큼 해봤고, 그 결과물이 이건데 안됩니다 .... 라고요

답변을 작성하시기 전에 로그인 해주세요.
전체 6
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT