Programmers/SQL(34)
-
[oracle/mysql]입양 시각 구하기(2)
쉬울 줄 알았으나 굉장히 오랜 시간을 들인 문제 특히 mysql 코드를 공들여 쉽게 설명하였으니 @set 함수를 사용하는 것에 어려움을 느꼈다면 꼭 끝까지 읽었으면 하는 바람이다. 문제 결과 당연히 시간별로 GROUP BY 해서 COUNT로 하면 나올 줄 알았더니 그렇게 하면 데이터가 없는 시간은 출력되지 않는다. 1 2 3 4 5 SELECT to_char(datetime, 'HH24') datetime, count(animal_id) count from animal_outs CONNECT BY LEVEL =24 group by to_char(datetime, 'HH24') order by datetime Colored by Color Scripter cs 1) ORACLE 1 2 3 4 5 6 7 8..
2023.01.20 -
[mysql/oracle]오프라인/온라인 판매 데이터 통합하기
문제 결과 1) oracle 1 2 3 4 5 6 7 8 9 10 11 12 13 select * from( select to_char(sales_date, 'yyyy-mm-dd') sales_date, product_id,user_id, sales_amount from online_sale where to_char(sales_date, 'yyyy-mm') = '2022-03' union all select to_char(sales_date, 'yyyy-mm-dd') sales_date, product_id, null, sales_amount from offline_sale where to_char(sales_date, 'yyyy-mm') = '2022-03' ) order by sales_date, p..
2023.01.20 -
년, 월, 성별 별 상품 구매 회원 수 구하기
가장 정답이 이해 안 가는 문제 문제 결과 화면 결과 화면과 같이 나타나기 위해서는 날짜 부분의 달에서 0이 없어야 한다고 생각했다 01월이 아니라 1월로 뜨도록 그래서 정규식으로 0을 없앴는데 계속해서 오답이 나왔다 1 2 3 4 5 6 7 8 9 10 11 12 SELECT to_char(a.sales_date, 'yyyy') year, decode( substr(to_char(a.sales_date, 'mm'),1,1), '0', regexp_substr(to_char(a.sales_date, 'mm'), '[^0]' ,1,1) ) month, gender, count(distinct a.user_id) users FROM ONLINE_SALE A, USER_INFO B WHERE A.USER_ID..
2023.01.17 -
자동차 대여 기록에서 장기/단기 대여 구분하기
1 2 3 4 5 6 7 8 SELECT HISTORY_ID, CAR_ID, to_char(start_date, 'yyyy-mm-dd') START_DATE, to_char(end_date,'yyyy-mm-dd') END_DATE, case when (end_date - start_date)+1 >= 30 then '장기 대여' else '단기 대여' end as RENT_TYPE from car_rental_company_rental_history where to_char(start_date, 'yyyy-mm') = '2022-09' order by history_id desc; Colored by Color Scripter cs 계속 틀렸었던 이유가 (end_date - start_date)+1에서 ..
2023.01.17