.
.
.
.
.
.
having
은 group by
필터링이라 같이 써야한다. select board_id, max(views)
from used_good_board
.
.
다른 풀이 보면서 알게 된 점.
order by 에도 (컬럼+컬럼) 연산 가능
select a.author_id, a.author_name, s.category, s.total_sales
from author a
join
(SELECT b.book_id, sum(sales*price) as total_sales, b.category, b.author_id
from book_sales bs
join book b
on bs.book_id=b.book_id
where date_format(sales_date,'%Y-%m')='2022-01'
group by 1) s
on a.author_id=s.author_id
group by 1,3
order by 1, 3 desc
select a.author_id, a.author_name, s.category, s.total_sales
from author a
join
(SELECT sum(sales*price) as total_sales, b.category, b.author_id
from book_sales bs
join book b
on bs.book_id=b.book_id
where date_format(sales_date,'%Y-%m')='2022-01'
group by 2,3 ) s
on a.author_id=s.author_id
group by 1,3
order by 1, 3 desc
근데 이렇게 푸는 것보다.,
join 2번해서 풀자..
select Month(start_date) as month, car_id, count(car_id) as records
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where Month(start_date) between '08' and '10'
and car_id in (select car_id
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where Month(start_date) between '08' and '10'
group by 1
having count(car_id)>=5
)
group by 1,2
having count(car_id)>=1 ##특정 월의 총 대여 횟수가 0인 경우에는 결과에서 제외
order by 1, 2 desc
특정 월의 총 대여 횟수가 0인 경우에는 결과에서 제외해주세요.-> 여기에 맞는 조건을 작성하지 않아서, 계속 틀리고있었다. 왜 안적었었냐면,,, 저게 없어도 records가 0인 기록이 없었기 때문에.. 코드실행결과만 보고 괜찮겠거니 했었다..
횟수가 0인 경우에는 결과에서 제외?
횟수 >= 1
조건으로 작성해주자
인라인뷰에 having절 서브쿼리, max인라인뷰 쿼리..서브쿼리를 몇개를 쓴건지,.. 두번째 쿼리에서 max(cnt)랑 member_id 연결이 어려울 것같아.. 갈아엎고 다른 방식으로 써보기로...
🔻정답쿼리
SELECT member_name, review_text, date_format(review_date,'%Y-%m-%d') as review_date
from member_profile m
join rest_review r
on r.member_id=m.member_id
where r.member_id=(select member_id
from rest_review
group by 1
order by count(member_id) desc
limit 1)
order by 3,2
where문에서 in으로 서브쿼리를 열지 않고 바로 member_id= 으로 연결했다.
❌겪은 오류
문법상의 오류는 아니지만.where r.member_id=(select r.member_id
이렇게 작성했어서 멤버아이디가 엄청 여러개가 나왔었다. 서브쿼리 안에서 from 절에 별칭을 주지 않았기 때문에,select member_id ~~~
로 수정했어야했다.
2주차 quiz
📌리스트
📌튜플
📌딕셔너리
#리스트
my_list = [10, 20, 30, 40, 50]
print(my_list[2])
my_list.append(60)
print(len(my_list))
del my_list[-1]
print(my_list[::-1])
#튜플
my_tuple = (10, 20, 30, 40, 50)
print(my_tuple[2])
print(len(my_tuple))
print(my_tuple[::-1])
list(my_tuple)
combined_tuple = my_tuple1 + my_tuple2
#딕셔너리
print(my_dict['name'])
' ' 작은따옴표 써주기 !!
my_dict['gender'] = 'female'
print(len(my_dict))
del my_dict['age']
3주차 quiz
📌조건문
📌반복문
📌while문
#조건문
1-1. (d)
1-2.
num=34
if num>=0:
print("양수")
else:
print("음수")
.
#반복문
2-1.
b,d
2-2.
odds=[]
for i in range(1,11):
if i % 2 != 0:
odds.append(i)
.
.
#while문
3-3.
(d)
.
#조건문+반복문
4-1.
for i in range(1,101):
if i % 3 == 0:
print("Fizz")
elif i % 5==0:
print("Buzz")
else:
print(i)
파이썬과 sql을 적절히 분배하려고 노력하면서 , 병행중이다..! (sql을 하다보면 python을 하고 싶고 python을 하면 sql이 하고싶다 .. 🤣)
점점 sql 코드카타 답이 복잡하고 길어지는데..!!
나도 쓰면서 이해하기 쉽도록 가독성이 좋게+이해하기 좋게 직관적인 쿼리로 작성할 수 있도록 습관을 잘 잡아야할 것 같다.
금요일에 Qcc가 좀 긴장되기도 한다. 일단 내일 마지막 sql라이브 세션을 잘 듣고 복습하도록 하자 ~!! 수요일도 마무리 ~ :)👏