-- Join data from the PQ Dashboard and the Outages system
SELECT line.ID,
line.AssetKey as "GTC Line ID", 
mline.LineName,
event.ID as "PQ Event ID",
event.StartTime as "PQ Event Start Time",
outages.CaseID as "Outages Case ID",
outages.StartTime as "Outages Start Time",
outages.MirsCauseID as "MIRS Cause ID",
outages.CauseDesc as "Cause Description"
FROM line
JOIN MeterLine as mline ON line.ID = mline.LineID
JOIN Event ON line.ID = event.LineID
LEFT OUTER JOIN Outages AS outages ON (outages.LineId = line.AssetKey AND CAST(outages.StartTime AS DATE) = CAST(event.StartTime AS DATE))
WHERE event.eventtypeid=1
AND line.AssetKey not like '%BR%'
--AND line.AssetKey = 4560
--ORDER BY mline.LineName, event.StartTime
ORDER BY event.StartTime, mline.LineName
