다음과 같은 쿼리가 있습니다 (단순화를 위해 축약되고 이름이 변경됨).
SELECT
SOME_COLUMN AS value1,
SOME_COMMON_ID as commonID,
SOME_TAG as tagID,
SOME_TIMESTAMP as endTime,
( SELECT
SOME_TIMESTAMP AS beginTime
FROM
EVENTLIST
WHERE
EVENTLIST.SOME_TAG = 'BEGIN'
AND EVENTLIST.SOME_COMMON_ID = commonID /* <-- Invalid column name commonID */
),
endTime - beginTime AS duration
FROM
EVENTLIST
JOIN
(...some irrelevant lookups on other tables)
WHERE
(...some criteria...)
내가 이루고 싶은 것 :
이 테이블은 일부 이벤트를 기록하며 이벤트가 발생한 시간은에 저장됩니다 SOME_TIMESTAMP
. 여러 이벤트는라는 공통 식별자로 그룹화됩니다 SOME_COMMON_ID
. 이벤트 유형은SOME_TAG
각 이벤트에 대해 태그가있는 이벤트 이후 현재 이벤트의 기간을 선택하고 싶습니다. BEGIN
SQL Server에서 이것을 달성하는 방법은 무엇입니까?
생각보다 훨씬 쉬웠습니다.
SELECT
SOME_COLUMN AS value1,
SOME_COMMON_ID as commonID,
SOME_TAG as tagID,
SOME_TIMESTAMP as endTime,
( SELECT
DATEDIFF(SECOND, e2.SOME_TIMESTAMP, e1.SOME_TIMESTAMP) AS duration
/* ^-- calc the diff here, not in the outer query */
FROM
EVENTLIST e2
WHERE
e2.SOME_TAG = 'BEGIN'
AND e2.SOME_COMMON_ID = e1.SOME_COMMON_ID /* <-- qualify table names */
)
FROM
EVENTLIST e1 /* <-- name required */
JOIN
(...some irrelevant lookups on other tables)
WHERE
(...some criteria...)
테이블을 한정하고 내부 쿼리에서 차이를 계산해야했습니다. 내부 쿼리의 차이를 계산해야하므로 가능합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다