DATETIME
참고: https://zephyrus1111.tistory.com/440
파이썬(python) 날짜 다루기 (feat. datetime)
파이썬(Python)에서는 datetime 모듈을 이용하여 특정 날짜의 연도, 월, 일을 계산한다거나 또는 날짜를 특정 형태로 문자열 변환하는 것이 가능하다. 이번 포스팅에서는 파이썬(Python) 내장 모듈인 da
zephyrus1111.tistory.com
예시) 2016년 a월 b일의 요일을 출력하기
import datetime
def solution(a, b):
return datetime.date(2016,a,b).strftime('%a').upper()
print(solution(5,20))
SQL 연습문제 5
팀 프로젝트 테이블(team_projects)
id | name | start_date | end_date | aws_cost |
1 | 일조 | 2023-01-01 | 2023-01-07 | 30000 |
2 | 꿈꾸는이조 | 2023-03-15 | 2023-03-22 | 50000 |
3 | 보람삼조 | 2023-11-20 | 2023-11-30 | 80000 |
4 | 사조참치 | 2022-07-01 | 2022-07-30 | 75000 |
- team_projects 테이블에서 AWS 예산(aws_cost)이 40000 이상 들어간 프로젝트들의 이름을 선택하는 쿼리를 작성해주세요!
- team_projects 테이블에서 2022년에 시작된 프로젝트를 선택하는 쿼리를 작성해주세요! 단, start_date < ‘2023-01-01’ 조건을 사용하지 말고 쿼리를 작성해주세요!
- team_projects 테이블에서 현재 진행중인 프로젝트를 선택하는 쿼리를 작성해주세요. 단, 지금 시점의 날짜를 하드코딩해서 쿼리하지 말아주세요!
- team_projects 테이블에서 각 프로젝트의 지속 기간을 일 수로 계산하는 쿼리를 작성해주세요!
1.
SELECT name
from team_projects
where aws_cost >= 40000
2.
SELECT *
FROM team_projects
where end_date LIKE '2022%'
3.
SELECT *
FROM team_projects
where DATE_FORMAT(now(), '%Y-%m-%d') between start_date and end_date
4.
SELECT name, dateDIFF(end_date, start_date) '지속 기간'
from team_projects
----------------------------------------------------------
나만의 필수암기노트
pivot만들때 집계함수를 쓰는 이유:
select cuisine_type,
if(age=10, order_count, 0) "10대",
if(age=20, order_count, 0) "20대",
if(age=30, order_count, 0) "30대",
if(age=40, order_count, 0) "40대",
if(age=50, order_count, 0) "50대"
from
(
select a.cuisine_type,
case when age between 10 and 19 then 10
when age between 20 and 29 then 20
when age between 30 and 39 then 30
when age between 40 and 49 then 40
when age between 50 and 59 then 50 end age,
count(1) order_count
from food_orders a inner join customers b on a.customer_id=b.customer_id
where age between 10 and 59
group by 1, 2
) t
group by 1
위의 조건문중 첫번째만 해석하면 10대가 들어오면 order_count값을 넣고 나머지 나이대는 다 0을 넣는다.
select cuisine_type,
max(if(age=10, order_count, 0)) "10대",
max(if(age=20, order_count, 0)) "20대",
max(if(age=30, order_count, 0)) "30대",
max(if(age=40, order_count, 0)) "40대",
max(if(age=50, order_count, 0)) "50대"
from
(
select a.cuisine_type,
case when age between 10 and 19 then 10
when age between 20 and 29 then 20
when age between 30 and 39 then 30
when age between 40 and 49 then 40
when age between 50 and 59 then 50 end age,
count(1) order_count
from food_orders a inner join customers b on a.customer_id=b.customer_id
where age between 10 and 59
group by 1, 2
) t
group by 1
이때 집계함수 max를 이용하면 제일 큰값만 집어 넣기 때문에 NULL 값이 걸러진다.(음수 사용시 유의)
https://data-kindergarten.tistory.com/46
SQL 고급 : PIVOT TABLE
아래의 왼쪽 그림과 같이 어느 백화점의 지역별 점포 리스트 현황 데이터가 있다고 가정해보겠습니다. 지역별로 점포 현황을 깔끔하게 정리해보려고 하는데요. 아래 오른쪽 그림과 같이 나타내
data-kindergarten.tistory.com
'SQL' 카테고리의 다른 글
AI 웹 개발 공부 9일 차 (사전 캠프) (0) | 2024.06.11 |
---|---|
AI 웹 개발 공부 8일 차 (사전 캠프) (0) | 2024.06.10 |
AI 웹 개발 공부 6일 차 (사전 캠프) (0) | 2024.06.05 |
AI 웹 개발 공부 5일 차 (사전 캠프) (0) | 2024.06.04 |
AI 웹 개발 공부 4일 차 (사전 캠프) (0) | 2024.06.03 |