I am using the below Select statement, which works fine. However, when I change the values below in Red to ? as is done in the WHERE clause toward the end of the statement in order to use parameters defined in specific cells in a worksheet, I get a "Numeric Value Out of Range" error. I do not know how to fix this as the cells I'm referencing for the parameters are exactly the same as those I'm referencing for the CDFYR parameters further below in the WHERE clause (which work fine). In other words, I have the value 2017 loaded into cell B1 and 2018 loaded into cell B2. Cells B1 and B2 are what I'm trying to use as my reference points for all of the CDFYR parameters.
SELECT DISTINCT
CDPAYEE AS "Payee #",
PAYEENAME AS "Payee Name",
MMNAME AS "Manufacturer Name",
ECHECK AS "Lead Name",
SUM ( CASE
WHEN CDFYR = 2017 THEN CDBASE
ELSE 0
END ) AS "Last FYTD COGS",
SUM ( CASE
WHEN CDFYR = 2018 THEN CDBASE
ELSE 0
END ) AS "This FYTD COGS"
FROM
SALESQRY.CSTQRYD CSTQRYD
LEFT OUTER JOIN QS36F.PAYEEVTL PAYEEVTL
ON
CSTQRYD.CDPAYEE = PAYEEVTL.PAYEE#
LEFT OUTER JOIN QS36F.PAYEEMANU PAYEEMANU
ON
PAYEEVTL.PAYEE# = PAYEEMANU.PPAYEE
LEFT OUTER JOIN QS36F.MANUFACT MANUFACT
ON
PAYEEMANU.PMANU# = MANUFACT.MMANU#
LEFT OUTER JOIN QS36F.EMPLQ EMPLQ
ON
PAYEEVTL.LEADID = EMPLQ.EEMPNUM
WHERE
CDFYR BETWEEN ? AND ?
AND CDPER BETWEEN ? AND ?
GROUP BY
CDPAYEE,
PAYEENAME,
MMNAME,
ECHECK
SELECT DISTINCT
CDPAYEE AS "Payee #",
PAYEENAME AS "Payee Name",
MMNAME AS "Manufacturer Name",
ECHECK AS "Lead Name",
SUM ( CASE
WHEN CDFYR = 2017 THEN CDBASE
ELSE 0
END ) AS "Last FYTD COGS",
SUM ( CASE
WHEN CDFYR = 2018 THEN CDBASE
ELSE 0
END ) AS "This FYTD COGS"
FROM
SALESQRY.CSTQRYD CSTQRYD
LEFT OUTER JOIN QS36F.PAYEEVTL PAYEEVTL
ON
CSTQRYD.CDPAYEE = PAYEEVTL.PAYEE#
LEFT OUTER JOIN QS36F.PAYEEMANU PAYEEMANU
ON
PAYEEVTL.PAYEE# = PAYEEMANU.PPAYEE
LEFT OUTER JOIN QS36F.MANUFACT MANUFACT
ON
PAYEEMANU.PMANU# = MANUFACT.MMANU#
LEFT OUTER JOIN QS36F.EMPLQ EMPLQ
ON
PAYEEVTL.LEADID = EMPLQ.EEMPNUM
WHERE
CDFYR BETWEEN ? AND ?
AND CDPER BETWEEN ? AND ?
GROUP BY
CDPAYEE,
PAYEENAME,
MMNAME,
ECHECK