database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 4597 게시물 읽기
No. 4597
음..어떡하면 좋아요 ㅜ.ㅜ
작성자
박지영
작성일
2003-02-27 18:17
조회수
1,510

음..그러니까 날짜별로 값을 평균내서 가져와야 되는데 조건이 날짜가 인터벌 된다는 것인데요.즉 특정일로부터 원하는 일까지 하루씩끊어서 해당하는 평균값을 가져와야 되요.나중에 호환문제때문에 왠만하면 내장 프로시져로 작성하면 안되구요 쿼리로 해결을 봐야 하는데.지금은 허접하게 union을 써서

 

select avg("Price") from "Order" where "Date">1046185200 and "Date"<=1046185200+3600 union all select avg("Price") from "Order" where "Date">1046185200+3600 and "Date"<=1046185200+3600*2 union all select avg("Price") from "Order" where "Date">1046185200+3600*2 and "Date"<=1046185200+3600*3 union all select avg("Price") from "Order" where "Date">1046185200+3600*3 and "Date"<=1046185200+3600*4 union all select avg("Price") from "Order" where "Date">1046185200+3600*4 and "Date"<=1046185200+3600*5 union all select avg("Price") from "Order" where "Date">1046185200+3600*5 and "Date"<=1046185200+3600*6 union all select avg("Price") from "Order" where "Date">1046185200+3600*6 and "Date"<=1046185200+3600*7 union all select avg("Price") from "Order" where "Date">1046185200+3600*7 and "Date"<=1046185200+3600*8 union all select avg("Price") from "Order" where "Date">1046185200+3600*8 and "Date"<=1046185200+3600*9 union all select avg("Price") from "Order" where "Date">1046185200+3600*9 and "Date"<=1046185200+3600*10 union all select avg("Price") from "Order" where "Date">1046185200+3600*10 and "Date"<=1046185200+3600*11 union all select avg("Price") from "Order" where "Date">1046185200+3600*11 and "Date"<=1046185200+3600*12 union all select avg("Price") from "Order" where "Date">1046185200+3600*12 and "Date"<=1046185200+3600*13 union all select avg("Price") from "Order" where "Date">1046185200+3600*13 and "Date"<=1046185200+3600*14 union all select avg("Price") from "Order" where "Date">1046185200+3600*14 and "Date"<=1046185200+3600*15 union all select avg("Price") from "Order" where "Date">1046185200+3600*15 and "Date"<=1046185200+3600*16 union all select avg("Price") from "Order" where "Date">1046185200+3600*16 and "Date"<=1046185200+3600*17 union all select avg("Price") from "Order" where "Date">1046185200+3600*17 and "Date"<=1046185200+3600*18 union all select avg("Price") from "Order" where "Date">1046185200+3600*18 and "Date"<=1046185200+3600*19 union all select avg("Price") from "Order" where "Date">1046185200+3600*19 and "Date"<=1046185200+3600*20 union all select avg("Price") from "Order" where "Date">1046185200+3600*20 and "Date"<=1046185200+3600*21 union all select avg("Price") from "Order" where "Date">1046185200+3600*21 and "Date"<=1046185200+3600*22 union all select avg("Price") from "Order" where "Date">1046185200+3600*22 and "Date"<=1046185200+3600*23 union all select avg("Price") from "Order" where "Date">1046185200+3600*23 and "Date"<=1046185200+3600*24;

 

이렇게 했는데...값은 확실히 나오는데..시간이 무지하게 걸려요.다른대안이 없을까 해서 이곳에 올렸어요.제 생각으로는 임시테이블에 검색된 모든 값을 올려 놓고 위처럼 쿼리를 날리는 건데요.데이터가 몇만개가 되서 속도가 보장이 안되네요. 좀 살려주세요 고수님ㅜ.ㅜ

이 글에 대한 댓글이 총 3건 있습니다.

그리구 위의 것은 한시간 간격으로 끊은건데요..ㅜ.ㅜ

박지영님이 2003-02-27 18:18에 작성한 댓글입니다.

이렇게 해보면 어떤가요

 

select avg("Price") from "Order"

-- where "Date" between xx and yy

group by date_part('hour',"Date");

황치영(gau)님이 2003-02-27 20:18에 작성한 댓글입니다.

황치영님의 쿼리에서 날짜 범위가 하루를 넘어가 버리면 다른날의 같은 시각이 같이 group by 될 것입니다. 이 문제도 함께 해결하려면,

group by 되어야할 놈은 yyyy-mm-dd HH24 가 되어야할 듯싶습니다.

 

아울러 그 결과가 무지막지한 비용을 만들것이라면, (데이터가 많다면, 분명 무지막지한 비용이 들 쿼리가 되겠지요)

 

그 yyyy-mm-dd HH24 결과를 만들어내는 것 자체를 인덱싱을 사용할 수도 있을겝니다. (이놈을 '부분인덱싱 기법'이라고 합니다)

 

아무튼 하루만을 생각한다면,

where "Date" between int4('2003-03-02 00:00:00'::abstime) and int4('int4('2003-03-02 23:59:59'::abstime) group by date_part('hour', "Date"::abstime)

이놈이 딱일듯, 황치영님 쿼리입니다(날짜 형변환만 했습니다)

김상기(ioseph)님이 2003-03-03 01:58에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
4601덤프관련..궁금한점 [3]
미투
2003-02-28
1342
4599솔라리스 설치할때여...
내맘
2003-02-28
1067
4598Vacuum 문제 발생. [1]
한태욱
2003-02-27
1385
4597음..어떡하면 좋아요 ㅜ.ㅜ [3]
박지영
2003-02-27
1510
4596설치후 삭제 그리고 다시 설치...-_-; [4]
서문교
2003-02-27
1631
4595날짜관련 변환 ?? [5]
미투
2003-02-25
3728
4594오라클에서 pgsql 로 변환시... sql문 [1]
어떤생각
2003-02-24
1696
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.020초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다