인프런 커뮤니티 질문&답변

GOLD D님의 프로필 이미지

작성한 질문수

데이터 분석 SQL Fundamentals

다양한 window 절의 이해 - 02

다양한 window절의 이해 -01, 02

24.06.12 07:53 작성

·

103

0

윈도우 절 코드 실습 중입니다.

select product_id, product_name, category_id , unit_price

, sum(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following) as unit_price_sum from products;

위 코드는 제공해주신 base 코드입니다. 해당 unit_price_sum을 소수점 둘째자리 까지만 표시하고자 round 함수를 썼는데,round(sum(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following), 2) as unit_price_sum from products;

 

"SQL Error [42883]: 오류: round(real, integer) 이름의 함수가 없음

Hint: 지정된 이름 및 인자 자료형과 일치하는 함수가 없습니다. 명시적 형변환자를 추가해야 할 수도 있습니다."

이와 같은 에러가 납니다.

그래서 with절로 해당 unit_price_sum을 temp_01이라는 임시 쿼리에 담아서 아래와 같이 했지만 이래도 위 오류와 동일하게 나오면서 되지 않네요

with temp_01 as

(

select avg(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following) as unit_price_avg from products

) select round(temp_01.*, 2) from temp_01;

select product_id, product_name, category_id, unit_price

,avg(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following) as unit_price_avg from products;

 

하는 방법이 있을까요? chat gpt도 서브쿼리로 답을 제공했는데 동일한 문제가 발생했습니다.

답변 2

0

권 철민님의 프로필 이미지
권 철민
지식공유자

2024. 06. 12. 20:05

안녕하십니까,

오, 인프런 AI 인턴이 잘 답변해 주었군요.

PostgreSQL이 묵시적인 형변환에 좀 엄격합니다. 명시적으로 NUMERIC으로 변환하여 적용하시면 될 것 같습니다.

round((sum(unit_price) over (order by unit_price))::numeric, 2)

감사합니다.

0

인프런 AI 인턴님의 프로필 이미지

2024. 06. 12. 07:56

안녕하세요, 인프런 AI 인턴입니다.

GOLD D님의 질문과 유사한 문제를 해결할 수 있는 답변을 찾았습니다. PostgreSQL에서는 round 함수 사용 시 데이터 타입에 주의가 필요합니다. 이 경우, round 함수에 전달되는 값의 데이터 타입이 real인데, round 함수가 기대하는 데이터 타입이 이와 정확히 일치하지 않아 발생하는 문제로 보입니다.

이와 관련하여, Postgres에서 round 함수 사용 시 명시적으로 데이터 타입 변환을 해 주어야 하는 경우가 있습니다. 다음은 PostgreSQL에서 round 함수를 사용하여 실수형 데이터를 반올림하는 방법에 대한 해설입니다:

round 에러 질문

select product_id, product_name, unit_price, round(sum(unit_price) over (order by unit_price)::numeric, 2) as unit_price_sum from products;

위 코드와 같이, sum(unit_price)의 결과에 대한 데이터 타입을 numeric으로 명시적으로 변환한 후 round 함수를 적용하면 문제를 해결할 수 있습니다.

GOLD D님의 경우, round 함수를 적용하기 전에 avg(unit_price) over (... 부분의 결과를 numeric으로 변환하는 것이 필요할 수 있습니다. 아래 코드를 참고하시어 수정을 시도해 보세요.

with temp_01 as
(
    select product_id, product_name, category_id, unit_price, round(avg(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following)::numeric, 2) as unit_price_avg from products
)
select * from temp_01;

혹은 직접 쿼리에 round 함수와 형변환을 적용해 보세요:

select product_id, product_name, category_id, unit_price, round(avg(unit_price) over (partition by category_id order by unit_price rows between 1 preceding and 1 following)::numeric, 2) as unit_price_avg from products;

위 예시와 같이 ::numeric을 통해 형변환을 하고 나서 반올림을 시도하면 문제를 해결할 가능성이 높습니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

GOLD D님의 프로필 이미지

작성한 질문수

질문하기