2016-10-27 3 views
1

타임 스탬프가 ts와 nextts 사이에있는 다음 행을 캡처하려고합니다. 나는 에포크 (Epoch) 형태의 ts와 nextts가있는 컬럼을 가지고있다.Sqlite의 subselect에서 Epoch 비교

id,ts,tsepoch,origin,product,bid,ask,nextts,nexttsepoch 
1,2016-10-18 20:20:54.733,1476822054733,sourceA,EUR/USD,1.09812,1.0982,2016-10-18 20:20:59.579,1476822059579 
2,2016-10-18 20:20:55.093,1476822055093,sourceA,UK 100,7010.5,7011.5,2016-10-18 20:20:57.972,1476822057972 
3,2016-10-18 20:20:55.149,1476822055149,sourceA,US 30,18159.0,18161.0,2016-10-18 20:20:55.871,1476822055871 
4,2016-10-18 20:20:55.871,1476822055871,sourceA,US 30,18159.0,18161.0,2016-10-18 20:20:57.256,1476822057256 
5,2016-10-18 20:20:57.256,1476822057256,sourceA,US 30,18160.0,18162.0,2016-10-18 20:20:58.745,1476822058745 

왜 다음 쿼리가 null을 반환합니까?

select _rowid_, ts, nextts, origin, bid, ask, 
(select sub.bid from end as sub where sub.origin <> origin and sub.product = product 
and sub.tsepoch > tsepoch and sub.tsepoch < nexttsepoch) as bid 

from test 
order by ts; 

왜 두 개의 정수 열을 비교하면 아무 것도 반환하지 않는 이유를 알 수 없습니다. 타임 스탬프가 현재 것보다 큰 값을 반환하려고하면 null도 반환됩니다.

select _rowid_, ts, nextts, origin, bid, ask, 
(select sub.bid from end as sub where sub.ts > ts) as bid 

from end where product = "UK 100" 
order by ts; 

올바른 DB 구조가 있습니까? ts와 nextts는 datetime이고 tsepoch와 nexttsepoch는 숫자입니다.

+0

스키마를 표시해주세요. 하위 쿼리를 개별적으로 실행하고 수행중인 작업을 확인 했습니까? – Schwern

+1

'ts'와'tsepoch '는 같은 날짜와 시간을 나타내지 않습니다. 그럴 예정입니까? 그렇지 않다면, 그들은보다 기술적 인 이름을 사용할 수 있으며 일관성을 위해 같은 유형을 사용해야합니다. 그렇다면 데이터 중복을 피하기 위해 하나의 열로 접혀 있어야합니다. – Schwern

+0

형식은 "epoch"가 아니라 "[epoch] (https://en.wiktionary.org/wiki/epoch) 이후의 밀리 초"입니다. 그리고 SQLite는 초를 대신 사용하기 때문에 자바 타임 스탬프로 보입니다. –

답변

1

문제는이 하위 쿼리입니다.

select sub.bid 
from end as sub 
where sub.origin <> origin and 
     sub.product = product and 
     sub.tsepoch > tsepoch and 
     sub.tsepoch < nexttsepoch 

각 행을 자체와 비교합니다. 조인 테이블이 포함되어 있지 않으므로 originsub.origin은 모두 end.origin입니다. 그것은 서면 같다 :

select bid 
from end 
where origin <> origin and 
     product = product and 
     tsepoch > tsepoch and 
     tsepoch < nexttsepoch 

가 대신 아마 자체 조인 end의 다른 열이 end의 열을 비교할 필요합니다.

select end1.bid 
from end as end1 
join end as end2 on end1.id <> end2.id 
where end1.origin <> end2.origin and 
     end1.product = end2.product and 
     end1.tsepoch > end2.tsepoch and 
     end1.tsepoch < end2.nexttsepoch 

모두가 동일한 원점을 가지고 있으며 결과를 필터링하는 where 절과 함께 더 이상 문제가 없기 때문에 아무 것도 반환하지 않습니다.

+1

죄송합니다. 자체 조인이 잘못되었습니다. 기다려주세요. – Schwern

+1

신경 쓰지 마세요, 맞습니다. 그들은 모두 같은 '기원'을 가지고 있습니다. – Schwern

+0

아무 것도 반환하지 않는 쿼리를 가져올 수 없습니다. 일부 열의 색인을 생성해야합니까? – user2146441