KPI4b.sql
/**
* This query will build up the 3 temporary tables,
* and the runner will then query these tables.
*/
-- Create all the temporary tables we need...
);
);
);
-- Ensure they're empty before we insert.
-- Insert the data we want into these temp tables.
INSERT INTO `nacl_crosstab_1` (`JOB_NO`, `NACL_DATE`)
INSERT INTO `nacl_crosstab_2` (`JOB_NO`, `NACL_DATE`) AND (l.
`DATE` > c.
`NACL_DATE`)
INSERT INTO `nacl_crosstab_3` (`JOB_NO`, `NACL_DATE`) AND (l.
`DATE` > c.
`NACL_DATE`) c.`JOB_NO`;
-- Finally, get the results in a usable format.
r.`HOUSE_KEY`,
TRIM(h.
`HOUSENO`) AS `HOUSENO`, h.`ROAD`,
h.`DISTRICT`,
h.`POSTCODE`,
r.`JOB_NO`,
n1.
`NACL_DATE` AS `NACL_1`, n2.
`NACL_DATE` AS `NACL_2`, n3.
`NACL_DATE` AS `NACL_3` repairs r
house h
ON (h.
`HOUSE_KEY` = r.
`HOUSE_KEY`) `nacl_crosstab_1` n1
ON (n1.
`JOB_NO` = r.
`JOB_NO`) `nacl_crosstab_2` n2
ON (n2.
`JOB_NO` = r.
`JOB_NO`) `nacl_crosstab_3` n3
ON (n3.
`JOB_NO` = r.
`JOB_NO`); AND (r.
`CALL_TYPE` = [calltype
])