728x90

https://school.programmers.co.kr/learn/courses/30/lessons/131530

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

풀이)

SELECT (price div 10000)*10000 as price_group, count(*) as products
from product
group by price_group
order by price_group;

group by에 대해 배울 수 있는 문제였다.

항상 전체를 어떻게 다뤄야하나... 코딩만 하다보니 for문 돌려야하나... 이러는 것 같다

 

연습해야지 어쩌겠어!!

 

set @a = 0;

select (@a := @a + 10000) as PRICE_GROUP,
        (select count(*)
            from PRODUCT
            where @a <= price and price < @a+10000) as PRODUCTS 
    from PRODUCT
    where @a < price  
    order by PRICE_GROUP;

그리고 다른 분의 코드를 보게 되었다!!

이렇게도 쓸 수 있구나.. 신기하다....

 

 

SELECT TRUNCATE(price,-4) AS price_group, COUNT(product_id) AS products 
FROM product 
GROUP BY price_group 
ORDER BY price_group;

그리고 내림 함수인 truncate에 대해서도 배울 수 있었다.

 

참고할 블로그!

https://devjhs.tistory.com/87

 

[mysql] ROUND(),TRUNCATE() - 반올림과 버림

ROUND() , TRUNCATE() - 역할ROUND(숫자,반올림할 자릿수) - 숫자를 반올림할 자릿수 +1 자릿수에서 반올림 TRUNCATE(숫자,버릴 자릿수) - 숫자를 버릴 자릿수 아래로 버림 ※ 반드시 버릴 자릿수를 명시해

devjhs.tistory.com

 

728x90

+ Recent posts