WITH book_author AS (
SELECT
book_id
, category
, b.author_id
, price
, author_name
FROM
book b
JOIN author a
USING(author_id)
), sales_b_a AS (
SELECT
*
, sales * price AS total_sales
FROM
book_sales s
JOIN book_author ba
USING(book_id)
WHERE
DATE(sales_date)
BETWEEN '2022-01-01' AND '2022-01-31'
)
SELECT
author_id
, author_name
, category
, SUM(total_sales) AS sales
FROM
sales_b_a
GROUP BY
author_id
, author_name
, category
ORDER BY
author_id
, category DESC
;
SELECT A.AUTHOR_ID, A.AUTHOR_NAME, B.CATEGORY, SUM(SALES*PRICE) AS TOTAL_SALES
FROM BOOK_SALES S
JOIN BOOK B
ON S.BOOK_ID = B.BOOK_ID
JOIN AUTHOR A
ON B.AUTHOR_ID = A.AUTHOR_ID
WHERE YEAR(SALES_DATE) = '2022' AND MONTH(SALES_DATE) = '1'
GROUP BY B.AUTHOR_ID, B.CATEGORY
ORDER BY A.AUTHOR_ID ASC, B.CATEGORY DESC
→ JOIN 두 번 & 2022년 1월 조건 처리 방식
→ 여기서 WHERE절 조건을 WHERE SALES_DATE LIKE '2022-01%'
나 WHERE DATE_FORMAT(SALES_DATE, '%Y-%m') IN ('2022-01')
로 해도 됨
def solution(n):
answer = 0
root = n**(1/2)
if int(root) == root:
answer = (root+1)**2
else:
answer = -1
return answer
def solution(n):
sqrt = n ** (1/2)
if sqrt % 1 == 0:
return (sqrt + 1) ** 2
return -1
→ 정수 확인을 % 1 == 0
으로 처리
def solution(n):
return n == int(n**.5)**2 and int(n**.5+1)**2 or -1
→ return에 and와 or을 쓸 수 있구나..
→ 삼항연산자라고 함
int more = (a > b) ? a : b
def solution(n):
from math import sqrt
return -1 if sqrt(n) % 1 else (sqrt(n)+1)**2
→ math에서 sqrt import해서 간단하게 풀기
def solution(n):
sqrt = pow(n, 0.5)
return pow(sqrt + 1, 2) if sqrt == int(sqrt) else -1
→ 내장함수 pow() 활용
pow(base, exp [, mod] )
자연 조인(NATURAL JOIN)
list, array, numpy array