oracle 쿼리 정리

sql 기간별 통계 query, group by

js

const date = new Date()
if (timeunit === 'all') {
} else if (timeunit === 'day') {
regStart = formatDate(date)
regEnd = formatDate(new Date(date.valueOf() + 1000 \* 3600 \* 24))
console.log(regStart, regEnd)
} else if (timeunit === 'week') { // 이번주 월요일 ~ 일요일
regStart = getMonday(new Date())
regEnd = getMonday(new Date(date.valueOf() + 1000 \* 3600 \* 24 \* 7))
} else if (timeunit === 'month') { // 이번달 1일 ~ 31일
regStart = formatDate(new Date(date.getFullYear(), date.getMonth(), 1))
regEnd = formatDate(new Date(date.getFullYear(), date.getMonth() + 1, 0))
}

function formatDate (date) {
const d = new Date(date)
let month = '' + (d.getMonth() + 1)
let day = '' + d.getDate()
const year = d.getFullYear()
if (month.length < 2) month = '0' + month
if (day.length < 2) day = '0' + day
return \[year, month, day\].join('-')
}

function getMonday (d) {
d = new Date(d)
const day = d.getDay()
const diff = d.getDate() - day + (day === 0 ? -6 : 1) // adjust when day is sunday
return new Date(d.setDate(diff))
}

월별

SELECT pv\_cnt
, uv\_cnt
, uv.conn\_time
FROM
(SELECT sum(pv\_cnt) AS pv\_cnt
, to\_char(conn\_time, 'YYYY-MM') AS conn\_time
FROM portal.bmt\_pv\_conn
GROUP BY to\_char(conn\_time, 'YYYY-MM')) pv,
(SELECT sum(uv\_cnt) AS uv\_cnt
, to\_char(conn\_time, 'YYYY-MM') AS conn\_time
FROM portal.bmt\_uv\_conn
GROUP BY to\_char(conn\_time, 'YYYY-MM')) uv
WHERE pv.conn\_time = uv.conn\_time
AND uv.conn\_time BETWEEN $1 AND $2
ORDER BY pv.conn\_time DESC

SELECT
pv\_cnt,
uv\_cnt,
uv.conn\_time
FROM
(SELECT
sum(pv\_cnt) AS pv\_cnt,
to\_char(conn\_time, 'YYYY-MM-DD') AS conn\_time
FROM portal.bmt\_pv\_conn
GROUP BY conn\_time) pv,
(SELECT
sum(uv\_cnt) AS uv\_cnt,
to\_char(conn\_time, 'YYYY-MM-DD') AS conn\_time
FROM portal.bmt\_uv\_conn
GROUP BY
conn\_time) uv
WHERE pv.conn\_time = uv.conn\_time
AND uv.conn\_time BETWEEN $1 AND $2
ORDER BY pv.conn\_time DESC
SELECT pv\_cnt
, uv\_cnt
, uv.conn\_time
FROM
(SELECT sum(pv\_cnt) AS pv\_cnt
, ceil((conn\_time - trunc(trunc(conn\_time,'mm'),'iw') + 1) / 7) AS conn\_time
FROM portal.bmt\_pv\_conn
GROUP BY ceil((conn\_time - trunc(trunc(conn\_time,'mm'),'iw') + 1) / 7)) pv,
(SELECT sum(uv\_cnt) AS uv\_cnt
, ceil((conn\_time - trunc(trunc(conn\_time,'mm'),'iw') + 1) / 7) AS conn\_time
FROM portal.bmt\_uv\_conn
GROUP BY ceil((conn\_time - trunc(trunc(conn\_time,'mm'),'iw') + 1) / 7)) uv
WHERE pv.conn\_time = uv.conn\_time
--AND uv.conn\_time BETWEEN $1 AND $2
ORDER BY pv.conn\_time DESC
SELECT pv\_cnt
, uv\_cnt
, uv.conn\_time
FROM
(SELECT sum(pv\_cnt) AS pv\_cnt
, date\_trunc('week', conn\_time) AS conn\_time
FROM portal.bmt\_pv\_conn
GROUP BY date\_trunc('week', conn\_time)) pv,
(SELECT sum(uv\_cnt) AS uv\_cnt
, date\_trunc('week', conn\_time) AS conn\_time
FROM portal.bmt\_uv\_conn
GROUP BY date\_trunc('week', conn\_time)) uv
WHERE pv.conn\_time = uv.conn\_time
-- AND uv.conn\_time BETWEEN $1 AND $2
ORDER BY pv.conn\_time DESC

KakaoTalk 20171129 152739242 KakaoTalk 20171027 142012325 1024x345

이전 글, 제목, 다음 글, 제목

SELECT no,title from table
WHERE no = (SELECT max(no)
                 FROM table
                 WHERE no < '${id}')

SELECT no,title from table
WHERE no = (SELECT min(no)
                 FROM table
                 WHERE no > '${id}')

상관관계 서브 쿼리

Correlated subquery

SELECT A.id ,A.order_no FROM pay A WHERE A.id = ( SELECT MAX(B.id) FROM pay B WHERE A.order_no = B.order_no );

postgresql COALESCE 함수, oracle NVL 함수

오라클에서는 질의 한 값이 null일경우 다른 값을 반환해주는 NVL 함수가 있다.

오라클 :  NVL(A,B) 함수는 A 가 NULL 이면 B 를 반환

postgresql에는 nvl함수가 없다.

대신 COALESCE 함수로 사용 가능

COALESCE(컬럼명, 반환 값)

COALESCE(comm, 0)

— 아래와 같이 질의 함

SELECT COMM, COALESCE(COMM,0) FROM TABLE

COALESCE은 오라클에서도 사용 가능

참고 :  그루비 NVL, NVL2, NULLIF, COALESCE

http://www.gurubee.net/lecture/1880


Written by@MuseKim
Muse Kim