문제점
- 복잡한 sql을 한번에 짜려고 함
- 정확한 sql 개념이 없어서 고쳐도 뭐가 나아지는지 모름
개선 방법
- 구하려는 값을 여러 단계로 나눔
- review와 product 를 join
- 카테고리 별로 group by 해서
- products에 count한 결과를 join
- 이 전체를 count한 결과에 따라 정렬
해결 과정
예시 쿼리
SELECT OrderDetailID, ProductName, CategoryID
FROM OrderDetails
INNER JOIN Products ON OrderDetails.ProductID == Products.ProductID
를 보고 내 테이블로 변경해서
SELECT r.*
FROM Review as r
INNER JOIN Product as p
ON r.product_code = p.product_code
1단계를 실행해봤는데 , ORA-00933: SQL command not properly ended 라는 오류가 발생했다.
별칭을 제거하고 끝에 ;를 붙이니 실행 성공.
왜 별칭을 붙이면 안될까? - 스프링에서는 별칭쓰면 안되나보다.. 학원 동기 친구가 말해줌..여기 블로그에도
https://alisyabob.tistory.com/91
다음 2단계인 카테고리 별로 group by 하는 쿼리
예시는
SELECT OrderDetailID, ProductName, CategoryID
FROM OrderDetails
INNER JOIN Products ON OrderDetails.ProductID == Products.ProductID
나는
SELECT COUNT(Category_Code) as OrderCount, Product_Name, Category_code
FROM review
INNER JOIN Product ON review.Product_code = Product.Product_code
GROUP BY Product.Category_code
이렇게 하니까 column ambiguously defined 오류 발생..보니까 예시는 테이블안에 OrderDetailId가 주문번호를 의미하던데 내 review테이블에는 리뷰번호가 없다. 그래서 review 테이블 삭제하고 review_count컬럼 하나 추가해서 다시 테이블을 생성했다.
천천히 보니까 예시테이블에는
다시 쪼개서
SELECT COUNT(review_count) as reviewcount, Product_Name, Category_code
FROM review
이것만 실행했더니 이번엔 not a single-group group function 오류 발생 ㅎ
그룹 함수를 썼는데 group by를 안써서, 그리고
테이블의 모든 칼럼을 안써서 그렇단다.
그래서 그럼 아까 쿼리에 inner join 문장을 빼고
SELECT COUNT(review_count) as reviewcount, Product_Name, Category_code
FROM review
--INNER JOIN Product ON review.Product_code = Product.Product_code
GROUP BY Product.Category_code;
이렇게 실행했더니
"PRODUCT"."CATEGORY_CODE": invalid identifier 오류 발생
프로덕트 테이블에 카테고리 코드 컬럼이 분명 있는데?
from review에서만 해서 그런가?
group by review.Category_code;로 실행하니
그건 group by expression이 아니란다..
그럼 다시 inner join 문장을 포함하고 돌려서
SELECT COUNT(review_count) as reviewcount, Product_Name, Category_code
FROM review
INNER JOIN Product ON review.Product_code = Product.Product_code
GROUP BY product.Category_code;
얘는 ORA-00918: column ambiguously defined
컬럼명이 중복되었을 경우에 생기는 에러라고 한다.
review.product_code와 product.product_code가 중복되는 것 같아 product.product_code as pps로 변경
SELECT cnt(review_count) as reviewcount, Product_Name, Category_code
FROM review
INNER JOIN Product ON review.Product_code = Product.Product_code as ppc
GROUP BY product.Category_code;
하지만 ORA-00933: SQL command not properly ended 또 오류 발생. 얘는 별칭을 줘서 날 수 있다는 오류인데.. 어디는 별칭을 주라고 하고 어디는 주지말라고 하고..
SELECT count(r.review_count) as reviewcount, r.Product_Name, r.Category_code
FROM review r
INNER JOIN Product p
ON r.product_code = p.product_code
GROUP BY p.Category_code;
별칭썼더니 이번엔 not a GROUP BY expression 오류, group by 에 select에 썼던 컬럼을 모두 쓰라는 글을 보고
https://itprogramming119.tistory.com/entry/Oracle-Error-ORA-00979-not-a-GROUP-BY-expression
SELECT count(r.review_count) as reviewcount, r.Product_Name, r.Category_code
FROM review r
INNER JOIN Product p
ON r.product_code = p.product_code
GROUP BY p.Category_code, count, r.Product_Name, r.Category_code ;
해봤지만 count를 못찾는다.
예시 쿼리에는 모든 컬럼을 쓰지 않아도 되어서 저 위의 글이 정확한지 모르겠음
SELECT count(r.review_count) reviewcount, r.Product_Name, r.Category_code
FROM review r
INNER JOIN Product p
ON r.product_code = p.product_code
GROUP BY p.Category_code, reviewcount ,r.Product_Name, r.Category_code;
테이블에 별칭 다시 붙여서 해봤지만 이제는 계속 count를 인식을 못하고 "REVIEWCOUNT": invalid identifier라는 오류가 뜬다..
쿼리 완성하기..2단계부터 계속 할 것