문제 1부터 10까지 숫자중 짝수만 출력
with emp2 as (select level as num
from dual
connect by level <=10)
select num
from emp2
where mod(num,2) = 0 ;
1부터 10 까지의 합을 출력
with emp2 as (select level as num
from dual
connect by level <=10)
select sum(num)
from emp2 ;
실험을 위한 결과를 출력할때 with 절을 사용해 쓴다.
e= exp
ln = 로그함수
(10) = 진수 들
select ln(10)
from dual
문제 e 의 10 승을 sql 로 구현하시오
select exp(10)
from dual;
1부터 10까지의 곱을 출력 (log 이해하기)
with emp2 as (select level as num
from dual
connect by level <=10)
select exp (sum(ln(num)))
from emp2;
1부터 6까지의 숫자중에 하나를 랜덤 으로 출력 (주사위를 던지세요)
select dbms_random.value(0.5,6.5)
from dual;
→ 결과값은 0.5 에서 6.5 사이의 난수가 출력이 된다.
→ sql 식에 round 를 둘러주게 되면 ?
select round(dbms_random.value(0.5,6.5))
from dual;
→ 1에서 6까지의 수가 랜덤으로 나온다.
문제 DICE1 (랜덤주사위) 를 10 번 던져서 나온 수를 구하시오.
with dice1 as (select round(dbms_random.value(0.5,6.5))as num
from dual
)
select num
from dice1
connect by level <= 10;
connect by level 절을 써야 with 절을 열번 실행하겠다는 의미이다.
문제 주사위를 100 번 던져서 숫자 3이 나올 확률
with dice1 as (select round(dbms_random.value(0.5,6.5))as num
from dual
)
select count(*)/100
from (select num
from dice1
connect by level <= 100)
where num = 3 ;
문제 주사위를 던져서 주사위의 눈이 홀수가 나올 확률 출력
with dice1 as (select round(dbms_random.value(0.5,6.5))as num
from dual
)
select count(*)/1000000
from (select num
from dice1
connect by level <= 1000000
)
where mod(num,2) = 1 ;
with dice1 as (select round(dbms_random.value(0.5,6.5))as num
from dual
)
select count(*)/1000000
from (select num
from dice1
connect by level <= 1000000)
where num in (1,3,5);
둘다 같은 식이다~~
문제 주사위 2개를 던져서 하나는 홀수, 하나는 짝수 나올 확률 출력
with dice_table as (select round(dbms_random.value(0.5,6.5))as dice1,
round(dbms_random.value(0.5,6.5))as dice2
from dual )
select count(*)/100000
from (select dice1, dice2
from dice_table
connect by level <= 100000
)
where (dice1 in (1,3,5) and dice2 in(2,4,6)
) or
(dice1 in (2,4,6) and dice2 in(1,3,5)
);
하나가 짝수나올 확률은 1/2 , 홀수가 나올 확률은 1/2 인데 동시에 발생하니까 곱해주면 1/4 이여서 0.25
문제 3개의 주사위를 랜덤으로 던졌을때 각 주사위의 값을 합했을때 10 이 나올 확률은 ?
with dice_table as (select round(dbms_random.value(0.5,6.5))as dice1,
round(dbms_random.value(0.5,6.5))as dice2,
round(dbms_random.value(0.5,6.5))as dice3
from dual )
select count(*)/100000
from (select dice1, dice2, dice3
from dice_table
connect by level <= 100000
)
where dice1+dice2+dice3 = 10 ;
'Oracle > SQL' 카테고리의 다른 글
SQL - DCL 문 (0) | 2021.12.05 |
---|---|
SQL - WITH 절 사용 서브쿼리 Factoring (0) | 2021.12.05 |
SQL -WITH 절 (가상테이블) (0) | 2021.12.05 |
SQL 제약 (5) Foreign Key (자식키) (0) | 2021.12.05 |
SQL (4) 제약 CHECK (0) | 2021.12.05 |