로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다.1
순위당첨 내용
1
6개 번호가 모두 일치
2
5개 번호가 일치
3
4개 번호가 일치
4
3개 번호가 일치
5
2개 번호가 일치
6(낙첨)
그 외
로또를 구매한 민우는 당첨 번호 발표일을 학수고대하고 있었습니다. 하지만, 민우의 동생이 로또에 낙서를 하여, 일부 번호를 알아볼 수 없게 되었습니다. 당첨 번호 발표 후, 민우는 자신이 구매했던 로또로 당첨이 가능했던 최고 순위와 최저 순위를 알아보고 싶어 졌습니다. 알아볼 수 없는 번호를0으로 표기하기로 하고, 민우가 구매한 로또 번호 6개가44, 1, 0, 0, 31 25라고 가정해보겠습니다. 당첨 번호 6개가31, 10, 45, 1, 6, 19라면, 당첨 가능한 최고 순위와 최저 순위의 한 예는 아래와 같습니다.
당첨 번호3110451619결과
최고 순위 번호
31
0→10
44
1
0→6
25
4개 번호 일치, 3등
최저 순위 번호
31
0→11
44
1
0→7
25
2개 번호 일치, 5등
순서와 상관없이, 구매한 로또에 당첨 번호와 일치하는 번호가 있으면 맞힌 걸로 인정됩니다.
알아볼 수 없는 두 개의 번호를 각각 10, 6이라고 가정하면 3등에 당첨될 수 있습니다.
3등을 만드는 다른 방법들도 존재합니다. 하지만, 2등 이상으로 만드는 것은 불가능합니다.
알아볼 수 없는 두 개의 번호를 각각 11, 7이라고 가정하면 5등에 당첨될 수 있습니다.
5등을 만드는 다른 방법들도 존재합니다. 하지만, 6등(낙첨)으로 만드는 것은 불가능합니다.
민우가 구매한 로또 번호를 담은 배열 lottos, 당첨 번호를 담은 배열 win_nums가 매개변수로 주어집니다. 이때, 당첨 가능한 최고 순위와 최저 순위를 차례대로 배열에 담아서 return 하도록 solution 함수를 완성해주세요.
Group by & order by & 서브쿼리를활용 (key = group by시가장상위의데이터로그룹핑되므로, order by를사용하면내가원하는데이터를위로올릴수있다)
아이디어
그룹핑 (group by yyyymmdd) + 정렬(order by yyyymm), count(데이터수)
서브쿼리2 : 위의내용을월별로그룹핑
그룹핑 (Group by yyyymm)
그룹함수를이용한결과출력
기존쿼리
select
max(bill_dt.readDt) readDt,
max(bill_dt.oneMonthAgo) oneMonthAgo,
max(bill_dt.twoMonthAgo) twoMonthAgo,
max(bill_dt.threeMonthAgo) threeMonthAgo
from (
select
case
when substring(READ_DT,1,6) = date_format(concat( ${read_dt},'01'), '%Y%m') then READ_DT
end as 'readDt',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -1 Month), '%Y%m') then READ_DT
end as 'oneMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -2 Month), '%Y%m') then READ_DT
end as 'twoMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -3 Month), '%Y%m') then READ_DT
end as 'threeMonthAgo'
from gnd_meter_billing gmb
where 조건 1
and 기간 between 기간1 and 기간2
group by substring(READ_DT,1,6), substring(READ_DT,7,2)
order by cnt_readDt desc
) bill_dt
수정쿼리
select
max(bill_dt.readDt) readDt,
max(bill_dt.oneMonthAgo) oneMonthAgo,
max(bill_dt.twoMonthAgo) twoMonthAgo,
max(bill_dt.threeMonthAgo) threeMonthAgo
from (
select *
from (
select
count(READ_DT) cnt_readDt,
substring(READ_DT,1,6) yyyymm,
case
when substring(READ_DT,1,6) = date_format(concat( ${read_dt},'01'), '%Y%m') then READ_DT
end as 'readDt',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -1 Month), '%Y%m') then READ_DT
end as 'oneMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -2 Month), '%Y%m') then READ_DT
end as 'twoMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -3 Month), '%Y%m') then READ_DT
end as 'threeMonthAgo'
from gnd_meter_billing gmb
where 조건 1
and 기간 between 기간1 and 기간2
group by substring(READ_DT,1,8)
order by substring(READ_DT,1,6), cnt_readDt desc
) bill_dt_by_yyyymmdd
group by bill_dt_by_yyyymmdd.yyyymm
) bill_dt
select max(bill_dt.readDt) readDt, max(bill_dt.oneMonthAgo) oneMonthAgo, max(bill_dt.twoMonthAgo) twoMonthAgo, max(bill_dt.threeMonthAgo) threeMonthAgo, max(bill_dt.fourMonthAgo) fourMonthAgo, max(bill_dt.twelveMonthAgo) twelveMonthAgo, max(bill_dt.thirteenMonthAgo) thirteenMonthAgo from ( select case when substring(READ_DT,1,6) = '202203' then READ_DT end as 'readDt', case when substring(READ_DT,1,6) = '202202' then READ_DT end as 'oneMonthAgo', case when substring(READ_DT,1,6) = '202201' then READ_DT end as 'twoMonthAgo', case when substring(READ_DT,1,6) = '202112' then READ_DT end as 'threeMonthAgo', case when substring(READ_DT,1,6) = '202111' then READ_DT end as 'fourMonthAgo', case when substring(READ_DT,1,6) = '202103' then READ_DT end as 'twelveMonthAgo', case when substring(READ_DT,1,6) = '202102' then READ_DT end as 'thirteenMonthAgo' from gnd_meter_billing gmb whereREAD_DT between 20210301000000 and 20220322000000 group by substring(READ_DT,1,6), substring(READ_DT,7,2) ) bill_dt ;