
Window Function (RANK, SUM), 날짜 포맷(date, date_format), python 기초
Window Function 의 기본 구조
window_function(argument) over (partition by 그룹 기준 컬럼 order by 정렬 기준)
특정 기준으로 순위를 매기는 함수
select cuisine_type,
restaurant_name,
rank() over (partition by cuisine_type order by order_count desc) rn,
order_count
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a

누적합을 구할때 주로 사용함.
select cuisine_type,
restaurant_name,
cnt_order,
sum(cnt_order) over (partition by cuisine_type) sum_cuisine,
sum(cnt_order) over (partition by cuisine_type order by cnt_order) cum_cuisine
from
(
select cuisine_type,
restaurant_name,
count(1) cnt_order
from food_orders
group by 1, 2
) a
order by cuisine_type , cnt_order

yyyy-mm-dd 형식의 컬럼을 date type 으로 변경
select date(date) date_type,
date
from payments

select date(date) date_type,
date_format(date(date), '%Y') "년",
date_format(date(date), '%m') "월",
date_format(date(date), '%d') "일",
date_format(date(date), '%w') "요일"
from payments

종종 생각 안나는 계산 방법
a//b # 3 (몫) a%b # 1 (나머지) a**b # 49 (거듭제곱)
Bool 자료형
x = True # 참 y = False # 거짓 # # 소문자로 쓰면 자료형으로 인식하지 않고 변수명이라 생각해 에러가 남. z = true # name 'true' is not defined # True = 1 # True/False는 변수명으로 쓸 수 없음!
a = 4 > 2 # True 출력 not a # False 출력 -> NOT 연산자로 참을 거짓으로, 거짓을 참으로 바꿔준다. a and b # False AND 연산자로 모두 참이어야 참을 반환한다. a or b # True OR 연산자로 둘 중 하나만 참이면 참이다.
phone = "02-123-1234"
print(phone.split("-")[0])
결과: 02
퀴즈
smith의 science 점수를 출력people = [ {'name': 'bob', 'age': 20, 'score':{'math':90,'science':70}}, {'name': 'carry', 'age': 38, 'score':{'math':40,'science':72}}, {'name': 'smith', 'age': 28, 'score':{'math':80,'science':90}}, {'name': 'john', 'age': 34, 'score':{'math':75,'science':100}} ]print(people[2]['score']['science'])
for i, fruit in enumerate(fruits):
print(i, fruit)
if i == 4:
break
기본 구조: def로 함수명 선언, return으로 결과 반환
def bus_fee(age):
if age > 65:
return 0
elif age > 20:
return 1200
else:
return 0
money = bus_fee(28)
print(money)