mysql query 질문입니다. 채택완료
천사의고통
7년 전
조회 2,487
테이블
| 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개
채택된 답변
+20 포인트
마르스컴퍼니
Expert
7년 전
</p>
<p>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</p>
<p>
max 값 체크는 못했습니다.
max 값 구하는 쿼리를 별도로 실행한 후, 출력하는 곳에서 처리해 줄 수는 있을 듯 합니다.
로그인 후 평가할 수 있습니다
답변에 대한 댓글 2개
�
휴란드
7년 전
�
천사의고통
7년 전
감사합니다.
max값은 데이타를 출력해서 rsort배열로 정렬하여 처리했습니다.^^
max값은 데이타를 출력해서 rsort배열로 정렬하여 처리했습니다.^^
댓글을 작성하려면 로그인이 필요합니다.
7년 전
왠만하면 프로그램단에서 처리하시죠 ㅡ.ㅡ
억지로 만들긴 했지만... 너무 비효율적인데 ㅋㅋㅋ ㅋㅋ 재대로 만든건지도 모르겠네요
일단 결과는 나왔습니다 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
</p>
<p>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
);</p>
<p>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</p>
<p> select "100000~199999" as price_rang, count(*) as cnt
from test where price BETWEEN 100000 and 199999</p>
<p> union</p>
<p> select "200000~299999" as price_rang, count(*) as cnt
from test where price BETWEEN 200000 and 299999</p>
<p> union</p>
<p> select "300000~399999" as price_rang, count(*) as cnt
from test where price BETWEEN 300000 and 399999</p>
<p> union</p>
<p> select "400000~499999" as price_rang, count(*) as cnt
from test where price BETWEEN 400000 and 499999</p>
<p> union</p>
<p> select "500000~599999" as price_rang, count(*) as cnt
from test where price BETWEEN 500000 and 599999</p>
<p>) as tb;
insert into tmp_tb2(mx) select max(cnt) as mx from tmp_tb1;</p>
<p>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;</p>
<p>
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
7년 전
계속 퀴리를 자도 잘 안된다는 .... 그 쿼리를 올려보세요.
그래야 어떤 문제가 있어서 안되는지 공부가 됩니다.
내가 이만큼 해봤고, 그 결과물이 이건데 안됩니다 .... 라고요
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
query만으로 하는것보다 프로그램단에서 하는게 더 나을거 같고
프로그램단에서 처리할거면 굳이 union까지 써가며 할 필요도 없을거 같아요 ㅎㅎ