I have created the stored procedure shown below. I want to look up certain values in a type table and assign them to variables like AmexType then use those values in a report query. I know one solution is to join the type tables to the report query, but I don't like that solution for readability and maintainability. When I run this report the Type varialbles are empty and only the last column has values. The value column in xlattable is a varchar that I am trying to CAST to an integer. Here are the 2 problems I am trying to solve:
1 - The first four type queries appear as empty tables in the report. I would like to hide them.
2 - The type values are empty so the first four columns have values of zero. I want to correct the query to show the values as integers.
DROP PROCEDURE IF EXISTS sp_DBR_payasr_Payments_By_Method
$$
CREATE PROCEDURE
sp_DBR_payasr_Payments_By_Method
( StartDate Date, EndDate Date )
BEGIN
DECLARE AmexType INT; DECLARE CreditCardType INT; DECLARE AchType INT; DECLARE InvoiceType INT;
SELECT AmexType = CAST(value AS UNSIGNED) FROM payasr.xlattable WHERE field_name = 'card_type' AND descr = 'Amex'; SELECT CreditCardType = CAST(value AS UNSIGNED) FROM payasr.xlattable WHERE field_name = 'payment_method_type' AND descr = 'CreditCard'; SELECT AchType = CAST(value AS UNSIGNED) FROM payasr.xlattable WHERE field_name = 'payment_method_type' AND descr = 'ACH'; SELECT InvoiceType = CAST(value AS UNSIGNED) FROM payasr.xlattable WHERE field_name = 'payment_method_type' AND descr = 'Invoice';
SELECT DATE_FORMAT(lc.payment_dt,'%Y-%m-%d') AS 'Date', SUM(CASE lc.card_type WHEN AmexType THEN COALESCE(lc.amount, 0) ELSE 0 END) AS 'AMX', SUM(CASE WHEN lc.payment_method_type = CreditCardType AND lc.card_type <> AmexType THEN COALESCE(lc.amount, 0) ELSE 0 END) AS 'Credit Cards', SUM(CASE WHEN lc.payment_method_type = AchType THEN COALESCE(lc.amount, 0) ELSE 0 END) AS 'ACH', SUM(CASE WHEN lc.payment_method_type = InvoiceType THEN COALESCE(lc.amount, 0) ELSE 0 END) AS 'Invoice', SUM(COALESCE(lc.amount, 0)) AS 'Total' FROM payasr.loan_collection lc WHERE lc.payment_dt >= StartDate AND lc.payment_dt <= EndDate AND lc.process_code = 'SUCCESS' GROUP BY lc.payment_dt ORDER BY lc.payment_dt;
END $$