[mysql/oracle]오프라인/온라인 판매 데이터 통합하기
              
          2023. 1. 20. 17:08ㆍProgrammers/SQL
문제

결과

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, product_id, user_id | cs | 
처음에 이 코드를 사용했을 때 오류가 많이 생겼다.
그 이유는 mysql로 돌려졌기 때문이다.
첫번째 오류: Every derived table must have its own alias -> 서브쿼리에 이름이 없기 때문
해결 : data란 이름을 줬다
두번째 오류 : execute command denied to user 'USERD453CC44C96B0C3F4124EA8E47'@'%' for routine 'sql_runner_run.to_char'
-> mysql에는 to_char를 쓸 수 없다
해결 : to_char 부분을 date_format으로 바꿔 사용했다
2) mysql
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | select * from(     select date_format(sales_date, '%Y-%m-%d') sales_date          , product_id          , user_id          , sales_amount     from online_sale     where date_format(sales_date, '%Y-%m') = '2022-03'     union all     select date_format(sales_date, '%Y-%m-%d') sales_date          , product_id          , null as user_id          , sales_amount     from offline_sale     where date_format(sales_date, '%Y-%m') = '2022-03'     ) data order by sales_date, product_id, user_id | cs | 
'Programmers > SQL' 카테고리의 다른 글
| [mysql/oracle]이름이 있는 동물의 아이디 (0) | 2023.01.26 | 
|---|---|
| [mysql/oracle]경기도에 위치한 식품창고 목록 출력하기 (0) | 2023.01.20 | 
| [oracle/mysql]입양 시각 구하기(2) (2) | 2023.01.20 | 
| 년, 월, 성별 별 상품 구매 회원 수 구하기 (0) | 2023.01.17 | 
| 자동차 대여 기록에서 장기/단기 대여 구분하기 (0) | 2023.01.17 |