Sorry, I just realized that I actually started from an older version of the query which still relied on the gps table. In the case of the sample project, removing GPS doesn’t make any noticeable difference. It may with larger datasets, perhaps, so here is the revised version without GPS.
CREATE VIEW allruns
AS
SELECT
t2.runID as runID,
t3.batchID as batchID,
t2.done as done,
CASE WHEN t12.motusTagID is null then t2.motusTagID else t12.motusTagID end as motusTagID,
t12.ambigID as ambigID,
t2.ant as port,
t2.len as runLen,
t2.tsBegin as tsBegin,
t2.tsEnd as tsEnd,
t3.monoBN as bootnum,
t4.projectID as tagProjID,
t4.mfgID as mfgID,
t4.type as tagType,
t4.codeSet as codeSet,
t4.manufacturer as mfg,
t4.model as tagModel,
t4.lifeSpan as tagLifespan,
t4.nomFreq as nomFreq,
t4.bi as tagBI,
t4.pulseLen as pulseLen,
t5.deployID as tagDeployID,
t5.speciesID as speciesID,
t5.markerNumber as markerNumber,
t5.markerType as markerType,
t5.tsStart as tagDeployStart,
t5.tsEnd as tagDeployEnd,
t5.latitude as tagDeployLat,
t5.longitude as tagDeployLon,
t5.elevation as tagDeployAlt,
t6a.deviceID as deviceID,
t6.deployID as recvDeployID,
t6.latitude as recvDeployLat,
t6.longitude as recvDeployLon,
t6.elevation as recvDeployAlt,
t6a.serno as recv,
t6.name as recvDeployName,
t6.siteName as recvSiteName,
t6.isMobile as isRecvMobile,
t6.projectID as recvProjID,
t7.antennaType as antType,
t7.bearing as antBearing,
t7.heightMeters as antHeight,
t8.english as speciesEN,
t8.french as speciesFR,
t8.scientific as speciesSci,
t8.group as speciesGroup,
t9.label as tagProjName,
t10.label as recvProjName
FROM
runs AS t2
left join allambigs t12 on t2.motusTagID = t12.ambigID
LEFT JOIN
batchRuns AS t3a ON t2.runID = t3a.runID
LEFT JOIN
batches AS t3 ON t3.batchID = t3a.batchID
LEFT JOIN
tags AS t4 ON t4.tagID = CASE WHEN t12.motusTagID is null then t2.motusTagID else t12.motusTagID end
LEFT JOIN
tagDeps AS t5 ON t5.tagID = CASE WHEN t12.motusTagID is null then t2.motusTagID else t12.motusTagID end
AND t5.tsStart =
(SELECT
max(t5b.tsStart)
FROM
tagDeps AS t5b
WHERE
t5b.tagID = CASE WHEN t12.motusTagID is null then t2.motusTagID else t12.motusTagID end
AND t5b.tsStart <= t2.tsBegin
AND (t5b.tsEnd IS NULL OR t5b.tsEnd >= t2.tsBegin)
)
LEFT JOIN
recvs as t6a on t6a.deviceID =t3.motusDeviceID
LEFT JOIN
recvDeps AS t6 ON t6.deviceID = t3.motusDeviceID AND
t6.tsStart =
(SELECT
max(t6b.tsStart)
FROM
recvDeps AS t6b
WHERE
t6b.deviceID=t3.motusDeviceID
AND t6b.tsStart <= t2.tsBegin
AND (t6b.tsEnd IS NULL OR t6b.tsEnd >= t2.tsBegin)
)
LEFT JOIN
antDeps AS t7 ON t7.deployID = t6.deployID AND t7.port = t2.ant
LEFT JOIN
species AS t8 ON t8.id = t5.speciesID
LEFT JOIN
projs AS t9 ON t9.ID = t5.projectID
LEFT JOIN
projs AS t10 ON t10.ID = t6.projectID
