SELECT c.Corporation, u.invoicenum, u.otherinfo, (r.firstname + ' ' + r.lastname) AS requestedby, (s.firstname + ' ' + s.lastname) AS returnto,
u.justification, u.costcenter, e.expelement, (rtrim(p.projectnum) + '-' + ltrim(p.name)) AS project, u.amount,u.quantity
FROM ((SELECT h.justification, isNull(d.costcenter, SCRCostCenter) AS costcenter, isNull(d.expelementid, SCRExpElement) AS expelementid,
d.amount, h.vendorid, isNull(d.projectid, SCRProject) AS projectid, h.datecreated, h.RequestedBy, h.SiteUserID, h.CorpID,
h.invoicenum, d.quantity, h.otherinfo
FROM CRDetail AS d
INNER JOIN CRHeader AS h ON d.CRNumber = h.CRNumber
WHERE d.deleted = 0 AND h.deleted = 0
AND (h.datecreated BETWEEN '01/01/2006' AND '08/07/2007'))
UNION ALL
(SELECT h.justification, isNull(d.costcenter, SCRCostCenter) AS costcenter, isNull(d.expelementid, SCRExpElement) AS expelementid, d.amount,
h.vendorid, isNull(d.projectid, SCRProject) AS projectid, IsNull(h.DisplayDate, h.DateCreated) AS datecreated,
'' AS RequestedBy, '' AS SiteUserID, '' AS CorpID, h.invoicenum, d.quantity, h.notes AS otherinfo
FROM OEDetail AS d
INNER JOIN OEHeader AS h ON d.OENumber = h.OENumber
WHERE d.deleted = 0 AND h.deleted = 0
AND (IsNull(h.displaydate, h.datecreated) BETWEEN '01/01/2006' AND '08/07/2007'))
) AS u
LEFT OUTER JOIN SiteUser AS r ON u.RequestedBy = r.SiteUserID
LEFT OUTER JOIN SiteUser AS s ON u.SiteUserID = s.SiteUserID
LEFT OUTER JOIN Corporation AS c ON u.CorpID = c.CorpID
INNER JOIN ExpenseElement AS e ON u.ExpElementID = e.ExpElementID
INNER JOIN Project AS p ON u.ProjectID = p.ProjectID
WHERE u.vendorID = 1
ORDER BY u.DateCreated