Skip to content

Instantly share code, notes, and snippets.

@phpdave
Created March 30, 2016 16:23
Show Gist options
  • Select an option

  • Save phpdave/da2d049559afd31f414e4fcacb0d4de6 to your computer and use it in GitHub Desktop.

Select an option

Save phpdave/da2d049559afd31f414e4fcacb0d4de6 to your computer and use it in GitHub Desktop.
SELECT
a.KEY1,
a.KEY2,
SUM(COALESCE (TOTAL_FIELD3,0)) AS TOTAL_FIELD3,
SUM(COALESCE (TOTAL_FIELD4,0)) AS TOTAL_FIELD4
FROM
TABLE1 a
LEFT JOIN
(SELECT
SUM(FIELD3) AS TOTAL_FIELD3,
T1KEY
FROM
TABLE2
WHERE
MYDATEFIELD <= PARM1
GROUP BY
T1KEY ) b
ON
a.T1KEY=b.T1KEY
LEFT JOIN
(SELECT
SUM(FIELD4) AS TOTAL_FIELD4,
T1KEY
FROM
TABLE2
WHERE
MYDATEFIELD >= PARM2 AND
MYDATEFIELD <= PARM1
GROUP BY
T1KEY ) c
ON
a.T1KEY=c.T1KEY
GROUP BY KEY1,KEY2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment